Skip to content

Commit aa67b16

Browse files
authored
Add undefined to Symbol.declarations' type (microsoft#42975)
* Add undefined to Symbol.declarations' type Symbol.declarations now has type `Declaration[] | undefined`. I made a mistake somewhere in the checker related to JS checking, so there are quite a few test failures right now. * undo clever change to getDeclaringConstructor * Address PR comments 1. More early-returns. 2. More line breaks.
1 parent 2a49cf7 commit aa67b16

25 files changed

+364
-309
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3266,7 +3266,7 @@ namespace ts {
32663266
if (node.name) {
32673267
setParent(node.name, node);
32683268
}
3269-
file.bindDiagnostics.push(createDiagnosticForNode(symbolExport.declarations[0], Diagnostics.Duplicate_identifier_0, symbolName(prototypeSymbol)));
3269+
file.bindDiagnostics.push(createDiagnosticForNode(symbolExport.declarations![0], Diagnostics.Duplicate_identifier_0, symbolName(prototypeSymbol)));
32703270
}
32713271
symbol.exports!.set(prototypeSymbol.escapedName, prototypeSymbol);
32723272
prototypeSymbol.parent = symbol;

src/compiler/builderState.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,17 @@ namespace ts {
166166

167167
// From ambient modules
168168
for (const ambientModule of program.getTypeChecker().getAmbientModules()) {
169-
if (ambientModule.declarations.length > 1) {
169+
if (ambientModule.declarations && ambientModule.declarations.length > 1) {
170170
addReferenceFromAmbientModule(ambientModule);
171171
}
172172
}
173173

174174
return referencedFiles;
175175

176176
function addReferenceFromAmbientModule(symbol: Symbol) {
177+
if (!symbol.declarations) {
178+
return;
179+
}
177180
// Add any file other than our own as reference
178181
for (const declaration of symbol.declarations) {
179182
const declarationSourceFile = getSourceFileOfNode(declaration);

src/compiler/checker.ts

Lines changed: 293 additions & 250 deletions
Large diffs are not rendered by default.

src/compiler/core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ namespace ts {
292292
return -1;
293293
}
294294

295-
export function countWhere<T>(array: readonly T[], predicate: (x: T, i: number) => boolean): number {
295+
export function countWhere<T>(array: readonly T[] | undefined, predicate: (x: T, i: number) => boolean): number {
296296
let count = 0;
297297
if (array) {
298298
for (let i = 0; i < array.length; i++) {

src/compiler/moduleSpecifiers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ namespace ts.moduleSpecifiers {
382382
}
383383

384384
function tryGetModuleNameFromAmbientModule(moduleSymbol: Symbol, checker: TypeChecker): string | undefined {
385-
const decl = find(moduleSymbol.declarations,
385+
const decl = moduleSymbol.declarations?.find(
386386
d => isNonGlobalAmbientModule(d) && (!isExternalModuleAugmentation(d) || !isExternalModuleNameRelative(getTextOfIdentifierOrLiteral(d.name)))
387387
) as (ModuleDeclaration & { name: StringLiteral }) | undefined;
388388
if (decl) {

src/compiler/transformers/declarations.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,15 @@ namespace ts {
206206
}
207207

208208
function reportNonlocalAugmentation(containingFile: SourceFile, parentSymbol: Symbol, symbol: Symbol) {
209-
const primaryDeclaration = find(parentSymbol.declarations, d => getSourceFileOfNode(d) === containingFile)!;
209+
const primaryDeclaration = parentSymbol.declarations?.find(d => getSourceFileOfNode(d) === containingFile)!;
210210
const augmentingDeclarations = filter(symbol.declarations, d => getSourceFileOfNode(d) !== containingFile);
211-
for (const augmentations of augmentingDeclarations) {
212-
context.addDiagnostic(addRelatedInfo(
213-
createDiagnosticForNode(augmentations, Diagnostics.Declaration_augments_declaration_in_another_file_This_cannot_be_serialized),
214-
createDiagnosticForNode(primaryDeclaration, Diagnostics.This_is_the_declaration_being_augmented_Consider_moving_the_augmenting_declaration_into_the_same_file)
215-
));
211+
if (augmentingDeclarations) {
212+
for (const augmentations of augmentingDeclarations) {
213+
context.addDiagnostic(addRelatedInfo(
214+
createDiagnosticForNode(augmentations, Diagnostics.Declaration_augments_declaration_in_another_file_This_cannot_be_serialized),
215+
createDiagnosticForNode(primaryDeclaration, Diagnostics.This_is_the_declaration_being_augmented_Consider_moving_the_augmenting_declaration_into_the_same_file)
216+
));
217+
}
216218
}
217219
}
218220

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4714,7 +4714,7 @@ namespace ts {
47144714
export interface Symbol {
47154715
flags: SymbolFlags; // Symbol flags
47164716
escapedName: __String; // Name of symbol
4717-
declarations: Declaration[]; // Declarations associated with this symbol
4717+
declarations?: Declaration[]; // Declarations associated with this symbol
47184718
valueDeclaration: Declaration; // First value declaration of the symbol
47194719
members?: SymbolTable; // Class, interface or object literal instance members
47204720
exports?: SymbolTable; // Module exports

src/compiler/utilities.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ namespace ts {
767767
}
768768

769769
export function getNonAugmentationDeclaration(symbol: Symbol) {
770-
return find(symbol.declarations, d => !isExternalModuleAugmentation(d) && !(isModuleDeclaration(d) && isGlobalScopeAugmentation(d)));
770+
return symbol.declarations?.find(d => !isExternalModuleAugmentation(d) && !(isModuleDeclaration(d) && isGlobalScopeAugmentation(d)));
771771
}
772772

773773
export function isEffectiveExternalModule(node: SourceFile, compilerOptions: CompilerOptions) {
@@ -4888,15 +4888,15 @@ namespace ts {
48884888
}
48894889

48904890
export function getLocalSymbolForExportDefault(symbol: Symbol) {
4891-
if (!isExportDefaultSymbol(symbol)) return undefined;
4891+
if (!isExportDefaultSymbol(symbol) || !symbol.declarations) return undefined;
48924892
for (const decl of symbol.declarations) {
48934893
if (decl.localSymbol) return decl.localSymbol;
48944894
}
48954895
return undefined;
48964896
}
48974897

48984898
function isExportDefaultSymbol(symbol: Symbol): boolean {
4899-
return symbol && length(symbol.declarations) > 0 && hasSyntacticModifier(symbol.declarations[0], ModifierFlags.Default);
4899+
return symbol && length(symbol.declarations) > 0 && hasSyntacticModifier(symbol.declarations![0], ModifierFlags.Default);
49004900
}
49014901

49024902
/** Return ".ts", ".d.ts", or ".tsx", if that is the extension. */
@@ -5445,7 +5445,7 @@ namespace ts {
54455445
}
54465446

54475447
export function getClassLikeDeclarationOfSymbol(symbol: Symbol): ClassLikeDeclaration | undefined {
5448-
return find(symbol.declarations, isClassLike);
5448+
return symbol.declarations?.find(isClassLike);
54495449
}
54505450

54515451
export function getObjectFlags(type: Type): ObjectFlags {

src/harness/fourslashImpl.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,8 @@ namespace FourSlash {
10031003

10041004
private verifySymbol(symbol: ts.Symbol, declarationRanges: Range[]) {
10051005
const { declarations } = symbol;
1006-
if (declarations.length !== declarationRanges.length) {
1007-
this.raiseError(`Expected to get ${declarationRanges.length} declarations, got ${declarations.length}`);
1006+
if (declarations?.length !== declarationRanges.length) {
1007+
this.raiseError(`Expected to get ${declarationRanges.length} declarations, got ${declarations?.length}`);
10081008
}
10091009

10101010
ts.zipWith(declarations, declarationRanges, (decl, range) => {

src/services/callHierarchy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ namespace ts.CallHierarchy {
181181
const indices = indicesOf(symbol.declarations);
182182
const keys = map(symbol.declarations, decl => ({ file: decl.getSourceFile().fileName, pos: decl.pos }));
183183
indices.sort((a, b) => compareStringsCaseSensitive(keys[a].file, keys[b].file) || keys[a].pos - keys[b].pos);
184-
const sortedDeclarations = map(indices, i => symbol.declarations[i]);
184+
const sortedDeclarations = map(indices, i => symbol.declarations![i]);
185185
let lastDecl: CallHierarchyDeclaration | undefined;
186186
for (const decl of sortedDeclarations) {
187187
if (isValidCallHierarchyDeclaration(decl)) {

0 commit comments

Comments
 (0)