Skip to content

Commit 161558a

Browse files
authored
fix: handle LS, PS and empty hashbang comments (#21419)
* fix: handle LS, PS and empty hashbang comments * wip * wip
1 parent 6194939 commit 161558a

6 files changed

Lines changed: 170 additions & 7 deletions

File tree

‎lib/shared/ast-utils.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
const breakableTypePattern =
1212
/^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/u;
1313
const lineBreakPattern = /\r\n|[\r\n\u2028\u2029]/u;
14-
const shebangPattern = /^#!([^\r\n]+)/u;
14+
const shebangPattern = /^#!([^\r\n\u2028\u2029]*)/u;
1515

1616
/**
1717
* Creates a version of the `lineBreakPattern` regex with the global flag.

‎tests/lib/languages/js/source-code/source-code.js‎

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -364,23 +364,39 @@ describe("SourceCode", () => {
364364
describe("getText()", () => {
365365
let sourceCode, ast;
366366

367-
describe("when text begins with a shebang", () => {
368-
it("should retrieve unaltered shebang text", () => {
367+
[
368+
[SHEBANG_TEST_CODE, "#!/usr/bin/env node"],
369+
["#!/usr/bin/env node\rconst x = 1;", "#!/usr/bin/env node"],
370+
["#!\rconst x = 1;", "#!"],
371+
["#!/usr/bin/env node\nconst x = 1;", "#!/usr/bin/env node"],
372+
["#!\nconst x = 1;", "#!"],
373+
["#!/usr/bin/env node\r\nconst x = 1;", "#!/usr/bin/env node"],
374+
["#!\r\nconst x = 1;", "#!"],
375+
["#!/usr/bin/env node\u2028const x = 1;", "#!/usr/bin/env node"],
376+
["#!\u2028const x = 1;", "#!"],
377+
["#!/usr/bin/env node\u2029const x = 1;", "#!/usr/bin/env node"],
378+
["#!\u2029const x = 1;", "#!"],
379+
["#!/usr/bin/env node", "#!/usr/bin/env node"],
380+
["#!", "#!"],
381+
].forEach(([code, shebangText]) => {
382+
it(code, () => {
369383
// Shebangs are normalized to line comments before parsing.
370384
ast = espree.parse(
371-
SHEBANG_TEST_CODE.replace(
385+
code.replace(
372386
astUtils.shebangPattern,
373387
(match, captured) => `//${captured}`,
374388
),
375389
DEFAULT_CONFIG,
376390
);
377-
sourceCode = new SourceCode(SHEBANG_TEST_CODE, ast);
391+
sourceCode = new SourceCode(code, ast);
378392

379393
const shebangToken = sourceCode.getAllComments()[0];
380-
const shebangText = sourceCode.getText(shebangToken);
381394

382395
assert.strictEqual(shebangToken.type, "Shebang");
383-
assert.strictEqual(shebangText, "#!/usr/bin/env node");
396+
assert.strictEqual(
397+
sourceCode.getText(shebangToken),
398+
shebangText,
399+
);
384400
});
385401
});
386402

‎tests/lib/rules/capitalized-comments.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ ruleTester.run("capitalized-comments", rule, {
7777
"#!/usr/bin/env node",
7878
{ code: "#!/usr/bin/env node", options: ["always"] },
7979
{ code: "#!/usr/bin/env node", options: ["never"] },
80+
"#!foo\u2028var a = 1;",
81+
{ code: "#!foo\u2028var a = 1;", options: ["always"] },
82+
{ code: "#!Foo\u2028var a = 1;", options: ["never"] },
83+
"#!foo\u2029var a = 1;",
84+
{ code: "#!foo\u2029var a = 1;", options: ["always"] },
85+
{ code: "#!Foo\u2029var a = 1;", options: ["never"] },
86+
"#!\u2028var a = 1;",
87+
"#!\u2029var a = 1;",
88+
"#!",
8089

8190
// Using "always" string option
8291
{ code: "//Uppercase", options: ["always"] },

‎tests/lib/rules/no-inline-comments.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ ruleTester.run("no-inline-comments", rule, {
4444
"foo(); /* globals foo */",
4545
"var foo; /* exported foo */",
4646

47+
// Shebang
48+
"#!/usr/bin/env node\u2028var a = 1;",
49+
"#!/usr/bin/env node\u2029var a = 1;",
50+
"#!\u2028var a = 1;",
51+
"#!\u2029var a = 1;",
52+
"#!/usr/bin/env node",
53+
"#!",
54+
4755
// JSX exception
4856
`var a = (
4957
<div>

‎tests/lib/rules/no-warning-comments.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ ruleTester.run("no-warning-comments", rule, {
6161
{ code: "// foo", options: [{ terms: ["foo-bar"] }] },
6262
"/** multi-line block comment with lines starting with\nTODO\nFIXME or\nXXX\n*/",
6363
{ code: "//!TODO ", options: [{ decoration: ["*"] }] },
64+
65+
// Shebang
66+
"#!todo\u2028var a = 1;",
67+
"#!\u2028var a = 1;",
68+
"#!todo\u2029var a = 1;",
69+
"#!\u2029var a = 1;",
70+
"#!todo",
71+
"#!",
6472
],
6573
invalid: [
6674
{
@@ -633,5 +641,21 @@ ruleTester.run("no-warning-comments", rule, {
633641
},
634642
],
635643
},
644+
{
645+
code: "#!todo\u2028// todo",
646+
errors: [
647+
{
648+
messageId: "unexpectedComment",
649+
data: {
650+
matchedTerm: "todo",
651+
comment: "todo",
652+
},
653+
line: 2,
654+
column: 1,
655+
endLine: 2,
656+
endColumn: 8,
657+
},
658+
],
659+
},
636660
],
637661
});

‎tests/lib/shared/ast-utils.js‎

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,111 @@ describe("shared ast-utils", () => {
116116
assert.isNotNull(match);
117117
assert.strictEqual(match[1], "/usr/bin/env node");
118118
});
119+
120+
it("should match a shebang before CR", () => {
121+
const match = astUtils.shebangPattern.exec(
122+
"#!/usr/bin/env node\rconst x = 1;",
123+
);
124+
125+
assert.isNotNull(match);
126+
assert.strictEqual(match[0], "#!/usr/bin/env node");
127+
assert.strictEqual(match[1], "/usr/bin/env node");
128+
});
129+
130+
it("should match an empty shebang before CR", () => {
131+
const match = astUtils.shebangPattern.exec("#!\rconst x = 1;");
132+
133+
assert.isNotNull(match);
134+
assert.strictEqual(match[0], "#!");
135+
assert.strictEqual(match[1], "");
136+
});
137+
138+
it("should match a shebang before LF", () => {
139+
const match = astUtils.shebangPattern.exec(
140+
"#!/usr/bin/env node\nconst x = 1;",
141+
);
142+
143+
assert.isNotNull(match);
144+
assert.strictEqual(match[0], "#!/usr/bin/env node");
145+
assert.strictEqual(match[1], "/usr/bin/env node");
146+
});
147+
148+
it("should match an empty shebang before LF", () => {
149+
const match = astUtils.shebangPattern.exec("#!\nconst x = 1;");
150+
151+
assert.isNotNull(match);
152+
assert.strictEqual(match[0], "#!");
153+
assert.strictEqual(match[1], "");
154+
});
155+
156+
it("should match a shebang before CRLF", () => {
157+
const match = astUtils.shebangPattern.exec(
158+
"#!/usr/bin/env node\r\nconst x = 1;",
159+
);
160+
161+
assert.isNotNull(match);
162+
assert.strictEqual(match[0], "#!/usr/bin/env node");
163+
assert.strictEqual(match[1], "/usr/bin/env node");
164+
});
165+
166+
it("should match an empty shebang before CRLF", () => {
167+
const match = astUtils.shebangPattern.exec("#!\r\nconst x = 1;");
168+
169+
assert.isNotNull(match);
170+
assert.strictEqual(match[0], "#!");
171+
assert.strictEqual(match[1], "");
172+
});
173+
174+
it("should match a shebang before LS", () => {
175+
const match = astUtils.shebangPattern.exec(
176+
"#!/usr/bin/env node\u2028const x = 1;",
177+
);
178+
179+
assert.isNotNull(match);
180+
assert.strictEqual(match[0], "#!/usr/bin/env node");
181+
assert.strictEqual(match[1], "/usr/bin/env node");
182+
});
183+
184+
it("should match an empty shebang before LS", () => {
185+
const match = astUtils.shebangPattern.exec("#!\u2028const x = 1;");
186+
187+
assert.isNotNull(match);
188+
assert.strictEqual(match[0], "#!");
189+
assert.strictEqual(match[1], "");
190+
});
191+
192+
it("should match a shebang before PS", () => {
193+
const match = astUtils.shebangPattern.exec(
194+
"#!/usr/bin/env node\u2029const x = 1;",
195+
);
196+
197+
assert.isNotNull(match);
198+
assert.strictEqual(match[0], "#!/usr/bin/env node");
199+
assert.strictEqual(match[1], "/usr/bin/env node");
200+
});
201+
202+
it("should match an empty shebang before PS", () => {
203+
const match = astUtils.shebangPattern.exec("#!\u2029const x = 1;");
204+
205+
assert.isNotNull(match);
206+
assert.strictEqual(match[0], "#!");
207+
assert.strictEqual(match[1], "");
208+
});
209+
210+
it("should match a shebang at EOF", () => {
211+
const match = astUtils.shebangPattern.exec("#!/usr/bin/env node");
212+
213+
assert.isNotNull(match);
214+
assert.strictEqual(match[0], "#!/usr/bin/env node");
215+
assert.strictEqual(match[1], "/usr/bin/env node");
216+
});
217+
218+
it("should match an empty shebang at EOF", () => {
219+
const match = astUtils.shebangPattern.exec("#!");
220+
221+
assert.isNotNull(match);
222+
assert.strictEqual(match[0], "#!");
223+
assert.strictEqual(match[1], "");
224+
});
119225
});
120226
});

0 commit comments

Comments
 (0)