Skip to content

Commit 84c5795

Browse files
authored
Merge pull request microsoft#17536 from minestarks/fix15223
Missing import codefix: Take scoped packages (@foo/bar) into consideration
2 parents c73fdc8 + 16112c3 commit 84c5795

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

src/services/codefixes/importFixes.ts

+12-5
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ namespace ts.codefix {
174174
if (localSymbol && localSymbol.escapedName === name && checkSymbolHasMeaning(localSymbol, currentTokenMeaning)) {
175175
// check if this symbol is already used
176176
const symbolId = getUniqueSymbolId(localSymbol);
177-
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name, /*isDefault*/ true));
177+
symbolIdActionMap.addActions(symbolId, getCodeActionForImport(moduleSymbol, name, /*isNamespaceImport*/ true));
178178
}
179179
}
180180

@@ -562,8 +562,8 @@ namespace ts.codefix {
562562

563563
function getNodeModulePathParts(fullPath: string) {
564564
// If fullPath can't be valid module file within node_modules, returns undefined.
565-
// Example of expected pattern: /base/path/node_modules/[otherpackage/node_modules/]package/[subdirectory/]file.js
566-
// Returns indices: ^ ^ ^ ^
565+
// Example of expected pattern: /base/path/node_modules/[@scope/otherpackage/@otherscope/node_modules/]package/[subdirectory/]file.js
566+
// Returns indices: ^ ^ ^ ^
567567

568568
let topLevelNodeModulesIndex = 0;
569569
let topLevelPackageNameIndex = 0;
@@ -573,6 +573,7 @@ namespace ts.codefix {
573573
const enum States {
574574
BeforeNodeModules,
575575
NodeModules,
576+
Scope,
576577
PackageContent
577578
}
578579

@@ -592,8 +593,14 @@ namespace ts.codefix {
592593
}
593594
break;
594595
case States.NodeModules:
595-
packageRootIndex = partEnd;
596-
state = States.PackageContent;
596+
case States.Scope:
597+
if (state === States.NodeModules && fullPath.charAt(partStart + 1) === "@") {
598+
state = States.Scope;
599+
}
600+
else {
601+
packageRootIndex = partEnd;
602+
state = States.PackageContent;
603+
}
597604
break;
598605
case States.PackageContent:
599606
if (fullPath.indexOf("/node_modules/", partStart) === partStart) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|f1/*0*/('');|]
4+
5+
// @Filename: package.json
6+
//// { "dependencies": { "package-name": "latest" } }
7+
8+
// @Filename: node_modules/@scope/package-name/bin/lib/index.d.ts
9+
//// export function f1(text: string): string;
10+
11+
// @Filename: node_modules/@scope/package-name/bin/lib/index.js
12+
//// function f1(text) { }
13+
//// exports.f1 = f1;
14+
15+
// @Filename: node_modules/@scope/package-name/package.json
16+
//// {
17+
//// "main": "bin/lib/index.js",
18+
//// "types": "bin/lib/index.d.ts"
19+
//// }
20+
21+
verify.importFixAtPosition([
22+
`import { f1 } from "@scope/package-name";
23+
24+
f1('');`
25+
]);

0 commit comments

Comments
 (0)