Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crash in auto import suggestions for default of exported UMD objects #60313

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this kind of matches the exact behavior of the compiler before #58460 , if needed this can be fine-tuned further

}
});
}

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 },
});