Merge pull request #13287 from zhengbli/importFixExtendedTsconfig

For `path` ending with `index`, use the `path` pattern for the import quick fix
This commit is contained in:
Zhengbo Li 2017-01-04 15:37:48 -08:00 committed by GitHub
commit 9a62db2b5c
3 changed files with 47 additions and 14 deletions

View file

@ -262,23 +262,12 @@ namespace FourSlash {
// Initialize the language service with all the scripts
let startResolveFileRef: FourSlashFile;
let configFileName: string;
ts.forEach(testData.files, file => {
// Create map between fileName and its content for easily looking up when resolveReference flag is specified
this.inputFiles[file.fileName] = file.content;
if (ts.getBaseFileName(file.fileName).toLowerCase() === "tsconfig.json") {
const configJson = ts.parseConfigFileTextToJson(file.fileName, file.content);
assert.isTrue(configJson.config !== undefined);
// Extend our existing compiler options so that we can also support tsconfig only options
if (configJson.config.compilerOptions) {
const baseDirectory = ts.normalizePath(ts.getDirectoryPath(file.fileName));
const tsConfig = ts.convertCompilerOptionsFromJson(configJson.config.compilerOptions, baseDirectory, file.fileName);
if (!tsConfig.errors || !tsConfig.errors.length) {
compilationOptions = ts.extend(compilationOptions, tsConfig.options);
}
}
configFileName = file.fileName;
}
if (!startResolveFileRef && file.fileOptions[metadataOptionNames.resolveReference] === "true") {
@ -290,6 +279,21 @@ namespace FourSlash {
}
});
if (configFileName) {
const baseDir = ts.normalizePath(ts.getDirectoryPath(configFileName));
const host = new Utils.MockParseConfigHost(baseDir, /*ignoreCase*/ false, this.inputFiles);
const configJsonObj = ts.parseConfigFileTextToJson(configFileName, this.inputFiles[configFileName]);
assert.isTrue(configJsonObj.config !== undefined);
const { options, errors } = ts.parseJsonConfigFileContent(configJsonObj.config, host, baseDir);
// Extend our existing compiler options so that we can also support tsconfig only options
if (!errors || errors.length === 0) {
compilationOptions = ts.extend(compilationOptions, options);
}
}
if (compilationOptions.typeRoots) {
compilationOptions.typeRoots = compilationOptions.typeRoots.map(p => ts.getNormalizedAbsolutePath(p, this.basePath));

View file

@ -443,6 +443,7 @@ namespace ts.codefix {
return undefined;
}
const relativeNameWithIndex = removeFileExtension(relativeName);
relativeName = removeExtensionAndIndexPostFix(relativeName);
if (options.paths) {
@ -462,7 +463,7 @@ namespace ts.codefix {
return key.replace("\*", matchedStar);
}
}
else if (pattern === relativeName) {
else if (pattern === relativeName || pattern === relativeNameWithIndex) {
return key;
}
}

View file

@ -0,0 +1,28 @@
/// <reference path="fourslash.ts" />
//// [|foo/*0*/();|]
// @Filename: folder_b/index.ts
//// export function foo() {};
// @Filename: tsconfig.path.json
//// {
//// "compilerOptions": {
//// "baseUrl": ".",
//// "paths": {
//// "b": [ "folder_b/index" ]
//// }
//// }
//// }
// @Filename: tsconfig.json
//// {
//// "extends": "./tsconfig.path",
//// "compilerOptions": { }
//// }
verify.importFixAtPosition([
`import { foo } from "b";
foo();`
]);