From d74838609eb64ad2f1edfeace7fa1164c1184fae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Louren=C3=A7o?= Date: Thu, 17 Jun 2021 15:29:32 +0100 Subject: [PATCH] Add record and tuple tokens to eslint parser (#13477) --- .../src/convert/convertTokens.cjs | 6 +++ eslint/babel-eslint-parser/test/index.js | 48 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/eslint/babel-eslint-parser/src/convert/convertTokens.cjs b/eslint/babel-eslint-parser/src/convert/convertTokens.cjs index 8cdaa566e134..0e871e0c519d 100644 --- a/eslint/babel-eslint-parser/src/convert/convertTokens.cjs +++ b/eslint/babel-eslint-parser/src/convert/convertTokens.cjs @@ -135,6 +135,12 @@ function convertToken(token, source, tl) { label === tl.doubleColon || label === tl.hash || label === tl.questionDot || + label === tl.braceHashL || + label === tl.braceBarL || + label === tl.braceBarR || + label === tl.bracketHashL || + label === tl.bracketBarL || + label === tl.bracketBarR || type.isAssign ) { token.type = "Punctuator"; diff --git a/eslint/babel-eslint-parser/test/index.js b/eslint/babel-eslint-parser/test/index.js index 64a3ae09d07d..093fbc65aefc 100644 --- a/eslint/babel-eslint-parser/test/index.js +++ b/eslint/babel-eslint-parser/test/index.js @@ -332,6 +332,54 @@ describe("Babel and Espree", () => { expect(babylonAST.tokens[1].type).toEqual("Punctuator"); }); + it("brace and bracket hash operator (token)", () => { + const code = "#[]; #{}"; + const babylonAST = parseForESLint(code, { + eslintVisitorKeys: true, + eslintScopeManager: true, + babelOptions: { + filename: "test.js", + parserOpts: { + plugins: [["recordAndTuple", { syntaxType: "hash" }]], + tokens: true, + }, + }, + }).ast; + expect(babylonAST.tokens[0]).toEqual( + expect.objectContaining({ type: "Punctuator", value: "#[" }), + ); + expect(babylonAST.tokens[3]).toEqual( + expect.objectContaining({ type: "Punctuator", value: "#{" }), + ); + }); + + it("brace and bracket bar operator (token)", () => { + const code = "{||}; [||]"; + const babylonAST = parseForESLint(code, { + eslintVisitorKeys: true, + eslintScopeManager: true, + babelOptions: { + filename: "test.js", + parserOpts: { + plugins: [["recordAndTuple", { syntaxType: "bar" }]], + tokens: true, + }, + }, + }).ast; + expect(babylonAST.tokens[0]).toEqual( + expect.objectContaining({ type: "Punctuator", value: "{|" }), + ); + expect(babylonAST.tokens[1]).toEqual( + expect.objectContaining({ type: "Punctuator", value: "|}" }), + ); + expect(babylonAST.tokens[3]).toEqual( + expect.objectContaining({ type: "Punctuator", value: "[|" }), + ); + expect(babylonAST.tokens[4]).toEqual( + expect.objectContaining({ type: "Punctuator", value: "|]" }), + ); + }); + if (process.env.BABEL_8_BREAKING) { it("hash (token)", () => { const code = "class A { #x }";