Skip to content

Commit 9a62db2

Browse files
authored
Merge pull request #13287 from zhengbli/importFixExtendedTsconfig
For `path` ending with `index`, use the `path` pattern for the import quick fix
2 parents 3d55456 + b19a949 commit 9a62db2

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

src/harness/fourslash.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -262,23 +262,12 @@ namespace FourSlash {
262262
// Initialize the language service with all the scripts
263263
let startResolveFileRef: FourSlashFile;
264264

265+
let configFileName: string;
265266
ts.forEach(testData.files, file => {
266267
// Create map between fileName and its content for easily looking up when resolveReference flag is specified
267268
this.inputFiles[file.fileName] = file.content;
268-
269269
if (ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json") {
270-
const configJson = ts.parseConfigFileTextToJson(file.fileName, file.content);
271-
assert.isTrue(configJson.config !== undefined);
272-
273-
// Extend our existing compiler options so that we can also support tsconfig only options
274-
if (configJson.config.compilerOptions) {
275-
const baseDirectory = ts.normalizePath(ts.getDirectoryPath(file.fileName));
276-
const tsConfig = ts.convertCompilerOptionsFromJson(configJson.config.compilerOptions, baseDirectory, file.fileName);
277-
278-
if (!tsConfig.errors || !tsConfig.errors.length) {
279-
compilationOptions = ts.extend(compilationOptions, tsConfig.options);
280-
}
281-
}
270+
configFileName = file.fileName;
282271
}
283272

284273
if (!startResolveFileRef && file.fileOptions[metadataOptionNames.resolveReference] === "true") {
@@ -290,6 +279,21 @@ namespace FourSlash {
290279
}
291280
});
292281

282+
if (configFileName) {
283+
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
284+
const host = new Utils.MockParseConfigHost(baseDir, /*ignoreCase*/ false, this.inputFiles);
285+
286+
const configJsonObj = ts.parseConfigFileTextToJson(configFileName, this.inputFiles[configFileName]);
287+
assert.isTrue(configJsonObj.config !== undefined);
288+
289+
const { options, errors } = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir);
290+
291+
// Extend our existing compiler options so that we can also support tsconfig only options
292+
if (!errors || errors.length === 0) {
293+
compilationOptions = ts.extend(compilationOptions, options);
294+
}
295+
}
296+
293297

294298
if (compilationOptions.typeRoots) {
295299
compilationOptions.typeRoots = compilationOptions.typeRoots.map(p => ts.getNormalizedAbsolutePath(p, this.basePath));

src/services/codefixes/importFixes.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ namespace ts.codefix {
443443
return undefined;
444444
}
445445

446+
const relativeNameWithIndex = removeFileExtension(relativeName);
446447
relativeName = removeExtensionAndIndexPostFix(relativeName);
447448

448449
if (options.paths) {
@@ -462,7 +463,7 @@ namespace ts.codefix {
462463
return key.replace("\*", matchedStar);
463464
}
464465
}
465-
else if (pattern === relativeName) {
466+
else if (pattern === relativeName || pattern === relativeNameWithIndex) {
466467
return key;
467468
}
468469
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
//// [|foo/*0*/();|]
4+
5+
// @Filename: folder_b/index.ts
6+
//// export function foo() {};
7+
8+
// @Filename: tsconfig.path.json
9+
//// {
10+
//// "compilerOptions": {
11+
//// "baseUrl": ".",
12+
//// "paths": {
13+
//// "b": [ "folder_b/index" ]
14+
//// }
15+
//// }
16+
//// }
17+
18+
// @Filename: tsconfig.json
19+
//// {
20+
//// "extends": "./tsconfig.path",
21+
//// "compilerOptions": { }
22+
//// }
23+
24+
verify.importFixAtPosition([
25+
`import { foo } from "b";
26+
27+
foo();`
28+
]);

0 commit comments

Comments
 (0)