From 7168c3d933e3a9e0d23c366a78000540ca910411 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Sun, 24 Oct 2021 17:40:16 +0200 Subject: [PATCH] Review --- .../src/convert/convertTokens.cjs | 30 +++--- eslint/babel-eslint-parser/test/index.js | 97 ++++++++++++++++++- yarn.lock | 2 +- 3 files changed, 114 insertions(+), 15 deletions(-) diff --git a/eslint/babel-eslint-parser/src/convert/convertTokens.cjs b/eslint/babel-eslint-parser/src/convert/convertTokens.cjs index c69f85b0c5e9..a05a0a09824a 100644 --- a/eslint/babel-eslint-parser/src/convert/convertTokens.cjs +++ b/eslint/babel-eslint-parser/src/convert/convertTokens.cjs @@ -200,23 +200,31 @@ module.exports = function convertTokens(tokens, code, tl) { const withoutComments = convertTemplateType(tokens, tl).filter( t => t.type !== "CommentLine" && t.type !== "CommentBlock", ); - for (let i = 0; i < withoutComments.length; i++) { - let token = withoutComments[i]; + for (let i = 0, { length } = withoutComments; i < length; i++) { + const token = withoutComments[i]; if (!process.env.BABEL_8_BREAKING) { // Babel 8 already produces a single token - if (ESLINT_VERSION >= 8 && token.type.label === tl.hash) { - i++; - token = withoutComments[i]; + if ( + ESLINT_VERSION >= 8 && + i + 1 < length && + token.type.label === tl.hash + ) { + const nextToken = withoutComments[i + 1]; - token.type = "PrivateIdentifier"; - token.start -= 1; - token.loc.start.column -= 1; - token.range = [token.start, token.end]; + // We must disambiguate private identifier from the hack pipes topic token + if (nextToken.type.label === tl.name && token.end === nextToken.start) { + i++; - result.push(token); - continue; + nextToken.type = "PrivateIdentifier"; + nextToken.start -= 1; + nextToken.loc.start.column -= 1; + nextToken.range = [nextToken.start, nextToken.end]; + + result.push(nextToken); + continue; + } } } diff --git a/eslint/babel-eslint-parser/test/index.js b/eslint/babel-eslint-parser/test/index.js index b420a5f47fc6..16af7ccafa9f 100644 --- a/eslint/babel-eslint-parser/test/index.js +++ b/eslint/babel-eslint-parser/test/index.js @@ -423,7 +423,7 @@ describe("Babel and Espree", () => { }); if (process.env.BABEL_8_BREAKING) { - it("hash (token) - ESLint 7", () => { + it("private identifier (token) - ESLint 7", () => { const code = "class A { #x }"; const babylonAST = parseForESLint(code, { eslintVisitorKeys: true, @@ -434,7 +434,6 @@ describe("Babel and Espree", () => { expect(babylonAST.tokens[3].value).toEqual("x"); }); } else { - // Espree doesn't support private fields yet it("hash (token) - ESLint 7", () => { const code = "class A { #x }"; const babylonAST = parseForESLint(code, { @@ -447,7 +446,7 @@ describe("Babel and Espree", () => { }); } - it("hash (token) - ESLint 8", () => { + it("private identifier (token) - ESLint 8", () => { const code = "class A { #x }"; const babylonAST = parseForESLint8(code, { eslintVisitorKeys: true, @@ -576,6 +575,98 @@ describe("Babel and Espree", () => { expect(babylonAST.tokens[22]).toMatchObject(staticKw); }); + if (process.env.BABEL_8_BREAKING) { + it("pipeline # topic token - ESLint 7", () => { + const code = ` + x |> # + y |> #[0] + class A { + #x = y |> + # + z + } + `; + const babylonAST = parseForESLint(code, { + eslintVisitorKeys: true, + eslintScopeManager: true, + babelOptions: { + filename: "test.js", + parserOpts: { + plugins: [ + ["pipelineOperator", { proposal: "hack", topicToken: "#" }], + ], + tokens: true, + }, + }, + }).ast; + + const topicToken = { type: "Punctuator", value: "#" }; + expect(babylonAST.tokens[2]).toMatchObject(topicToken); + expect(babylonAST.tokens[5]).toMatchObject(topicToken); + expect(babylonAST.tokens[16]).toMatchObject(topicToken); + }); + } else { + it("pipeline # topic token - ESLint 7", () => { + const code = ` + x |> # + y |> #[0] + class A { + #x = y |> + # + z + } + `; + const babylonAST = parseForESLint(code, { + eslintVisitorKeys: true, + eslintScopeManager: true, + babelOptions: { + filename: "test.js", + parserOpts: { + plugins: [ + ["pipelineOperator", { proposal: "hack", topicToken: "#" }], + ], + tokens: true, + }, + }, + }).ast; + + const topicToken = { type: "Punctuator", value: "#" }; + expect(babylonAST.tokens[2]).toMatchObject(topicToken); + expect(babylonAST.tokens[5]).toMatchObject(topicToken); + expect(babylonAST.tokens[17]).toMatchObject(topicToken); + }); + } + + it("pipeline # topic token - ESLint 8", () => { + const code = ` + x |> # + y |> #[0] + class A { + #x = y |> + # + z + } + `; + const babylonAST = parseForESLint8(code, { + eslintVisitorKeys: true, + eslintScopeManager: true, + babelOptions: { + filename: "test.js", + parserOpts: { + plugins: [ + ["pipelineOperator", { proposal: "hack", topicToken: "#" }], + ], + tokens: true, + }, + }, + }).ast; + + const topicToken = { type: "Punctuator", value: "#" }; + expect(babylonAST.tokens[2]).toMatchObject(topicToken); + expect(babylonAST.tokens[5]).toMatchObject(topicToken); + expect(babylonAST.tokens[16]).toMatchObject(topicToken); + }); + it("empty program with line comment", () => { parseAndAssertSame("// single comment"); }); diff --git a/yarn.lock b/yarn.lock index 0d20e53b7975..0da6bc09f86d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7723,7 +7723,7 @@ __metadata: languageName: node linkType: hard -"eslint-8@npm:eslint@^8.0.0, eslint@npm:8.0.0": +"eslint-8@npm:eslint@^8.0.0": version: 8.0.0 resolution: "eslint@npm:8.0.0" dependencies: