From ecf5f3cbad184f7aef909ac3e3ad50067c8600cd Mon Sep 17 00:00:00 2001 From: Victor Date: Wed, 6 Nov 2024 10:00:47 +0100 Subject: [PATCH] JS-391 Babel presets as package names instead of paths (#4891) --- packages/jsts/src/parsers/options.ts | 11 ++++------- .../jsts/tests/parsers/fixtures/parse/valid.js | 1 + packages/jsts/tests/parsers/parse.test.ts | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/jsts/src/parsers/options.ts b/packages/jsts/src/parsers/options.ts index 1c07fccd246..5b0bc74f114 100644 --- a/packages/jsts/src/parsers/options.ts +++ b/packages/jsts/src/parsers/options.ts @@ -18,8 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { Linter } from 'eslint'; -import { dirname } from 'node:path'; -import { fileURLToPath } from 'node:url'; /** * Builds ESLint parser options @@ -72,15 +70,14 @@ export function buildParserOptions(initialOptions: Linter.ParserOptions, usingBa * @returns the extend parser options */ function babelParserOptions(options: Linter.ParserOptions) { - const pluginPath = `${dirname(fileURLToPath(import.meta.url))}/../../../../node_modules`; const babelOptions = { targets: 'defaults', presets: [ - `${pluginPath}/@babel/preset-react`, - `${pluginPath}/@babel/preset-flow`, - `${pluginPath}/@babel/preset-env`, + ['@babel/preset-react', {}], + ['@babel/preset-flow', {}], + ['@babel/preset-env', {}], ], - plugins: [[`${pluginPath}/@babel/plugin-proposal-decorators`, { version: '2022-03' }]], + plugins: [['@babel/plugin-proposal-decorators', { version: '2022-03' }]], babelrc: false, configFile: false, parserOpts: { diff --git a/packages/jsts/tests/parsers/fixtures/parse/valid.js b/packages/jsts/tests/parsers/fixtures/parse/valid.js index 318c0272911..24b568d833d 100644 --- a/packages/jsts/tests/parsers/fixtures/parse/valid.js +++ b/packages/jsts/tests/parsers/fixtures/parse/valid.js @@ -1 +1,2 @@ 'howdy'; +const a = ; diff --git a/packages/jsts/tests/parsers/parse.test.ts b/packages/jsts/tests/parsers/parse.test.ts index 8662646bf7e..105622a3912 100644 --- a/packages/jsts/tests/parsers/parse.test.ts +++ b/packages/jsts/tests/parsers/parse.test.ts @@ -37,6 +37,20 @@ const parseFunctions = [ ]; describe('parseForESLint', () => { + it(`Babel should fail parsing input with JSX without the React preset`, async () => { + const filePath = path.join(import.meta.dirname, 'fixtures', 'parse', 'valid.js'); + const fileContent = await readFile(filePath); + const fileType = 'MAIN'; + + const input = { filePath, fileType, fileContent } as JsTsAnalysisInput; + const options = buildParserOptions(input, true); + options.babelOptions.presets.shift(); + + expect(() => parseForESLint(fileContent, parseFunctions[0].parser.parse, options)).toThrow( + APIError.parsingError('Unexpected token (2:15)', { line: 2 }), + ); + }); + parseFunctions.forEach(({ parser, usingBabel, errorMessage }) => { it(`should parse a valid input with ${parser.parser}`, async () => { const filePath = path.join(import.meta.dirname, 'fixtures', 'parse', 'valid.js');