Skip to content

Commit 22f37cd

Browse files
authored
Fix ordering of module specifiers based on package.json presence (microsoft#46437)
* Add failing test * Fix ordering of module specifiers based on package.json presence
1 parent f2e5947 commit 22f37cd

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/services/codefixes/importFixes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,15 @@ namespace ts.codefix {
663663
}
664664
const { allowsImportingSpecifier } = createPackageJsonImportFilter(sourceFile, preferences, host);
665665
return fixes.reduce((best, fix) =>
666+
// Takes true branch of conditional if `fix` is better than `best`
666667
compareModuleSpecifiers(fix, best, sourceFile, program, allowsImportingSpecifier) === Comparison.LessThan ? fix : best
667668
);
668669
}
669670

671+
/** @returns `Comparison.LessThan` if `a` is better than `b`. */
670672
function compareModuleSpecifiers(a: ImportFix, b: ImportFix, importingFile: SourceFile, program: Program, allowsImportingSpecifier: (specifier: string) => boolean): Comparison {
671673
if (a.kind !== ImportFixKind.UseNamespace && b.kind !== ImportFixKind.UseNamespace) {
672-
return compareBooleans(allowsImportingSpecifier(a.moduleSpecifier), allowsImportingSpecifier(b.moduleSpecifier))
674+
return compareBooleans(allowsImportingSpecifier(b.moduleSpecifier), allowsImportingSpecifier(a.moduleSpecifier))
673675
|| compareNodeCoreModuleSpecifiers(a.moduleSpecifier, b.moduleSpecifier, importingFile, program)
674676
|| compareNumberOfDirectorySeparators(a.moduleSpecifier, b.moduleSpecifier);
675677
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @module: esnext
4+
// @moduleResolution: node
5+
6+
// @Filename: /node_modules/vue/package.json
7+
//// {
8+
//// "name": "vue",
9+
//// "types": "dist/vue.d.ts"
10+
//// }
11+
12+
// @Filename: /node_modules/vue/dist/vue.d.ts
13+
//// export * from "@vue/runtime-dom"
14+
15+
// @Filename: /node_modules/@vue/runtime-dom/package.json
16+
//// {
17+
//// "name": "@vue/runtime-dom",
18+
//// "types": "dist/runtime-dom.d.ts"
19+
//// }
20+
21+
// @Filename: /node_modules/@vue/runtime-dom/dist/runtime-dom.d.ts
22+
//// export * from "@vue/runtime-core";
23+
//// export {}
24+
//// declare module '@vue/reactivity' {
25+
//// export interface RefUnwrapBailTypes {
26+
//// runtimeDOMBailTypes: any
27+
//// }
28+
//// }
29+
30+
// @Filename: /node_modules/@vue/runtime-core/package.json
31+
//// {
32+
//// "name": "@vue/runtime-core",
33+
//// "types": "dist/runtime-core.d.ts"
34+
//// }
35+
36+
// @Filename: /node_modules/@vue/runtime-core/dist/runtime-core.d.ts
37+
//// import { ref } from '@vue/reactivity';
38+
//// export { ref };
39+
//// declare module '@vue/reactivity' {
40+
//// export interface RefUnwrapBailTypes {
41+
//// runtimeCoreBailTypes: any
42+
//// }
43+
//// }
44+
45+
// @Filename: /node_modules/@vue/reactivity/package.json
46+
//// {
47+
//// "name": "@vue/reactivity",
48+
//// "types": "dist/reactivity.d.ts"
49+
//// }
50+
51+
// @Filename: /node_modules/@vue/reactivity/dist/reactivity.d.ts
52+
//// export declare function ref<T = any>(): T;
53+
54+
// @Filename: /package.json
55+
//// {
56+
//// "dependencies": {
57+
//// "vue": "*"
58+
//// }
59+
//// }
60+
61+
// @Filename: /index.ts
62+
//// import {} from "vue";
63+
//// ref/**/
64+
65+
verify.completions({
66+
marker: "",
67+
includes: [{
68+
name: "ref",
69+
source: "vue",
70+
sourceDisplay: "vue",
71+
hasAction: true,
72+
sortText: completion.SortText.AutoImportSuggestions,
73+
}],
74+
preferences: {
75+
includeCompletionsForModuleExports: true,
76+
allowIncompleteCompletions: true,
77+
},
78+
});
79+
80+
verify.applyCodeActionFromCompletion("", {
81+
name: "ref",
82+
source: "vue",
83+
description: `Add 'ref' to existing import declaration from "vue"`,
84+
data: {
85+
exportName: "ref",
86+
fileName: "/node_modules/vue/dist/vue.d.ts",
87+
},
88+
preferences: {
89+
includeCompletionsForModuleExports: true,
90+
allowIncompleteCompletions: true,
91+
},
92+
newFileContent: `import { ref } from "vue";\nref`
93+
});

0 commit comments

Comments
 (0)