Skip to content

Commit

Permalink
JS-391 Babel presets as package names instead of paths (#4891)
Browse files Browse the repository at this point in the history
  • Loading branch information
vdiez committed Nov 14, 2024
1 parent e9ad482 commit ecf5f3c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/jsts/src/parsers/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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: {
Expand Down
1 change: 1 addition & 0 deletions packages/jsts/tests/parsers/fixtures/parse/valid.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
'howdy';
const a = <bla></bla>;
14 changes: 14 additions & 0 deletions packages/jsts/tests/parsers/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit ecf5f3c

Please sign in to comment.