Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 24, 2021
1 parent 9d4f3d6 commit 7168c3d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 15 deletions.
30 changes: 19 additions & 11 deletions eslint/babel-eslint-parser/src/convert/convertTokens.cjs
Expand Up @@ -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;
}
}
}

Expand Down
97 changes: 94 additions & 3 deletions eslint/babel-eslint-parser/test/index.js
Expand Up @@ -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,
Expand All @@ -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, {
Expand All @@ -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,
Expand Down Expand Up @@ -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");
});
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Expand Up @@ -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:
Expand Down

0 comments on commit 7168c3d

Please sign in to comment.