Reduce polymorphism resulting from unstable Node shapes (#51682)

* Move .symbol to Declaration

* simplify some factories

* Move localSymbol to Declaration

* Ensure JSDocContainer types are properly initialized

* Move contextualType from Node to NodeLinks

* Move 'locals' and 'nextContainer' out of Node

* Move 'flowNode' out of 'Node'

* Pre-define endFlowNode/returnFlowNode

* Pre-define some SourceFile properties and a more stable cloneNode

* Don't add excess properties to type nodes in typeToTypeNode

* Refactor wrapSymbolTrackerToReportForContext to improve perf
This commit is contained in:
Ron Buckton
2022-12-13 15:11:10 -05:00
committed by GitHub
parent 7267fcaeb9
commit 6d41964fd0
62 changed files with 2730 additions and 1622 deletions

View File

@@ -6,6 +6,7 @@ import {
Block,
CallExpression,
canBeConvertedToAsync,
canHaveSymbol,
CodeFixContext,
concatenate,
createMultiMap,
@@ -867,7 +868,7 @@ function getArgBindingName(funcNode: Expression, transformer: Transformer): Synt
}
function getSymbol(node: Node): Symbol | undefined {
return node.symbol ? node.symbol : transformer.checker.getSymbolAtLocation(node);
return tryCast(node, canHaveSymbol)?.symbol ?? transformer.checker.getSymbolAtLocation(node);
}
function getOriginalNode(node: Node): Node {

View File

@@ -46,10 +46,10 @@ function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, po
const token = getTokenAtPosition(sourceFile, pos);
if (!isThis(token)) return undefined;
const fn = getThisContainer(token, /*includeArrowFunctions*/ false);
const fn = getThisContainer(token, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false);
if (!isFunctionDeclaration(fn) && !isFunctionExpression(fn)) return undefined;
if (!isSourceFile(getThisContainer(fn, /*includeArrowFunctions*/ false))) { // 'this' is defined outside, convert to arrow function
if (!isSourceFile(getThisContainer(fn, /*includeArrowFunctions*/ false, /*includeClassComputedPropertyName*/ false))) { // 'this' is defined outside, convert to arrow function
const fnKeyword = Debug.checkDefined(findChildOfKind(fn, SyntaxKind.FunctionKeyword, sourceFile));
const { name } = fn;
const body = Debug.checkDefined(fn.body); // Should be defined because the function contained a 'this' expression

View File

@@ -1,5 +1,6 @@
import {
canHaveExportModifier,
canHaveLocals,
Declaration,
Diagnostics,
ExportDeclaration,
@@ -125,7 +126,7 @@ function getInfo(sourceFile: SourceFile, pos: number, program: Program): Info |
if (moduleSourceFile === undefined || isSourceFileFromLibrary(program, moduleSourceFile)) return undefined;
const moduleSymbol = moduleSourceFile.symbol;
const locals = moduleSymbol.valueDeclaration?.locals;
const locals = tryCast(moduleSymbol.valueDeclaration, canHaveLocals)?.locals;
if (locals === undefined) return undefined;
const localSymbol = locals.get(token.escapedText);