Skip to content

Commit

Permalink
Chore: use messageId in rule wrap-iife
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Aug 19, 2018
1 parent a7544e6 commit 9959b8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
13 changes: 9 additions & 4 deletions lib/rules/wrap-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ module.exports = {
}
],

fixable: "code"
fixable: "code",
messages: {
wrapInvocation: "Wrap an immediate function invocation in parentheses.",
wrapExpression: "Wrap only the function expression in parens.",
moveInvocation: "Move the invocation into the parens that contain the function."
}
},

create(context) {
Expand Down Expand Up @@ -97,7 +102,7 @@ module.exports = {
if (!callExpressionWrapped && !functionExpressionWrapped) {
context.report({
node,
message: "Wrap an immediate function invocation in parentheses.",
messageId: "wrapInvocation",
fix(fixer) {
const nodeToSurround = style === "inside" ? innerNode : node;

Expand All @@ -107,7 +112,7 @@ module.exports = {
} else if (style === "inside" && !functionExpressionWrapped) {
context.report({
node,
message: "Wrap only the function expression in parens.",
messageId: "WrapExpression",
fix(fixer) {

/*
Expand All @@ -127,7 +132,7 @@ module.exports = {
} else if (style === "outside" && !callExpressionWrapped) {
context.report({
node,
message: "Move the invocation into the parens that contain the function.",
messageId: "moveInvocation",
fix(fixer) {

/*
Expand Down
35 changes: 19 additions & 16 deletions tests/lib/rules/wrap-iife.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ const rule = require("../../../lib/rules/wrap-iife"),
// Tests
//------------------------------------------------------------------------------


const ruleTester = new RuleTester();

const wrapInvocationError = { messageId: "wrapInvocation", type: "CallExpression" };
const wrapExpressionError = { messageId: "wrapExpression", type: "CallExpression" };
const moveInvocationError = { messageId: "moveInvocation", type: "CallExpression" };

ruleTester.run("wrap-iife", rule, {
valid: [
{
Expand Down Expand Up @@ -110,90 +113,90 @@ ruleTester.run("wrap-iife", rule, {
{
code: "0, function(){ }();",
output: "0, (function(){ }());",
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "[function(){ }()];",
output: "[(function(){ }())];",
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "var a = function(){ }();",
output: "var a = (function(){ }());",
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "(function(){ }(), 0);",
output: "((function(){ }()), 0);",
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "(function a(){ })();",
output: "(function a(){ }());",
options: ["outside"],
errors: [{ message: "Move the invocation into the parens that contain the function.", type: "CallExpression" }]
errors: [moveInvocationError]
},
{
code: "(function a(){ }());",
output: "(function a(){ })();",
options: ["inside"],
errors: [{ message: "Wrap only the function expression in parens.", type: "CallExpression" }]
errors: [wrapExpressionError]
},
{

// Ensure all comments get preserved when autofixing.
code: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ } /* i */ ( /* j */ baz /* k */) /* l */ ) /* m */ ;",
output: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ }) /* i */ ( /* j */ baz /* k */) /* l */ /* m */ ;",
options: ["inside"],
errors: [{ message: "Wrap only the function expression in parens.", type: "CallExpression" }]
errors: [wrapExpressionError]
},
{
code: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ } /* i */ ) /* j */ ( /* k */ baz /* l */) /* m */ ;",
output: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ } /* i */ /* j */ ( /* k */ baz /* l */)) /* m */ ;",
options: ["outside"],
errors: [{ message: "Move the invocation into the parens that contain the function.", type: "CallExpression" }]
errors: [moveInvocationError]
},
{
code: "+function(){return 1;}()",
output: "+(function(){return 1;}())",
options: ["outside"],
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "+function(){return 1;}()",
output: "+(function(){return 1;})()",
options: ["inside"],
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "window.bar = function() { return 3; }.call(this, arg1);",
output: "window.bar = (function() { return 3; }).call(this, arg1);",
options: ["inside", { functionPrototypeMethods: true }],
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "window.bar = function() { return 3; }['call'](this, arg1);",
output: "window.bar = (function() { return 3; })['call'](this, arg1);",
options: ["inside", { functionPrototypeMethods: true }],
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "window.bar = function() { return 3; }.call(this, arg1);",
output: "window.bar = (function() { return 3; }.call(this, arg1));",
options: ["outside", { functionPrototypeMethods: true }],
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
errors: [wrapInvocationError]
},
{
code: "window.bar = (function() { return 3; }.call(this, arg1));",
output: "window.bar = (function() { return 3; }).call(this, arg1);",
options: ["inside", { functionPrototypeMethods: true }],
errors: [{ message: "Wrap only the function expression in parens.", type: "CallExpression" }]
errors: [wrapExpressionError]
},
{
code: "window.bar = (function() { return 3; }).call(this, arg1);",
output: "window.bar = (function() { return 3; }.call(this, arg1));",
options: ["outside", { functionPrototypeMethods: true }],
errors: [{ message: "Move the invocation into the parens that contain the function.", type: "CallExpression" }]
errors: [moveInvocationError]
}
]
});

0 comments on commit 9959b8f

Please sign in to comment.