Skip to content

feat(typescript-estree): experimental option to cache type checking APIs #6426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .eslintignore.all
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
node_modules
dist
jest.config.js
fixtures
coverage
__snapshots__
.docusaurus
build

# Files copied as part of the build
packages/types/src/generated/**/*.ts

# Playground types downloaded from the web
packages/website/src/vendor

# Additionally, JS files
**/*.js
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ module.exports = {
allowAutomaticSingleRunInference: true,
tsconfigRootDir: __dirname,
warnOnUnsupportedTypeScriptVersion: false,
EXPERIMENTAL_memoizeTypeCheckingAPIs:
!!process.env.MEMOIZE_TYPE_CHECKING_APIS,
EXPERIMENTAL_useSourceOfProjectReferenceRedirect: false,
cacheLifetime: {
// we pretty well never create/change tsconfig structure - so need to ever evict the cache
Expand Down
22 changes: 22 additions & 0 deletions .eslintrc.many.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// A version of our config that enables many more rules, for use in perf testing

// @ts-check
/** @type {import('./packages/utils/src/ts-eslint/Linter.js').Linter.Config} */
module.exports = {
extends: ['plugin:@typescript-eslint/all', 'prettier'],
overrides: [
{
files: ['*.js'],
extends: ['plugin:@typescript-eslint/disable-type-checked'],
},
],
parser: '@typescript-eslint/parser',
parserOptions: {
EXPERIMENTAL_memoizeTypeCheckingAPIs:
!!process.env.MEMOIZE_TYPE_CHECKING_APIS,
project: ['./tsconfig.eslint.json'],
tsconfigRootDir: __dirname,
},
plugins: ['@typescript-eslint'],
root: true,
};
7 changes: 7 additions & 0 deletions docs/architecture/TypeScript-ESTree.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*/
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;

/**
* ***EXPERIMENTAL FLAG*** - Use this at your own risk.
*
* Whether to memoize many of TypeScript's type checking APIs.
*/
EXPERIMENTAL__memoizeTypeCheckingAPIs?: boolean;

/**
* ***EXPERIMENTAL FLAG*** - Use this at your own risk.
*
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"generate-website-dts": "nx run website:generate-website-dts",
"generate-lib": "nx run scope-manager:generate-lib",
"lint-fix": "yarn lint --fix",
"lint-many": "npx eslint --config .eslintrc.many.js --ignore-path .eslintignore.all packages/",
"lint-markdown-fix": "yarn lint-markdown --fix",
"lint-markdown": "markdownlint \"**/*.md\" --config=.markdownlint.json --ignore-path=.markdownlintignore",
"lint": "nx run-many --target=lint --parallel",
Expand Down Expand Up @@ -82,8 +83,9 @@
"cross-fetch": "^3.1.5",
"cspell": "^6.0.0",
"downlevel-dts": ">=0.11.0",
"eslint-plugin-deprecation": "^1.3.3",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-deprecation": "^1.3.3",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-eslint-plugin": "^5.0.8",
"eslint-plugin-import": "^2.27.5",
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ interface ParserOptions {
debugLevel?: DebugLevel;
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;
errorOnUnknownASTType?: boolean;
EXPERIMENTAL_memoizeTypeCheckingAPIs?: boolean;
EXPERIMENTAL_useSourceOfProjectReferenceRedirect?: boolean; // purposely undocumented for now
extraFileExtensions?: string[];
filePath?: string;
Expand Down
35 changes: 35 additions & 0 deletions packages/typescript-estree/src/createParserServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,40 @@ import type * as ts from 'typescript';
import type { ASTMaps } from './convert';
import type { ParserServices } from './parser-options';

type MethodKeyOf<Container> = keyof {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[Key in keyof Container]: Container[Key] extends (parameter: any) => unknown
? Key
: never;
};

function memoizeMethods<Container, Keys extends MethodKeyOf<Container>[]>(
container: Container,
keys: Keys,
): void {
for (const key of keys) {
const originalMethod = (
container[key] as (parameter: object) => unknown
).bind(container);
const cache = new WeakMap<object, unknown>();

container[key] = ((parameter: object): unknown => {
const cached = cache.get(parameter);
if (cached) {
return cached;
}

const created = originalMethod(parameter);
cache.set(parameter, created);
return created;
}) as Container[typeof key];
}
}

export function createParserServices(
astMaps: ASTMaps,
program: ts.Program | null,
memoize: boolean,
): ParserServices {
if (!program) {
// we always return the node maps because
Expand All @@ -16,6 +47,10 @@ export function createParserServices(

const checker = program.getTypeChecker();

if (memoize) {
memoizeMethods(checker, ['getSymbolAtLocation', 'getTypeAtLocation']);
}

return {
program,
...astMaps,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export function createParseSettings(
: new Set(),
errorOnTypeScriptSyntacticAndSemanticIssues: false,
errorOnUnknownASTType: options.errorOnUnknownASTType === true,
EXPERIMENTAL_memoizeTypeCheckingAPIs:
options.EXPERIMENTAL_memoizeTypeCheckingAPIs === true,
EXPERIMENTAL_useSourceOfProjectReferenceRedirect:
options.EXPERIMENTAL_useSourceOfProjectReferenceRedirect === true,
extraFileExtensions:
Expand Down
5 changes: 5 additions & 0 deletions packages/typescript-estree/src/parseSettings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export interface MutableParseSettings {
*/
errorOnUnknownASTType: boolean;

/**
* Whether to memoize many of TypeScript's type checking APIs.
*/
EXPERIMENTAL_memoizeTypeCheckingAPIs: boolean;

/**
* Whether TS should use the source files for referenced projects instead of the compiled .d.ts files.
*
Expand Down
7 changes: 7 additions & 0 deletions packages/typescript-estree/src/parser-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ interface ParseAndGenerateServicesOptions extends ParseOptions {
*/
errorOnTypeScriptSyntacticAndSemanticIssues?: boolean;

/**
* ***EXPERIMENTAL FLAG*** - Use this at your own risk.
*
* Whether to memoize many of TypeScript's type checking APIs.
*/
EXPERIMENTAL_memoizeTypeCheckingAPIs?: boolean;

/**
* ***EXPERIMENTAL FLAG*** - Use this at your own risk.
*
Expand Down
6 changes: 5 additions & 1 deletion packages/typescript-estree/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,11 @@ function parseAndGenerateServices<T extends TSESTreeOptions = TSESTreeOptions>(
*/
return {
ast: estree as AST<T>,
services: createParserServices(astMaps, program),
services: createParserServices(
astMaps,
program,
parseSettings.EXPERIMENTAL_memoizeTypeCheckingAPIs,
),
};
}

Expand Down
1 change: 1 addition & 0 deletions packages/website/src/components/linter/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const parseSettings: ParseSettings = {
DEPRECATED__createDefaultProgram: false,
errorOnTypeScriptSyntacticAndSemanticIssues: false,
errorOnUnknownASTType: false,
EXPERIMENTAL_memoizeTypeCheckingAPIs: false,
EXPERIMENTAL_useSourceOfProjectReferenceRedirect: false,
extraFileExtensions: [],
filePath: '',
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6855,6 +6855,11 @@ escape-string-regexp@^4.0.0:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz#14ba83a5d373e3d311e5afca29cf5bfad965bf34"
integrity sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==

eslint-config-prettier@^8.8.0:
version "8.8.0"
resolved "https://registry.yarnpkg.com/eslint-config-prettier/-/eslint-config-prettier-8.8.0.tgz#bfda738d412adc917fd7b038857110efe98c9348"
integrity sha512-wLbQiFre3tdGgpDv67NQKnJuTlcUVYHas3k+DZCc2U2BadthoEY4B7hLPvAxaqdyOGCzuLfii2fqGph10va7oA==

eslint-import-resolver-node@^0.3.7:
version "0.3.7"
resolved "https://registry.yarnpkg.com/eslint-import-resolver-node/-/eslint-import-resolver-node-0.3.7.tgz#83b375187d412324a1963d84fa664377a23eb4d7"
Expand Down