Skip to content

Commit

Permalink
update prefer-spread
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jun 18, 2020
1 parent 651bac3 commit a3cbe6e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
8 changes: 2 additions & 6 deletions lib/rules/prefer-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,13 @@ const astUtils = require("./utils/ast-utils");
*/
function isVariadicApplyCalling(node) {
return (
node.callee.type === "MemberExpression" &&
node.callee.property.type === "Identifier" &&
node.callee.property.name === "apply" &&
node.callee.computed === false &&
astUtils.isSpecificMemberAccess(node.callee, null, "apply") &&
node.arguments.length === 2 &&
node.arguments[1].type !== "ArrayExpression" &&
node.arguments[1].type !== "SpreadElement"
);
}


/**
* Checks whether or not `thisArg` is not changed by `.apply()`.
* @param {ASTNode|null} expectedThis The node that is the owner of the applied function.
Expand Down Expand Up @@ -75,7 +71,7 @@ module.exports = {
return;
}

const applied = node.callee.object;
const applied = astUtils.skipChainExpression(astUtils.skipChainExpression(node.callee).object);
const expectedThis = (applied.type === "MemberExpression") ? applied.object : null;
const thisArg = node.arguments[0];

Expand Down
46 changes: 44 additions & 2 deletions tests/lib/rules/prefer-spread.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const { RuleTester } = require("../../../lib/rule-tester");

const errors = [{ messageId: "preferSpread", type: "CallExpression" }];

const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2020 } });

ruleTester.run("prefer-spread", rule, {
valid: [
Expand All @@ -39,7 +39,11 @@ ruleTester.run("prefer-spread", rule, {
// ignores incomplete things.
"foo.apply();",
"obj.foo.apply();",
"obj.foo.apply(obj, ...args)"
"obj.foo.apply(obj, ...args)",

// Optional chaining
"(a?.b).c.foo.apply(a?.b.c, args);",
"a?.b.c.foo.apply((a?.b).c, args);"
],
invalid: [
{
Expand Down Expand Up @@ -73,6 +77,44 @@ ruleTester.run("prefer-spread", rule, {
{
code: "[].concat.apply([\n/*empty*/\n], args);",
errors
},

// Optional chaining
{
code: "foo.apply?.(undefined, args);",
errors
},
{
code: "foo?.apply(undefined, args);",
errors
},
{
code: "foo?.apply?.(undefined, args);",
errors
},
{
code: "(foo?.apply)(undefined, args);",
errors
},
{
code: "(foo?.apply)?.(undefined, args);",
errors
},
{
code: "(obj?.foo).apply(obj, args);",
errors
},
{
code: "a?.b.c.foo.apply(a?.b.c, args);",
errors
},
{
code: "(a?.b.c).foo.apply(a?.b.c, args);",
errors
},
{
code: "(a?.b).c.foo.apply((a?.b).c, args);",
errors
}
]
});

0 comments on commit a3cbe6e

Please sign in to comment.