Skip to content

Commit c497b48

Browse files
Add undefined to Symbol.valueDeclaration (#43033)
* About halfway through the checker I'm going to merge with master to avoid clashing with the declaration fix. * Add undefined to Symbol.valueDeclaration Also add undefined to a number of utility functions that have always accepted it, but never added it to their type. * Fix lint from code review Co-authored-by: Daniel Rosenwasser <[email protected]> * remove obsoleted fix from inferFromUsage Co-authored-by: Daniel Rosenwasser <[email protected]>
1 parent 3d1c6e8 commit c497b48

18 files changed

+98
-72
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3187,7 +3187,7 @@ namespace ts {
31873187
undefined;
31883188
init = init && getRightMostAssignedExpression(init);
31893189
if (init) {
3190-
const isPrototypeAssignment = isPrototypeAccess(isVariableDeclaration(node) ? node.name : isBinaryExpression(node) ? node.left : node);
3190+
const isPrototypeAssignment = isPrototypeAccess(isVariableDeclaration(node!) ? node.name : isBinaryExpression(node!) ? node.left : node!);
31913191
return !!getExpandoInitializer(isBinaryExpression(init) && (init.operatorToken.kind === SyntaxKind.BarBarToken || init.operatorToken.kind === SyntaxKind.QuestionQuestionToken) ? init.right : init, isPrototypeAssignment);
31923192
}
31933193
return false;

src/compiler/checker.ts

Lines changed: 59 additions & 40 deletions
Large diffs are not rendered by default.

src/compiler/factory/nodeFactory.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5507,7 +5507,7 @@ namespace ts {
55075507
: reduceLeft(expressions, factory.createComma)!;
55085508
}
55095509

5510-
function getName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags: EmitFlags = 0) {
5510+
function getName(node: Declaration | undefined, allowComments?: boolean, allowSourceMaps?: boolean, emitFlags: EmitFlags = 0) {
55115511
const nodeName = getNameOfDeclaration(node);
55125512
if (nodeName && isIdentifier(nodeName) && !isGeneratedIdentifier(nodeName)) {
55135513
// TODO(rbuckton): Does this need to be parented?
@@ -5571,7 +5571,7 @@ namespace ts {
55715571
* @param allowComments A value indicating whether comments may be emitted for the name.
55725572
* @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
55735573
*/
5574-
function getDeclarationName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean) {
5574+
function getDeclarationName(node: Declaration | undefined, allowComments?: boolean, allowSourceMaps?: boolean) {
55755575
return getName(node, allowComments, allowSourceMaps);
55765576
}
55775577

src/compiler/moduleSpecifiers.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ namespace ts.moduleSpecifiers {
103103

104104
const info = getInfo(importingSourceFile.path, host);
105105
const moduleSourceFile = getSourceFileOfNode(moduleSymbol.valueDeclaration || getNonAugmentationDeclaration(moduleSymbol));
106+
if (!moduleSourceFile) {
107+
return [];
108+
}
106109
const modulePaths = getAllModulePaths(importingSourceFile.path, moduleSourceFile.originalFileName, host);
107110
const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile);
108111

src/compiler/transformers/declarations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1202,7 +1202,7 @@ namespace ts {
12021202
fakespace.symbol = props[0].parent!;
12031203
const exportMappings: [Identifier, string][] = [];
12041204
let declarations: (VariableStatement | ExportDeclaration)[] = mapDefined(props, p => {
1205-
if (!isPropertyAccessExpression(p.valueDeclaration)) {
1205+
if (!p.valueDeclaration || !isPropertyAccessExpression(p.valueDeclaration)) {
12061206
return undefined; // TODO GH#33569: Handle element access expressions that created late bound names (rather than silently omitting them)
12071207
}
12081208
getSymbolAccessibilityDiagnostic = createGetSymbolAccessibilityDiagnosticForNode(p.valueDeclaration);

src/compiler/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4046,7 +4046,7 @@ namespace ts {
40464046
* The function returns the value (local variable) symbol of an identifier in the short-hand property assignment.
40474047
* This is necessary as an identifier in short-hand property assignment can contains two meaning: property name and property value.
40484048
*/
4049-
getShorthandAssignmentValueSymbol(location: Node): Symbol | undefined;
4049+
getShorthandAssignmentValueSymbol(location: Node | undefined): Symbol | undefined;
40504050

40514051
getExportSpecifierLocalTargetSymbol(location: ExportSpecifier | Identifier): Symbol | undefined;
40524052
/**
@@ -4715,7 +4715,7 @@ namespace ts {
47154715
flags: SymbolFlags; // Symbol flags
47164716
escapedName: __String; // Name of symbol
47174717
declarations?: Declaration[]; // Declarations associated with this symbol
4718-
valueDeclaration: Declaration; // First value declaration of the symbol
4718+
valueDeclaration?: Declaration; // First value declaration of the symbol
47194719
members?: SymbolTable; // Class, interface or object literal instance members
47204720
exports?: SymbolTable; // Module exports
47214721
globalExports?: SymbolTable; // Conditional global UMD exports
@@ -7394,7 +7394,7 @@ namespace ts {
73947394
* @param allowComments A value indicating whether comments may be emitted for the name.
73957395
* @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
73967396
*/
7397-
/* @internal */ getDeclarationName(node: Declaration, allowComments?: boolean, allowSourceMaps?: boolean): Identifier;
7397+
/* @internal */ getDeclarationName(node: Declaration | undefined, allowComments?: boolean, allowSourceMaps?: boolean): Identifier;
73987398
/**
73997399
* Gets a namespace-qualified name for use in expressions.
74007400
*

src/compiler/utilities.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,9 @@ namespace ts {
734734
return isShorthandAmbientModule(moduleSymbol.valueDeclaration);
735735
}
736736

737-
function isShorthandAmbientModule(node: Node): boolean {
737+
function isShorthandAmbientModule(node: Node | undefined): boolean {
738738
// The only kind of module that can be missing a body is a shorthand ambient module.
739-
return node && node.kind === SyntaxKind.ModuleDeclaration && (!(<ModuleDeclaration>node).body);
739+
return !!node && node.kind === SyntaxKind.ModuleDeclaration && (!(<ModuleDeclaration>node).body);
740740
}
741741

742742
export function isBlockScopedContainerTopLevel(node: Node): boolean {

src/compiler/utilitiesPublic.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ namespace ts {
614614
return (declaration as NamedDeclaration).name;
615615
}
616616

617-
export function getNameOfDeclaration(declaration: Declaration | Expression): DeclarationName | undefined {
617+
export function getNameOfDeclaration(declaration: Declaration | Expression | undefined): DeclarationName | undefined {
618618
if (declaration === undefined) return undefined;
619619
return getNonAssignedNameOfDeclaration(declaration) ||
620620
(isFunctionExpression(declaration) || isClassExpression(declaration) ? getAssignedName(declaration) : undefined);
@@ -1208,8 +1208,8 @@ namespace ts {
12081208

12091209
// Functions
12101210

1211-
export function isFunctionLike(node: Node): node is SignatureDeclaration {
1212-
return node && isFunctionLikeKind(node.kind);
1211+
export function isFunctionLike(node: Node | undefined): node is SignatureDeclaration {
1212+
return !!node && isFunctionLikeKind(node.kind);
12131213
}
12141214

12151215
/* @internal */

src/services/codefixes/convertConstToLet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace ts.codefix {
1818
const token = getTokenAtPosition(sourceFile, pos);
1919
const checker = program.getTypeChecker();
2020
const symbol = checker.getSymbolAtLocation(token);
21-
if (symbol) {
21+
if (symbol?.valueDeclaration) {
2222
return symbol.valueDeclaration.parent.parent as VariableStatement;
2323
}
2424
}

src/services/codefixes/convertFunctionToEs6Class.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace ts.codefix {
1616

1717
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, position: number, checker: TypeChecker, preferences: UserPreferences, compilerOptions: CompilerOptions): void {
1818
const ctorSymbol = checker.getSymbolAtLocation(getTokenAtPosition(sourceFile, position))!;
19-
if (!ctorSymbol || !(ctorSymbol.flags & (SymbolFlags.Function | SymbolFlags.Variable))) {
19+
if (!ctorSymbol || !ctorSymbol.valueDeclaration || !(ctorSymbol.flags & (SymbolFlags.Function | SymbolFlags.Variable))) {
2020
// Bad input
2121
return undefined;
2222
}
@@ -46,7 +46,7 @@ namespace ts.codefix {
4646
// all instance members are stored in the "member" array of symbol
4747
if (symbol.members) {
4848
symbol.members.forEach((member, key) => {
49-
if (key === "constructor") {
49+
if (key === "constructor" && member.valueDeclaration) {
5050
// fn.prototype.constructor = fn
5151
changes.delete(sourceFile, member.valueDeclaration.parent);
5252
return;

0 commit comments

Comments
 (0)