Skip to content

Commit

Permalink
Fixed crash in auto import suggestions for default of exported UMD …
Browse files Browse the repository at this point in the history
…objects (#60313)
  • Loading branch information
Andarist authored Oct 23, 2024
1 parent 1679f44 commit 437d7f7
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ import {
isExternalModule,
isExternalModuleImportEqualsDeclaration,
isExternalModuleReference,
isExternalModuleSymbol,
isFileLevelUniqueName,
isForInStatement,
isForOfStatement,
Expand Down Expand Up @@ -4027,7 +4028,13 @@ export function getDefaultLikeExportNameFromDeclaration(symbol: Symbol): string
return tryCast(d.propertyName, isIdentifier)?.text;
}
// GH#52694
return tryCast(getNameOfDeclaration(d), isIdentifier)?.text;
const name = tryCast(getNameOfDeclaration(d), isIdentifier)?.text;
if (name) {
return name;
}
if (symbol.parent && !isExternalModuleSymbol(symbol.parent)) {
return symbol.parent.getName();
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

verify.completions({
marker: "1",
// some kind of a check should be added here
includes: [{ name: "$" }],
preferences: {
includeCompletionsForModuleExports: true,
includeCompletionsForModuleExports: true,
}
});
51 changes: 51 additions & 0 deletions tests/cases/fourslash/completionsImportDefaultExportCrash2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/// <reference path="fourslash.ts" />

// @module: nodenext
// @allowJs: true

// @Filename: /node_modules/dom7/index.d.ts
//// export interface Dom7Array {
//// length: number;
//// prop(propName: string): any;
//// }
////
//// export interface Dom7 {
//// (): Dom7Array;
//// fn: any;
//// }
////
//// declare const Dom7: Dom7;
////
//// export {
//// Dom7 as $,
//// };

// @Filename: /dom7.js
//// import * as methods from 'dom7';
//// Object.keys(methods).forEach((methodName) => {
//// if (methodName === '$') return;
//// methods.$.fn[methodName] = methods[methodName];
//// });
////
//// export default methods.$;

// @Filename: /swipe-back.js
//// /*1*/

verify.completions({
marker: "1",
includes: [{
name: "$",
hasAction: true,
source: 'dom7',
sortText: completion.SortText.AutoImportSuggestions,
}, {
name: "Dom7",
hasAction: true,
source: './dom7',
sortText: completion.SortText.AutoImportSuggestions,
}],
preferences: {
includeCompletionsForModuleExports: true,
}
});
49 changes: 49 additions & 0 deletions tests/cases/fourslash/completionsImport_umdDefaultNoCrash1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference path="fourslash.ts" />

// @moduleResolution: node
// @allowJs: true
// @checkJs: true

// @Filename: /node_modules/dottie/package.json
//// {
//// "name": "dottie",
//// "main": "dottie.js"
//// }

// @Filename: /node_modules/dottie/dottie.js
//// (function (undefined) {
//// var root = this;
////
//// var Dottie = function () {};
////
//// Dottie["default"] = function (object, path, value) {};
////
//// if (typeof module !== "undefined" && module.exports) {
//// exports = module.exports = Dottie;
//// } else {
//// root["Dottie"] = Dottie;
//// root["Dot"] = Dottie;
////
//// if (typeof define === "function") {
//// define([], function () {
//// return Dottie;
//// });
//// }
//// }
//// })();

// @Filename: /src/index.js
//// /**/

verify.completions({
marker: "",
includes: [
{
name: "Dottie",
hasAction: true,
source: "/node_modules/dottie/dottie",
sortText: completion.SortText.AutoImportSuggestions,
},
],
preferences: { includeCompletionsForModuleExports: true },
});
43 changes: 43 additions & 0 deletions tests/cases/fourslash/completionsImport_umdDefaultNoCrash2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// <reference path="fourslash.ts" />

// @moduleResolution: node
// @allowJs: true
// @checkJs: true

// @Filename: /node_modules/dottie/package.json
//// {
//// "name": "dottie",
//// "main": "dottie.js"
//// }

// @Filename: /node_modules/dottie/dottie.js
//// (function (undefined) {
//// var root = this;
////
//// var Dottie = function () {};
////
//// Dottie["default"] = function (object, path, value) {};
////
//// if (typeof module !== "undefined" && module.exports) {
//// exports = module.exports = Dottie;
//// } else {
//// root["Dottie"] = Dottie;
//// root["Dot"] = Dottie;
////
//// if (typeof define === "function") {
//// define([], function () {
//// return Dottie;
//// });
//// }
//// }
//// })();

// @Filename: /src/index.js
//// import Dottie from 'dottie';
//// /**/

verify.completions({
marker: "",
includes: [{ name: "Dottie" }],
preferences: { includeCompletionsForModuleExports: true },
});

0 comments on commit 437d7f7

Please sign in to comment.