Skip to content

Commit 3c7d7c0

Browse files
authored
no-thenable: Fix crash on {[Symbol.prototype]: 0} (#2248)
1 parent 3b504fa commit 3c7d7c0

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

rules/no-thenable.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ const messages = {
1313

1414
const isStringThen = (node, context) =>
1515
getStaticValue(node, context.sourceCode.getScope(node))?.value === 'then';
16+
const isPropertyThen = (node, context) => {
17+
// `getPropertyName` throws on `({[Symbol.prototype]: 0})`
18+
// https://github.com/eslint-community/eslint-utils/pull/182
19+
try {
20+
return getPropertyName(node, context.sourceCode.getScope(node)) === 'then';
21+
} catch {}
22+
23+
return false;
24+
};
1625

1726
const cases = [
1827
// `{then() {}}`,
@@ -23,10 +32,7 @@ const cases = [
2332
selector: 'ObjectExpression',
2433
* getNodes(node, context) {
2534
for (const property of node.properties) {
26-
if (
27-
property.type === 'Property'
28-
&& getPropertyName(property, context.sourceCode.getScope(property)) === 'then'
29-
) {
35+
if (property.type === 'Property' && isPropertyThen(property, context)) {
3036
yield property.key;
3137
}
3238
}

test/no-thenable.mjs

+10
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ test.snapshot({
1313
'const foo = {[then]: 1}',
1414
'const NOT_THEN = "no-then";const foo = {[NOT_THEN]: 1}',
1515
'function foo({then}) {}',
16+
'({[Symbol.prototype]: 1})',
1617

1718
// `class`
1819
'class then {}',
@@ -35,6 +36,11 @@ test.snapshot({
3536
'class Foo {static get #then() {}}',
3637
'class Foo {static get [then]() {}}',
3738
'class Foo {notThen = then}',
39+
'class Foo {[Symbol.property]}',
40+
'class Foo {static [Symbol.property]}',
41+
'class Foo {get [Symbol.property]() {}}',
42+
'class Foo {[Symbol.property]() {}}',
43+
'class Foo {static get [Symbol.property]() {}}',
3844

3945
// Assign
4046
'foo[then] = 1',
@@ -46,6 +52,7 @@ test.snapshot({
4652
'delete foo.then',
4753
'typeof foo.then',
4854
'foo.then != 1',
55+
'foo[Symbol.property] = 1',
4956

5057
// `Object.fromEntries`
5158
'Object.fromEntries([then, 1])',
@@ -60,6 +67,7 @@ test.snapshot({
6067
'Object.fromEntries([[..."then", 1]])',
6168
'Object.fromEntries([["then", 1]], extraArgument)',
6269
'Object.fromEntries(...[["then", 1]])',
70+
'Object.fromEntries([[Symbol.property, 1]])',
6371

6472
// `{Object,Reflect}.defineProperty`
6573
'Object.defineProperty(foo, then, 1)',
@@ -71,6 +79,8 @@ test.snapshot({
7179
'Object.defineProperty(foo, "then", )',
7280
'Object.defineProperty(...foo, "then", 1)',
7381
'Object.defineProperty(foo, ...["then", 1])',
82+
'Object.defineProperty(foo, Symbol.property, 1)',
83+
'Reflect.defineProperty(foo, Symbol.property, 1)',
7484

7585
// `export`
7686
'export {default} from "then"',

0 commit comments

Comments
 (0)