Skip to content

Commit 9959b8f

Browse files
committed
Chore: use messageId in rule wrap-iife
1 parent a7544e6 commit 9959b8f

2 files changed

Lines changed: 28 additions & 20 deletions

File tree

lib/rules/wrap-iife.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ module.exports = {
3939
}
4040
],
4141

42-
fixable: "code"
42+
fixable: "code",
43+
messages: {
44+
wrapInvocation: "Wrap an immediate function invocation in parentheses.",
45+
wrapExpression: "Wrap only the function expression in parens.",
46+
moveInvocation: "Move the invocation into the parens that contain the function."
47+
}
4348
},
4449

4550
create(context) {
@@ -97,7 +102,7 @@ module.exports = {
97102
if (!callExpressionWrapped && !functionExpressionWrapped) {
98103
context.report({
99104
node,
100-
message: "Wrap an immediate function invocation in parentheses.",
105+
messageId: "wrapInvocation",
101106
fix(fixer) {
102107
const nodeToSurround = style === "inside" ? innerNode : node;
103108

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

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

133138
/*

tests/lib/rules/wrap-iife.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ const rule = require("../../../lib/rules/wrap-iife"),
1616
// Tests
1717
//------------------------------------------------------------------------------
1818

19-
2019
const ruleTester = new RuleTester();
2120

21+
const wrapInvocationError = { messageId: "wrapInvocation", type: "CallExpression" };
22+
const wrapExpressionError = { messageId: "wrapExpression", type: "CallExpression" };
23+
const moveInvocationError = { messageId: "moveInvocation", type: "CallExpression" };
24+
2225
ruleTester.run("wrap-iife", rule, {
2326
valid: [
2427
{
@@ -110,90 +113,90 @@ ruleTester.run("wrap-iife", rule, {
110113
{
111114
code: "0, function(){ }();",
112115
output: "0, (function(){ }());",
113-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
116+
errors: [wrapInvocationError]
114117
},
115118
{
116119
code: "[function(){ }()];",
117120
output: "[(function(){ }())];",
118-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
121+
errors: [wrapInvocationError]
119122
},
120123
{
121124
code: "var a = function(){ }();",
122125
output: "var a = (function(){ }());",
123-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
126+
errors: [wrapInvocationError]
124127
},
125128
{
126129
code: "(function(){ }(), 0);",
127130
output: "((function(){ }()), 0);",
128-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
131+
errors: [wrapInvocationError]
129132
},
130133
{
131134
code: "(function a(){ })();",
132135
output: "(function a(){ }());",
133136
options: ["outside"],
134-
errors: [{ message: "Move the invocation into the parens that contain the function.", type: "CallExpression" }]
137+
errors: [moveInvocationError]
135138
},
136139
{
137140
code: "(function a(){ }());",
138141
output: "(function a(){ })();",
139142
options: ["inside"],
140-
errors: [{ message: "Wrap only the function expression in parens.", type: "CallExpression" }]
143+
errors: [wrapExpressionError]
141144
},
142145
{
143146

144147
// Ensure all comments get preserved when autofixing.
145148
code: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ } /* i */ ( /* j */ baz /* k */) /* l */ ) /* m */ ;",
146149
output: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ }) /* i */ ( /* j */ baz /* k */) /* l */ /* m */ ;",
147150
options: ["inside"],
148-
errors: [{ message: "Wrap only the function expression in parens.", type: "CallExpression" }]
151+
errors: [wrapExpressionError]
149152
},
150153
{
151154
code: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ } /* i */ ) /* j */ ( /* k */ baz /* l */) /* m */ ;",
152155
output: "( /* a */ function /* b */ foo /* c */ ( /* d */ bar /* e */ ) /* f */ { /* g */ return; /* h */ } /* i */ /* j */ ( /* k */ baz /* l */)) /* m */ ;",
153156
options: ["outside"],
154-
errors: [{ message: "Move the invocation into the parens that contain the function.", type: "CallExpression" }]
157+
errors: [moveInvocationError]
155158
},
156159
{
157160
code: "+function(){return 1;}()",
158161
output: "+(function(){return 1;}())",
159162
options: ["outside"],
160-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
163+
errors: [wrapInvocationError]
161164
},
162165
{
163166
code: "+function(){return 1;}()",
164167
output: "+(function(){return 1;})()",
165168
options: ["inside"],
166-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
169+
errors: [wrapInvocationError]
167170
},
168171
{
169172
code: "window.bar = function() { return 3; }.call(this, arg1);",
170173
output: "window.bar = (function() { return 3; }).call(this, arg1);",
171174
options: ["inside", { functionPrototypeMethods: true }],
172-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
175+
errors: [wrapInvocationError]
173176
},
174177
{
175178
code: "window.bar = function() { return 3; }['call'](this, arg1);",
176179
output: "window.bar = (function() { return 3; })['call'](this, arg1);",
177180
options: ["inside", { functionPrototypeMethods: true }],
178-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
181+
errors: [wrapInvocationError]
179182
},
180183
{
181184
code: "window.bar = function() { return 3; }.call(this, arg1);",
182185
output: "window.bar = (function() { return 3; }.call(this, arg1));",
183186
options: ["outside", { functionPrototypeMethods: true }],
184-
errors: [{ message: "Wrap an immediate function invocation in parentheses.", type: "CallExpression" }]
187+
errors: [wrapInvocationError]
185188
},
186189
{
187190
code: "window.bar = (function() { return 3; }.call(this, arg1));",
188191
output: "window.bar = (function() { return 3; }).call(this, arg1);",
189192
options: ["inside", { functionPrototypeMethods: true }],
190-
errors: [{ message: "Wrap only the function expression in parens.", type: "CallExpression" }]
193+
errors: [wrapExpressionError]
191194
},
192195
{
193196
code: "window.bar = (function() { return 3; }).call(this, arg1);",
194197
output: "window.bar = (function() { return 3; }.call(this, arg1));",
195198
options: ["outside", { functionPrototypeMethods: true }],
196-
errors: [{ message: "Move the invocation into the parens that contain the function.", type: "CallExpression" }]
199+
errors: [moveInvocationError]
197200
}
198201
]
199202
});

0 commit comments

Comments
 (0)