Skip to content
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

JS-391 Babel presets as package names instead of paths #4891

Merged
merged 2 commits into from
Nov 6, 2024
Merged
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
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