Skip to content

test(typescript-estree): split up parse #6049

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`parseAndGenerateServices invalid project error messages throws when non of multiple projects include the file 1`] = `
"ESLint was configured to run on \`<tsconfigRootDir>/ts/notIncluded0j1.ts\` using \`parserOptions.project\`:
- <tsconfigRootDir>/tsconfig.json
- <tsconfigRootDir>/tsconfig.extra.json
However, none of those TSConfigs include this file. Either:
- Change ESLint's list of included files to not include this file
- Change one of those TSConfigs to include this file
- Create a new TSConfig that includes this file and include it in your parserOptions.project
See the TypeScript ESLint docs for more info: https://typescript-eslint.io/docs/linting/troubleshooting##i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,6 @@ However, that TSConfig does not include this file. Either:
See the TypeScript ESLint docs for more info: https://typescript-eslint.io/docs/linting/troubleshooting##i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file"
`;

exports[`parseAndGenerateServices invalid project error messages throws when non of multiple projects include the file 1`] = `
"ESLint was configured to run on \`<tsconfigRootDir>/ts/notIncluded0j1.ts\` using \`parserOptions.project\`:
- <tsconfigRootDir>/tsconfig.json
- <tsconfigRootDir>/tsconfig.extra.json
However, none of those TSConfigs include this file. Either:
- Change ESLint's list of included files to not include this file
- Change one of those TSConfigs to include this file
- Create a new TSConfig that includes this file and include it in your parserOptions.project
See the TypeScript ESLint docs for more info: https://typescript-eslint.io/docs/linting/troubleshooting##i-get-errors-telling-me-eslint-was-configured-to-run--however-that-tsconfig-does-not--none-of-those-tsconfigs-include-this-file"
`;

exports[`parseAndGenerateServices isolated parsing should parse .js file - with JSX content - parserOptions.jsx = false 1`] = `
{
"ast": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { join, resolve } from 'path';

import * as parser from '../../src';
import type * as astConverterModule from '../../src/ast-converter';
import type * as sharedParserUtilsModule from '../../src/create-program/shared';
import type { TSESTreeOptions } from '../../src/parser-options';

const FIXTURES_DIR = join(__dirname, '../fixtures/simpleProject');

// we can't spy on the exports of an ES module - so we instead have to mock the entire module
jest.mock('../../src/ast-converter', () => {
const astConverterActual = jest.requireActual<typeof astConverterModule>(
'../../src/ast-converter',
);

return {
...astConverterActual,
__esModule: true,
astConverter: jest.fn(astConverterActual.astConverter),
};
});
jest.mock('../../src/create-program/shared', () => {
const sharedActual = jest.requireActual<typeof sharedParserUtilsModule>(
'../../src/create-program/shared',
);

return {
...sharedActual,
__esModule: true,
createDefaultCompilerOptionsFromExtra: jest.fn(
sharedActual.createDefaultCompilerOptionsFromExtra,
),
};
});

// Tests in CI by default run with lowercase program file names,
// resulting in path.relative results starting with many "../"s
jest.mock('typescript', () => {
const ts = jest.requireActual('typescript');
return {
...ts,
sys: {
...ts.sys,
useCaseSensitiveFileNames: true,
},
};
});

/**
* Aligns paths between environments, node for windows uses `\`, for linux and mac uses `/`
*/
function alignErrorPath(error: Error): never {
error.message = error.message.replace(/\\(?!["])/gm, '/');
throw error;
}

beforeEach(() => {
jest.clearAllMocks();
});

describe('parseAndGenerateServices', () => {
describe('invalid project error messages', () => {
it('throws when non of multiple projects include the file', () => {
const PROJECT_DIR = resolve(FIXTURES_DIR, '../invalidFileErrors');
const code = 'var a = true';
const config: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
tsconfigRootDir: PROJECT_DIR,
project: ['./**/tsconfig.json', './**/tsconfig.extra.json'],
};
const testParse = (filePath: string) => (): void => {
try {
parser.parseAndGenerateServices(code, {
...config,
filePath: join(PROJECT_DIR, filePath),
});
} catch (error) {
throw alignErrorPath(error as Error);
}
};

expect(testParse('ts/notIncluded0j1.ts')).toThrowErrorMatchingSnapshot();
});
});
});
142 changes: 142 additions & 0 deletions packages/typescript-estree/tests/lib/parse-moduleResolver.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import { join, resolve } from 'path';

import * as parser from '../../src';
import type * as astConverterModule from '../../src/ast-converter';
import type * as sharedParserUtilsModule from '../../src/create-program/shared';
import type { TSESTreeOptions } from '../../src/parser-options';

const FIXTURES_DIR = join(__dirname, '../fixtures/simpleProject');

// we can't spy on the exports of an ES module - so we instead have to mock the entire module
jest.mock('../../src/ast-converter', () => {
const astConverterActual = jest.requireActual<typeof astConverterModule>(
'../../src/ast-converter',
);

return {
...astConverterActual,
__esModule: true,
astConverter: jest.fn(astConverterActual.astConverter),
};
});
jest.mock('../../src/create-program/shared', () => {
const sharedActual = jest.requireActual<typeof sharedParserUtilsModule>(
'../../src/create-program/shared',
);

return {
...sharedActual,
__esModule: true,
createDefaultCompilerOptionsFromExtra: jest.fn(
sharedActual.createDefaultCompilerOptionsFromExtra,
),
};
});

// Tests in CI by default run with lowercase program file names,
// resulting in path.relative results starting with many "../"s
jest.mock('typescript', () => {
const ts = jest.requireActual('typescript');
return {
...ts,
sys: {
...ts.sys,
useCaseSensitiveFileNames: true,
},
};
});

beforeEach(() => {
jest.clearAllMocks();
});
describe('parseAndGenerateServices', () => {
describe('moduleResolver', () => {
beforeEach(() => {
parser.clearCaches();
});

const PROJECT_DIR = resolve(FIXTURES_DIR, '../moduleResolver');
const code = `
import { something } from '__PLACEHOLDER__';

something();
`;
const config: TSESTreeOptions = {
comment: true,
tokens: true,
range: true,
loc: true,
project: './tsconfig.json',
tsconfigRootDir: PROJECT_DIR,
filePath: resolve(PROJECT_DIR, 'file.ts'),
};
const withDefaultProgramConfig: TSESTreeOptions = {
...config,
project: './tsconfig.defaultProgram.json',
createDefaultProgram: true,
};

describe('when file is in the project', () => {
it('returns error if __PLACEHOLDER__ can not be resolved', () => {
expect(
parser
.parseAndGenerateServices(code, config)
.services.program.getSemanticDiagnostics(),
).toHaveProperty(
[0, 'messageText'],
"Cannot find module '__PLACEHOLDER__' or its corresponding type declarations.",
);
});

it('throws error if moduleResolver can not be found', () => {
expect(() =>
parser.parseAndGenerateServices(code, {
...config,
moduleResolver: resolve(
PROJECT_DIR,
'./this_moduleResolver_does_not_exist.js',
),
}),
).toThrowErrorMatchingInlineSnapshot(`
"Could not find the provided parserOptions.moduleResolver.
Hint: use an absolute path if you are not in control over where the ESLint instance runs."
`);
});

it('resolves __PLACEHOLDER__ correctly', () => {
expect(
parser
.parseAndGenerateServices(code, {
...config,
moduleResolver: resolve(PROJECT_DIR, './moduleResolver.js'),
})
.services.program.getSemanticDiagnostics(),
).toHaveLength(0);
});
});

describe('when file is not in the project and createDefaultProgram=true', () => {
it('returns error because __PLACEHOLDER__ can not be resolved', () => {
expect(
parser
.parseAndGenerateServices(code, withDefaultProgramConfig)
.services.program.getSemanticDiagnostics(),
).toHaveProperty(
[0, 'messageText'],
"Cannot find module '__PLACEHOLDER__' or its corresponding type declarations.",
);
});

it('resolves __PLACEHOLDER__ correctly', () => {
expect(
parser
.parseAndGenerateServices(code, {
...withDefaultProgramConfig,
moduleResolver: resolve(PROJECT_DIR, './moduleResolver.js'),
})
.services.program.getSemanticDiagnostics(),
).toHaveLength(0);
});
});
});
});
Loading