From 13c750818c2ac42dc3f5498e62bf9960e0f4947e Mon Sep 17 00:00:00 2001 From: Victor Diez Date: Mon, 4 Nov 2024 15:06:09 +0100 Subject: [PATCH 1/2] Babel presets as objects instead of paths --- packages/jsts/src/parsers/options.ts | 15 ++++++--------- .../jsts/tests/parsers/fixtures/parse/valid.js | 1 + packages/jsts/tests/parsers/parse.test.ts | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/jsts/src/parsers/options.ts b/packages/jsts/src/parsers/options.ts index 1c07fccd246..47ea3843990 100644 --- a/packages/jsts/src/parsers/options.ts +++ b/packages/jsts/src/parsers/options.ts @@ -18,8 +18,10 @@ * 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'; +import { default as babelFlowPreset } from '@babel/preset-flow'; +import { default as babelReactPreset } from '@babel/preset-react'; +import { default as babelEnvPreset } from '@babel/preset-env'; +import babelDecoratorProposalPlugin from '@babel/plugin-proposal-decorators'; /** * Builds ESLint parser options @@ -72,15 +74,10 @@ 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`, - ], - plugins: [[`${pluginPath}/@babel/plugin-proposal-decorators`, { version: '2022-03' }]], + presets: [babelReactPreset, babelFlowPreset, babelEnvPreset], + plugins: [[babelDecoratorProposalPlugin, { 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'); From d802ab10940a1ae50607109ed351300874390fe4 Mon Sep 17 00:00:00 2001 From: Victor Diez Date: Mon, 4 Nov 2024 16:46:36 +0100 Subject: [PATCH 2/2] Babel presets as objects instead of paths --- packages/jsts/src/parsers/options.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/jsts/src/parsers/options.ts b/packages/jsts/src/parsers/options.ts index 47ea3843990..5b0bc74f114 100644 --- a/packages/jsts/src/parsers/options.ts +++ b/packages/jsts/src/parsers/options.ts @@ -18,10 +18,6 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { Linter } from 'eslint'; -import { default as babelFlowPreset } from '@babel/preset-flow'; -import { default as babelReactPreset } from '@babel/preset-react'; -import { default as babelEnvPreset } from '@babel/preset-env'; -import babelDecoratorProposalPlugin from '@babel/plugin-proposal-decorators'; /** * Builds ESLint parser options @@ -76,8 +72,12 @@ export function buildParserOptions(initialOptions: Linter.ParserOptions, usingBa function babelParserOptions(options: Linter.ParserOptions) { const babelOptions = { targets: 'defaults', - presets: [babelReactPreset, babelFlowPreset, babelEnvPreset], - plugins: [[babelDecoratorProposalPlugin, { version: '2022-03' }]], + presets: [ + ['@babel/preset-react', {}], + ['@babel/preset-flow', {}], + ['@babel/preset-env', {}], + ], + plugins: [['@babel/plugin-proposal-decorators', { version: '2022-03' }]], babelrc: false, configFile: false, parserOpts: {