2 var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
5 Object.defineProperty(exports, "__esModule", { value: true });
6 exports.createProjectProgram = void 0;
7 const debug_1 = __importDefault(require("debug"));
8 const path_1 = __importDefault(require("path"));
9 const createWatchProgram_1 = require("./createWatchProgram");
10 const node_utils_1 = require("../node-utils");
11 const log = debug_1.default('typescript-eslint:typescript-estree:createProjectProgram');
12 const DEFAULT_EXTRA_FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx'];
13 function getExtension(fileName) {
17 return fileName.endsWith('.d.ts') ? '.d.ts' : path_1.default.extname(fileName);
20 * @param code The code of the file being linted
21 * @param createDefaultProgram True if the default program should be created
22 * @param extra The config object
23 * @returns If found, returns the source file corresponding to the code and the containing program
25 function createProjectProgram(code, createDefaultProgram, extra) {
26 log('Creating project program for: %s', extra.filePath);
27 const astAndProgram = node_utils_1.firstDefined(createWatchProgram_1.getProgramsForProjects(code, extra.filePath, extra), currentProgram => {
28 const ast = currentProgram.getSourceFile(extra.filePath);
29 // working around https://github.com/typescript-eslint/typescript-eslint/issues/1573
30 const expectedExt = getExtension(extra.filePath);
31 const returnedExt = getExtension(ast === null || ast === void 0 ? void 0 : ast.fileName);
32 if (expectedExt !== returnedExt) {
35 return ast && { ast, program: currentProgram };
37 if (!astAndProgram && !createDefaultProgram) {
38 // the file was either not matched within the tsconfig, or the extension wasn't expected
40 '"parserOptions.project" has been set for @typescript-eslint/parser.',
41 `The file does not match your project config: ${path_1.default.relative(extra.tsconfigRootDir || process.cwd(), extra.filePath)}.`,
43 let hasMatchedAnError = false;
44 const extraFileExtensions = extra.extraFileExtensions || [];
45 extraFileExtensions.forEach(extraExtension => {
46 if (!extraExtension.startsWith('.')) {
47 errorLines.push(`Found unexpected extension "${extraExtension}" specified with the "extraFileExtensions" option. Did you mean ".${extraExtension}"?`);
49 if (DEFAULT_EXTRA_FILE_EXTENSIONS.includes(extraExtension)) {
50 errorLines.push(`You unnecessarily included the extension "${extraExtension}" with the "extraFileExtensions" option. This extension is already handled by the parser by default.`);
53 const fileExtension = path_1.default.extname(extra.filePath);
54 if (!DEFAULT_EXTRA_FILE_EXTENSIONS.includes(fileExtension)) {
55 const nonStandardExt = `The extension for the file (${fileExtension}) is non-standard`;
56 if (extraFileExtensions.length > 0) {
57 if (!extraFileExtensions.includes(fileExtension)) {
58 errorLines.push(`${nonStandardExt}. It should be added to your existing "parserOptions.extraFileExtensions".`);
59 hasMatchedAnError = true;
63 errorLines.push(`${nonStandardExt}. You should add "parserOptions.extraFileExtensions" to your config.`);
64 hasMatchedAnError = true;
67 if (!hasMatchedAnError) {
68 errorLines.push('The file must be included in at least one of the projects provided.');
70 throw new Error(errorLines.join('\n'));
74 exports.createProjectProgram = createProjectProgram;
75 //# sourceMappingURL=createProjectProgram.js.map