Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f77ba6

Browse files
dsgkirkbyJamesHenry
authored andcommitted
chore: add some tests and refactor tsconfig path resolution (typescript-eslint#412)
1 parent fab182f commit 6f77ba6

File tree

2 files changed

+56
-12
lines changed

2 files changed

+56
-12
lines changed

packages/typescript-estree/src/tsconfig-parser.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ function diagnosticReporter(diagnostic: ts.Diagnostic): void {
5050

5151
const noopFileWatcher = { close: () => {} };
5252

53+
function getTsconfigPath(tsconfigPath: string, extra: Extra): string {
54+
return path.isAbsolute(tsconfigPath)
55+
? tsconfigPath
56+
: path.join(extra.tsconfigRootDir || process.cwd(), tsconfigPath);
57+
}
58+
5359
/**
5460
* Calculate project environments using options provided by consumer and paths from config
5561
* @param code The code being linted
@@ -64,7 +70,6 @@ export function calculateProjectParserOptions(
6470
extra: Extra,
6571
): ts.Program[] {
6672
const results = [];
67-
const tsconfigRootDir = extra.tsconfigRootDir;
6873

6974
// preserve reference to code and file being linted
7075
currentLintOperationState.code = code;
@@ -77,11 +82,8 @@ export function calculateProjectParserOptions(
7782
watchCallback(filePath, ts.FileWatcherEventKind.Changed);
7883
}
7984

80-
for (let tsconfigPath of extra.projects) {
81-
// if absolute paths aren't provided, make relative to tsconfigRootDir
82-
if (!path.isAbsolute(tsconfigPath)) {
83-
tsconfigPath = path.join(tsconfigRootDir, tsconfigPath);
84-
}
85+
for (let rawTsconfigPath of extra.projects) {
86+
const tsconfigPath = getTsconfigPath(rawTsconfigPath, extra);
8587

8688
const existingWatch = knownWatchProgramMap.get(tsconfigPath);
8789

@@ -193,12 +195,7 @@ export function createProgram(code: string, filePath: string, extra: Extra) {
193195
return undefined;
194196
}
195197

196-
let tsconfigPath = extra.projects[0];
197-
198-
// if absolute paths aren't provided, make relative to tsconfigRootDir
199-
if (!path.isAbsolute(tsconfigPath)) {
200-
tsconfigPath = path.join(extra.tsconfigRootDir, tsconfigPath);
201-
}
198+
const tsconfigPath = getTsconfigPath(extra.projects[0], extra);
202199

203200
const commandLine = ts.getParsedCommandLineOfConfigFile(
204201
tsconfigPath,

packages/typescript-estree/tests/lib/semanticInfo.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ describe('semanticInfo', () => {
4848
);
4949
});
5050

51+
it(`should cache the created ts.program`, () => {
52+
const filename = testFiles[0];
53+
const code = readFileSync(filename, 'utf8');
54+
const options = createOptions(filename);
55+
const optionsProjectString = {
56+
...options,
57+
project: './tsconfig.json',
58+
};
59+
expect(
60+
parseAndGenerateServices(code, optionsProjectString).services.program,
61+
).toBe(
62+
parseAndGenerateServices(code, optionsProjectString).services.program,
63+
);
64+
});
65+
5166
it(`should handle "project": "./tsconfig.json" and "project": ["./tsconfig.json"] the same`, () => {
5267
const filename = testFiles[0];
5368
const code = readFileSync(filename, 'utf8');
@@ -65,6 +80,38 @@ describe('semanticInfo', () => {
6580
);
6681
});
6782

83+
it(`should resolve absolute and relative tsconfig paths the same`, () => {
84+
const filename = testFiles[0];
85+
const code = readFileSync(filename, 'utf8');
86+
const options = createOptions(filename);
87+
const optionsAbsolutePath = {
88+
...options,
89+
project: `${__dirname}/../fixtures/semanticInfo/tsconfig.json`,
90+
};
91+
const optionsRelativePath = {
92+
...options,
93+
project: `./tsconfig.json`,
94+
};
95+
const absolutePathResult = parseAndGenerateServices(
96+
code,
97+
optionsAbsolutePath,
98+
);
99+
const relativePathResult = parseAndGenerateServices(
100+
code,
101+
optionsRelativePath,
102+
);
103+
if (absolutePathResult.services.program === undefined) {
104+
throw new Error('Unable to create ts.program for absolute tsconfig');
105+
} else if (relativePathResult.services.program === undefined) {
106+
throw new Error('Unable to create ts.program for relative tsconfig');
107+
}
108+
expect(
109+
absolutePathResult.services.program.getResolvedProjectReferences(),
110+
).toEqual(
111+
relativePathResult.services.program.getResolvedProjectReferences(),
112+
);
113+
});
114+
68115
// case-specific tests
69116
it('isolated-file tests', () => {
70117
const fileName = resolve(FIXTURES_DIR, 'isolated-file.src.ts');

0 commit comments

Comments
 (0)