Skip to content

Commit

Permalink
Enable support of JavaScript experimental class properties with Babel…
Browse files Browse the repository at this point in the history
… parser (#2751)
  • Loading branch information
yassin-kammoun-sonarsource committed Aug 12, 2021
1 parent 651bb50 commit 02f1309
Show file tree
Hide file tree
Showing 5 changed files with 1,001 additions and 17 deletions.
2 changes: 2 additions & 0 deletions eslint-bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"dependencies": {
"@babel/core": "7.13.15",
"@babel/eslint-parser": "7.13.14",
"@babel/preset-env": "7.15.0",
"@babel/preset-flow": "7.13.13",
"@babel/preset-react": "7.13.13",
"@typescript-eslint/eslint-plugin": "4.26.0",
Expand All @@ -64,6 +65,7 @@
"@typescript-eslint/parser",
"@babel/core",
"@babel/eslint-parser",
"@babel/preset-env",
"@babel/preset-flow",
"@babel/preset-react",
"builtin-modules",
Expand Down
6 changes: 5 additions & 1 deletion eslint-bridge/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@ export function parseExceptionCodeOf(exceptionMsg: string): ParseExceptionCode {
function babelConfig(config: Linter.ParserOptions) {
const pluginPath = `${__dirname}/../node_modules`;
const babelOptions = {
presets: [`${pluginPath}/@babel/preset-react`, `${pluginPath}/@babel/preset-flow`],
presets: [
`${pluginPath}/@babel/preset-react`,
`${pluginPath}/@babel/preset-flow`,
`${pluginPath}/@babel/preset-env`,
],
babelrc: false,
configFile: false,
};
Expand Down
12 changes: 11 additions & 1 deletion eslint-bridge/tests/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { ParseExceptionCode, parseExceptionCodeOf } from 'parser';
import { buildSourceCode, ParseExceptionCode, parseExceptionCodeOf } from 'parser';
import { SourceCode } from 'eslint';
import { ParsingError } from 'analyzer';
import { visit } from '../src/utils';
Expand Down Expand Up @@ -157,6 +157,16 @@ import { ParseExceptionCode } from '../src/parser';
const message = `DEBUG Failed to parse foo.vue with TypeScript compiler: '}' expected.`;
expect(callsToLogger.filter(args => args[0] === message)).toHaveLength(1);
});

it(`should parse experimental class properties with Babel parser`, () => {
const code = ` class C { #f = 42; #m() {} }`;
setContext({ workDir: '', shouldUseTypeScriptParserForJS: false, sonarlint: false });
const sourceCode = buildSourceCode(
{ filePath: '/some/path', fileContent: code },
'js',
) as SourceCode;
expect(sourceCode.ast).toBeDefined();
});
});

describe('parseTypeScriptSourceFile', () => {
Expand Down

0 comments on commit 02f1309

Please sign in to comment.