Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add some tests and refactor tsconfig path resolution #412

Merged
merged 1 commit into from Apr 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 9 additions & 12 deletions packages/typescript-estree/src/tsconfig-parser.ts
Expand Up @@ -50,6 +50,12 @@ function diagnosticReporter(diagnostic: ts.Diagnostic): void {

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

function getTsconfigPath(tsconfigPath: string, extra: Extra): string {
return path.isAbsolute(tsconfigPath)
? tsconfigPath
: path.join(extra.tsconfigRootDir || process.cwd(), tsconfigPath);
}

/**
* Calculate project environments using options provided by consumer and paths from config
* @param code The code being linted
Expand All @@ -64,7 +70,6 @@ export function calculateProjectParserOptions(
extra: Extra,
): ts.Program[] {
const results = [];
const tsconfigRootDir = extra.tsconfigRootDir;

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

for (let tsconfigPath of extra.projects) {
// if absolute paths aren't provided, make relative to tsconfigRootDir
if (!path.isAbsolute(tsconfigPath)) {
tsconfigPath = path.join(tsconfigRootDir, tsconfigPath);
}
for (let rawTsconfigPath of extra.projects) {
const tsconfigPath = getTsconfigPath(rawTsconfigPath, extra);

const existingWatch = knownWatchProgramMap.get(tsconfigPath);

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

let tsconfigPath = extra.projects[0];

// if absolute paths aren't provided, make relative to tsconfigRootDir
if (!path.isAbsolute(tsconfigPath)) {
tsconfigPath = path.join(extra.tsconfigRootDir, tsconfigPath);
}
const tsconfigPath = getTsconfigPath(extra.projects[0], extra);

const commandLine = ts.getParsedCommandLineOfConfigFile(
tsconfigPath,
Expand Down
47 changes: 47 additions & 0 deletions packages/typescript-estree/tests/lib/semanticInfo.ts
Expand Up @@ -48,6 +48,21 @@ describe('semanticInfo', () => {
);
});

it(`should cache the created ts.program`, () => {
const filename = testFiles[0];
const code = readFileSync(filename, 'utf8');
const options = createOptions(filename);
const optionsProjectString = {
...options,
project: './tsconfig.json',
};
expect(
parseAndGenerateServices(code, optionsProjectString).services.program,
).toBe(
parseAndGenerateServices(code, optionsProjectString).services.program,
);
});

it(`should handle "project": "./tsconfig.json" and "project": ["./tsconfig.json"] the same`, () => {
const filename = testFiles[0];
const code = readFileSync(filename, 'utf8');
Expand All @@ -65,6 +80,38 @@ describe('semanticInfo', () => {
);
});

it(`should resolve absolute and relative tsconfig paths the same`, () => {
const filename = testFiles[0];
const code = readFileSync(filename, 'utf8');
const options = createOptions(filename);
const optionsAbsolutePath = {
...options,
project: `${__dirname}/../fixtures/semanticInfo/tsconfig.json`,
};
const optionsRelativePath = {
...options,
project: `./tsconfig.json`,
};
const absolutePathResult = parseAndGenerateServices(
code,
optionsAbsolutePath,
);
const relativePathResult = parseAndGenerateServices(
code,
optionsRelativePath,
);
if (absolutePathResult.services.program === undefined) {
throw new Error('Unable to create ts.program for absolute tsconfig');
} else if (relativePathResult.services.program === undefined) {
throw new Error('Unable to create ts.program for relative tsconfig');
}
expect(
absolutePathResult.services.program.getResolvedProjectReferences(),
).toEqual(
relativePathResult.services.program.getResolvedProjectReferences(),
);
});

// case-specific tests
it('isolated-file tests', () => {
const fileName = resolve(FIXTURES_DIR, 'isolated-file.src.ts');
Expand Down