Skip to content

Commit

Permalink
no-thenable: Fix crash on {[Symbol.prototype]: 0} (#2248)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Jan 4, 2024
1 parent 3b504fa commit 3c7d7c0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
14 changes: 10 additions & 4 deletions rules/no-thenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ const messages = {

const isStringThen = (node, context) =>
getStaticValue(node, context.sourceCode.getScope(node))?.value === 'then';
const isPropertyThen = (node, context) => {
// `getPropertyName` throws on `({[Symbol.prototype]: 0})`
// https://github.com/eslint-community/eslint-utils/pull/182
try {
return getPropertyName(node, context.sourceCode.getScope(node)) === 'then';
} catch {}

return false;
};

const cases = [
// `{then() {}}`,
Expand All @@ -23,10 +32,7 @@ const cases = [
selector: 'ObjectExpression',
* getNodes(node, context) {
for (const property of node.properties) {
if (
property.type === 'Property'
&& getPropertyName(property, context.sourceCode.getScope(property)) === 'then'
) {
if (property.type === 'Property' && isPropertyThen(property, context)) {
yield property.key;
}
}
Expand Down
10 changes: 10 additions & 0 deletions test/no-thenable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test.snapshot({
'const foo = {[then]: 1}',
'const NOT_THEN = "no-then";const foo = {[NOT_THEN]: 1}',
'function foo({then}) {}',
'({[Symbol.prototype]: 1})',

// `class`
'class then {}',
Expand All @@ -35,6 +36,11 @@ test.snapshot({
'class Foo {static get #then() {}}',
'class Foo {static get [then]() {}}',
'class Foo {notThen = then}',
'class Foo {[Symbol.property]}',
'class Foo {static [Symbol.property]}',
'class Foo {get [Symbol.property]() {}}',
'class Foo {[Symbol.property]() {}}',
'class Foo {static get [Symbol.property]() {}}',

// Assign
'foo[then] = 1',
Expand All @@ -46,6 +52,7 @@ test.snapshot({
'delete foo.then',
'typeof foo.then',
'foo.then != 1',
'foo[Symbol.property] = 1',

// `Object.fromEntries`
'Object.fromEntries([then, 1])',
Expand All @@ -60,6 +67,7 @@ test.snapshot({
'Object.fromEntries([[..."then", 1]])',
'Object.fromEntries([["then", 1]], extraArgument)',
'Object.fromEntries(...[["then", 1]])',
'Object.fromEntries([[Symbol.property, 1]])',

// `{Object,Reflect}.defineProperty`
'Object.defineProperty(foo, then, 1)',
Expand All @@ -71,6 +79,8 @@ test.snapshot({
'Object.defineProperty(foo, "then", )',
'Object.defineProperty(...foo, "then", 1)',
'Object.defineProperty(foo, ...["then", 1])',
'Object.defineProperty(foo, Symbol.property, 1)',
'Reflect.defineProperty(foo, Symbol.property, 1)',

// `export`
'export {default} from "then"',
Expand Down

0 comments on commit 3c7d7c0

Please sign in to comment.