Skip to content

Commit 437d7f7

Browse files
authored
Fixed crash in auto import suggestions for default of exported UMD objects (#60313)
1 parent 1679f44 commit 437d7f7

File tree

5 files changed

+153
-3
lines changed

5 files changed

+153
-3
lines changed

src/services/utilities.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import {
169169
isExternalModule,
170170
isExternalModuleImportEqualsDeclaration,
171171
isExternalModuleReference,
172+
isExternalModuleSymbol,
172173
isFileLevelUniqueName,
173174
isForInStatement,
174175
isForOfStatement,
@@ -4027,7 +4028,13 @@ export function getDefaultLikeExportNameFromDeclaration(symbol: Symbol): string
40274028
return tryCast(d.propertyName, isIdentifier)?.text;
40284029
}
40294030
// GH#52694
4030-
return tryCast(getNameOfDeclaration(d), isIdentifier)?.text;
4031+
const name = tryCast(getNameOfDeclaration(d), isIdentifier)?.text;
4032+
if (name) {
4033+
return name;
4034+
}
4035+
if (symbol.parent && !isExternalModuleSymbol(symbol.parent)) {
4036+
return symbol.parent.getName();
4037+
}
40314038
});
40324039
}
40334040

tests/cases/fourslash/completionsImportDefaultExportCrash.ts renamed to tests/cases/fourslash/completionsImportDefaultExportCrash1.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535

3636
verify.completions({
3737
marker: "1",
38-
// some kind of a check should be added here
38+
includes: [{ name: "$" }],
3939
preferences: {
40-
includeCompletionsForModuleExports: true,
40+
includeCompletionsForModuleExports: true,
4141
}
4242
});
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: nodenext
4+
// @allowJs: true
5+
6+
// @Filename: /node_modules/dom7/index.d.ts
7+
//// export interface Dom7Array {
8+
//// length: number;
9+
//// prop(propName: string): any;
10+
//// }
11+
////
12+
//// export interface Dom7 {
13+
//// (): Dom7Array;
14+
//// fn: any;
15+
//// }
16+
////
17+
//// declare const Dom7: Dom7;
18+
////
19+
//// export {
20+
//// Dom7 as $,
21+
//// };
22+
23+
// @Filename: /dom7.js
24+
//// import * as methods from 'dom7';
25+
//// Object.keys(methods).forEach((methodName) => {
26+
//// if (methodName === '$') return;
27+
//// methods.$.fn[methodName] = methods[methodName];
28+
//// });
29+
////
30+
//// export default methods.$;
31+
32+
// @Filename: /swipe-back.js
33+
//// /*1*/
34+
35+
verify.completions({
36+
marker: "1",
37+
includes: [{
38+
name: "$",
39+
hasAction: true,
40+
source: 'dom7',
41+
sortText: completion.SortText.AutoImportSuggestions,
42+
}, {
43+
name: "Dom7",
44+
hasAction: true,
45+
source: './dom7',
46+
sortText: completion.SortText.AutoImportSuggestions,
47+
}],
48+
preferences: {
49+
includeCompletionsForModuleExports: true,
50+
}
51+
});
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
// @allowJs: true
5+
// @checkJs: true
6+
7+
// @Filename: /node_modules/dottie/package.json
8+
//// {
9+
//// "name": "dottie",
10+
//// "main": "dottie.js"
11+
//// }
12+
13+
// @Filename: /node_modules/dottie/dottie.js
14+
//// (function (undefined) {
15+
//// var root = this;
16+
////
17+
//// var Dottie = function () {};
18+
////
19+
//// Dottie["default"] = function (object, path, value) {};
20+
////
21+
//// if (typeof module !== "undefined" && module.exports) {
22+
//// exports = module.exports = Dottie;
23+
//// } else {
24+
//// root["Dottie"] = Dottie;
25+
//// root["Dot"] = Dottie;
26+
////
27+
//// if (typeof define === "function") {
28+
//// define([], function () {
29+
//// return Dottie;
30+
//// });
31+
//// }
32+
//// }
33+
//// })();
34+
35+
// @Filename: /src/index.js
36+
//// /**/
37+
38+
verify.completions({
39+
marker: "",
40+
includes: [
41+
{
42+
name: "Dottie",
43+
hasAction: true,
44+
source: "/node_modules/dottie/dottie",
45+
sortText: completion.SortText.AutoImportSuggestions,
46+
},
47+
],
48+
preferences: { includeCompletionsForModuleExports: true },
49+
});
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @moduleResolution: node
4+
// @allowJs: true
5+
// @checkJs: true
6+
7+
// @Filename: /node_modules/dottie/package.json
8+
//// {
9+
//// "name": "dottie",
10+
//// "main": "dottie.js"
11+
//// }
12+
13+
// @Filename: /node_modules/dottie/dottie.js
14+
//// (function (undefined) {
15+
//// var root = this;
16+
////
17+
//// var Dottie = function () {};
18+
////
19+
//// Dottie["default"] = function (object, path, value) {};
20+
////
21+
//// if (typeof module !== "undefined" && module.exports) {
22+
//// exports = module.exports = Dottie;
23+
//// } else {
24+
//// root["Dottie"] = Dottie;
25+
//// root["Dot"] = Dottie;
26+
////
27+
//// if (typeof define === "function") {
28+
//// define([], function () {
29+
//// return Dottie;
30+
//// });
31+
//// }
32+
//// }
33+
//// })();
34+
35+
// @Filename: /src/index.js
36+
//// import Dottie from 'dottie';
37+
//// /**/
38+
39+
verify.completions({
40+
marker: "",
41+
includes: [{ name: "Dottie" }],
42+
preferences: { includeCompletionsForModuleExports: true },
43+
});

0 commit comments

Comments
 (0)