Skip to content

Commit efc4d6b

Browse files
authored
fix: astral letters in consistent-return, no-eval, no-invalid-this (#21360)
1 parent 4618052 commit efc4d6b

5 files changed

Lines changed: 136 additions & 1 deletion

File tree

‎lib/rules/utils/ast-utils.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ function isModifyingReference(reference, index, references) {
108108
* @returns {boolean} `true` if the string starts with uppercase.
109109
*/
110110
function startsWithUpperCase(s) {
111-
return s[0] !== s[0].toLocaleLowerCase();
111+
const [firstChar] = s;
112+
113+
return firstChar !== firstChar.toLocaleLowerCase();
112114
}
113115

114116
/**

‎tests/lib/rules/consistent-return.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ ruleTester.run("consistent-return", rule, {
3535
"function foo() { function bar() { return true; } return; }",
3636
"function foo() { function bar() { return; } return false; }",
3737
"function Foo() { if (!(this instanceof Foo)) return new Foo(); }",
38+
"function Foo() { if (true) return 1; }",
39+
"function Φ() { if (true) return 1; }",
40+
{
41+
code: "function 𐐀() { if (true) return 1; }",
42+
languageOptions: { ecmaVersion: 6 },
43+
},
3844
"function foo() { if (true) return 5; else return undefined; }",
3945
"function foo() { if (true) return 5; else return void 0; }",
4046
{

‎tests/lib/rules/no-eval.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,31 @@ ruleTester.run("no-eval", rule, {
239239
globals: { window: "readonly" },
240240
},
241241
},
242+
243+
// Constructors with Unicode uppercase letters.
244+
"function Φ() { this.eval('foo'); }",
245+
{
246+
code: "function 𐐀() { this.eval('foo'); }",
247+
languageOptions: { ecmaVersion: 6 },
248+
},
249+
"Φ = function() { this.eval('foo'); };",
250+
{
251+
code: "𐐀 = function() { this.eval('foo'); };",
252+
languageOptions: { ecmaVersion: 6 },
253+
},
254+
"var Φ = function() { this.eval('foo'); };",
255+
{
256+
code: "var 𐐀 = function() { this.eval('foo'); };",
257+
languageOptions: { ecmaVersion: 6 },
258+
},
259+
{
260+
code: "function f(Φ = function() { this.eval('foo'); }) {}",
261+
languageOptions: { ecmaVersion: 6 },
262+
},
263+
{
264+
code: "function f(𐐀 = function() { this.eval('foo'); }) {}",
265+
languageOptions: { ecmaVersion: 6 },
266+
},
242267
],
243268

244269
invalid: [

‎tests/lib/rules/no-invalid-this.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,54 @@ const patterns = [
310310
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
311311
invalid: [],
312312
},
313+
{
314+
code: "function Φ() { this.x = 1; }",
315+
languageOptions: { ecmaVersion: 6 },
316+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
317+
invalid: [],
318+
},
319+
{
320+
code: "function 𐐀() { this.x = 1; }",
321+
languageOptions: { ecmaVersion: 6 },
322+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
323+
invalid: [],
324+
},
325+
{
326+
code: "Φ = function() { this.x = 1; };",
327+
languageOptions: { ecmaVersion: 6 },
328+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
329+
invalid: [],
330+
},
331+
{
332+
code: "𐐀 = function() { this.x = 1; };",
333+
languageOptions: { ecmaVersion: 6 },
334+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
335+
invalid: [],
336+
},
337+
{
338+
code: "var Φ = function() { this.x = 1; };",
339+
languageOptions: { ecmaVersion: 6 },
340+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
341+
invalid: [],
342+
},
343+
{
344+
code: "var 𐐀 = function() { this.x = 1; };",
345+
languageOptions: { ecmaVersion: 6 },
346+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
347+
invalid: [],
348+
},
349+
{
350+
code: "function f(Φ = function() { this.x = 1; }) {}",
351+
languageOptions: { ecmaVersion: 6 },
352+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
353+
invalid: [],
354+
},
355+
{
356+
code: "function f(𐐀 = function() { this.x = 1; }) {}",
357+
languageOptions: { ecmaVersion: 6 },
358+
valid: [NORMAL, USE_STRICT, IMPLIED_STRICT, MODULES],
359+
invalid: [],
360+
},
313361

314362
// On a property.
315363
{

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,60 @@ describe("ast-utils", () => {
204204
});
205205
});
206206

207+
describe("isES5Constructor", () => {
208+
it("should recognize a name starting with an uppercase ASCII letter", () => {
209+
const node = espree.parse("function Foo() {}", ESPREE_CONFIG)
210+
.body[0];
211+
212+
assert.isTrue(astUtils.isES5Constructor(node));
213+
});
214+
215+
it("should recognize a name starting with an uppercase Greek letter", () => {
216+
const node = espree.parse("function Φ() {}", ESPREE_CONFIG).body[0];
217+
218+
assert.isTrue(astUtils.isES5Constructor(node));
219+
});
220+
221+
it("should recognize a name starting with an uppercase astral letter", () => {
222+
const node = espree.parse("function 𐐀() {}", ESPREE_CONFIG).body[0];
223+
224+
assert.isTrue(astUtils.isES5Constructor(node));
225+
});
226+
227+
it("should reject a name starting with a lowercase ASCII letter", () => {
228+
const node = espree.parse("function foo() {}", ESPREE_CONFIG)
229+
.body[0];
230+
231+
assert.isFalse(astUtils.isES5Constructor(node));
232+
});
233+
234+
it("should only check the first letter", () => {
235+
const node = espree.parse("function fooBar() {}", ESPREE_CONFIG)
236+
.body[0];
237+
238+
assert.isFalse(astUtils.isES5Constructor(node));
239+
});
240+
241+
it("should reject a name starting with a lowercase Greek letter", () => {
242+
const node = espree.parse("function φ() {}", ESPREE_CONFIG).body[0];
243+
244+
assert.isFalse(astUtils.isES5Constructor(node));
245+
});
246+
247+
it("should reject a name starting with a lowercase astral letter", () => {
248+
const node = espree.parse("function 𐐨() {}", ESPREE_CONFIG).body[0];
249+
250+
assert.isFalse(astUtils.isES5Constructor(node));
251+
});
252+
253+
it("should reject a name starting with a letter without case", () => {
254+
const node = espree.parse("function 안녕() {}", ESPREE_CONFIG)
255+
.body[0];
256+
257+
assert.isFalse(astUtils.isES5Constructor(node));
258+
});
259+
});
260+
207261
describe("checkReference", () => {
208262
// catch
209263
it("should return true if reference is assigned for catch", () => {

0 commit comments

Comments
 (0)