Skip to content

Commit

Permalink
no-thenable: Fix Object.fromEntries() check (#2130)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed May 17, 2023
1 parent f10f1a6 commit f3265b9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 55 deletions.
36 changes: 21 additions & 15 deletions rules/no-thenable.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,35 @@ const cases = [
},
messageId: MESSAGE_ID_OBJECT,
},
// TODO[@fisker]: Bug, we are checking wrong pattern `Object.fromEntries(['then', …])`
// `Object.fromEntries([['then', …]])`
{
selector: 'CallExpression',
* getNodes(node, context) {
if (!isMethodCall(node, {
object: 'Object',
method: 'fromEntries',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})) {
if (!(
isMethodCall(node, {
object: 'Object',
method: 'fromEntries',
argumentsLength: 1,
optionalCall: false,
optionalMember: false,
})
&& node.arguments[0].type === 'ArrayExpression'
)) {
return;
}

const [firstArgument] = node.arguments;
if (firstArgument.type !== 'ArrayExpression') {
return;
}
for (const pairs of node.arguments[0].elements) {
if (
pairs?.type === 'ArrayExpression'
&& pairs.elements[0]
&& pairs.elements[0].type !== 'SpreadElement'
) {
const [key] = pairs.elements;

const [firstElement] = firstArgument.elements;
if (isStringThen(firstElement, context)) {
yield firstElement;
if (isStringThen(key, context)) {
yield key;
}
}
}
},
messageId: MESSAGE_ID_OBJECT,
Expand Down
22 changes: 17 additions & 5 deletions test/no-thenable.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@ test.snapshot({

// `Object.fromEntries`
'Object.fromEntries([then, 1])',
'Object.fromEntries(["notThen", "then"])',
'const NOT_THEN = "not-then";Object.fromEntries([NOT_THEN, 1])',
'Object.fromEntries([,,])',
'Object.fromEntries([[,,],[]])',
'const NOT_THEN = "not-then";Object.fromEntries([[NOT_THEN, 1]])',
'Object.fromEntries([[["then", 1]]])',
'NotObject.fromEntries([["then", 1]])',
'Object.notFromEntries([["then", 1]])',
'Object.fromEntries?.([["then", 1]])',
'Object?.fromEntries([["then", 1]])',
'Object.fromEntries([[..."then", 1]])',
'Object.fromEntries([["then", 1]], extraArgument)',
'Object.fromEntries(...[["then", 1]])',

// `{Object,Reflect}.defineProperty`
'Object.defineProperty(foo, then, 1)',
Expand Down Expand Up @@ -143,9 +152,11 @@ test.snapshot({
'const THEN = "then";Reflect.defineProperty(foo, THEN, 1)',

// `Object.fromEntries`
'Object.fromEntries(["then", 1])',
'Object.fromEntries([`then`, 1])',
'const THEN = "then";Object.fromEntries([THEN, 1])',
'Object.fromEntries([["then", 1]])',
'Object.fromEntries([["then"]])',
'Object.fromEntries([[`then`, 1]])',
'const THEN = "then";Object.fromEntries([[THEN, 1]])',
'Object.fromEntries([foo, ["then", 1]])',

// `export`
'const then = 1; export {then}',
Expand Down Expand Up @@ -177,5 +188,6 @@ test.snapshot({
'export let {foo, ...then} = 1',
'export var {foo, ...then} = 1',
'export const {foo: {bar: [{baz: then}]}} = 1',
'export const notThen = 1, then = 1',
],
});

0 comments on commit f3265b9

Please sign in to comment.