Perf research: use scope analysis to circumvent type information for some cases #6217
bradzacher
started this conversation in
Technical Discussions
Replies: 1 comment · 1 reply
-
Hi, @bradzacher What do you think about providing fast predicates type checking utilities? export function isAny(node: TSESTree.Node, service: ParserServices): boolean {
const scopeAnalysisType = getScopeAnalysisType(node);
if( [
SCOPE_ANALYSIS_TYPE.Literal,
SCOPE_ANALYSIS_TYPE.FunctionExpression,
SCOPE_ANALYSIS_TYPE.ArrayExpression,
//...
].includes(scopeAnalysisType)
) {
return false;
}
const type = services.getTypeAtLocation(node);
return isTypeFlagSet(type, ts.TypeFlags.Any);
}
export function isNullable(node: TSESTree.Node): boolean {
if( [
SCOPE_ANALYSIS_TYPE.FunctionExpression,
SCOPE_ANALYSIS_TYPE.ArrayExpression,
//...
].includes(scopeAnalysisType)
) {
return false;
}
const type = ts.getType(node);
return isTypeFlagSet(type, ts.TypeFlags.Undefined | ts.TypeFlags.Null);
//...
}
// some-rule.ts
if (isAny(node)) {
context.report({ ... });
} else {
const type = services.getTypeAtLocation(node);
// ...
} In this way,
export function isPromise(node: TSESTree.Node): boolean {
//
const functionDef = /* .. */;
if (functionDef.async) return true;
const type = ts.getType(node);
//...
} |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Type information is computed lazily by TS. What this means is that the more type information a rule accesses, the slower that rule is.
Right now all lint rules directly use TS's APIs to ask TS to compute types before operating on those types and asking questions of those types.
I wonder if we can improve the performance here by using the scope analysis information that's already calculated to get some basic types from the AST.
My hypothesis is that there are a lot of questions we're asking that could be answered solely from the AST.
For example:
For
restrict-plus-operands
the rule will inspect both sides of the binary expression, ask for types, and then run some predicates over the types to determine if the operation is safe or not.In this example, however, we shouldn't ever need to ask TS anything - we could solve this entirely using the AST + scope analysis.
For example instead of doing (pseudocode):
we could potentially circumvent some work by doing something like:
It's a lot more code, for sure, but it would save us from having to dip into the types for a number of cases.
Some of the more complex cases (like looking for promises, where we need to deeply inspect the object properties) could be a lot faster if we can circumvent ever asking for the type because we know it's definitely a number.
It would probably be a bit awkward to use any utilities that are built here, but it could work well in conjunction with #6013 if we had our own type API surface to automate some of the short-circuiting for people.
Beta Was this translation helpful? Give feedback.
All reactions