Skip to content

Commit 29dc51c

Browse files
Fix: check the type of the first arg of the old context.report() API
1 parent 45dac85 commit 29dc51c

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

Diff for: lib/rules/no-deprecated-report-api.js

+22-3
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,28 @@ module.exports = {
4747
fix (fixer) {
4848
const openingParen = sourceCode.getTokenBefore(node.arguments[0]);
4949
const closingParen = sourceCode.getLastToken(node);
50-
const keys = node.arguments.length >= 5
51-
? ['node', 'loc', 'message', 'data', 'fix']
52-
: ['node', 'message', 'data', 'fix'];
50+
51+
// If there is exactly one argument, the API expects an object.
52+
// Otherwise, if the second argument is a string, the arguments are interpreted as
53+
// ['node', 'message', 'data', 'fix'].
54+
// Otherwise, the arguments are interpreted as ['node', 'loc', 'message', 'data', 'fix'].
55+
56+
let keys;
57+
if (
58+
(node.arguments[1].type === 'Literal' && typeof node.arguments[1].value === 'string') ||
59+
node.arguments[1].type === 'TemplateLiteral'
60+
) {
61+
keys = ['node', 'message', 'data', 'fix'];
62+
} else if (
63+
node.arguments[1].type === 'ObjectExpression' ||
64+
node.arguments[1].type === 'ArrayExpression' ||
65+
(node.arguments[1].type === 'Literal' && typeof node.arguments[1].value !== 'string')
66+
) {
67+
keys = ['node', 'loc', 'message', 'data', 'fix'];
68+
} else {
69+
// Otherwise, we can't statically determine what argument means what, so no safe fix is possible.
70+
return null;
71+
}
5372

5473
return fixer.replaceTextRange(
5574
[openingParen.range[1], closingParen.range[0]],

Diff for: lib/rules/require-meta-fixable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545
contextIdentifiers.has(node.callee.object) &&
4646
node.callee.property.type === 'Identifier' &&
4747
node.callee.property.name === 'report' &&
48-
(node.arguments.length > 3 || (
48+
(node.arguments.length > 4 || (
4949
node.arguments.length === 1 &&
5050
node.arguments[0].type === 'ObjectExpression' &&
5151
node.arguments[0].properties.some(prop => utils.getKeyName(prop) === 'fix')

Diff for: tests/lib/rules/no-deprecated-report-api.js

+26-13
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,14 @@ ruleTester.run('no-deprecated-report-api', rule, {
103103
code: `
104104
module.exports = {
105105
create(context) {
106-
context.report(theNode, theLocation, theMessage, theData, theFix);
106+
context.report(theNode, { line: foo, column: bar }, theMessage, theData, theFix);
107107
}
108108
};
109109
`,
110110
output: `
111111
module.exports = {
112112
create(context) {
113-
context.report({node: theNode, loc: theLocation, message: theMessage, data: theData, fix: theFix});
113+
context.report({node: theNode, loc: { line: foo, column: bar }, message: theMessage, data: theData, fix: theFix});
114114
}
115115
};
116116
`,
@@ -120,14 +120,14 @@ ruleTester.run('no-deprecated-report-api', rule, {
120120
code: `
121121
module.exports = {
122122
create(context) {
123-
context.report(theNode, theMessage, theData, theFix);
123+
context.report(theNode, "theMessage", theData, theFix);
124124
}
125125
};
126126
`,
127127
output: `
128128
module.exports = {
129129
create(context) {
130-
context.report({node: theNode, message: theMessage, data: theData, fix: theFix});
130+
context.report({node: theNode, message: "theMessage", data: theData, fix: theFix});
131131
}
132132
};
133133
`,
@@ -144,7 +144,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
144144
output: `
145145
module.exports = {
146146
create(context) {
147-
context.report({node: theNode, message: theMessage, data: theData, fix: theFix});
147+
context.report(theNode, theMessage, theData, theFix);
148148
}
149149
};
150150
`,
@@ -154,14 +154,14 @@ ruleTester.run('no-deprecated-report-api', rule, {
154154
code: `
155155
module.exports = {
156156
create(context) {
157-
context.report(theNode, theMessage, theData);
157+
context.report(theNode, 'foo', theData);
158158
}
159159
};
160160
`,
161161
output: `
162162
module.exports = {
163163
create(context) {
164-
context.report({node: theNode, message: theMessage, data: theData});
164+
context.report({node: theNode, message: 'foo', data: theData});
165165
}
166166
};
167167
`,
@@ -171,14 +171,14 @@ ruleTester.run('no-deprecated-report-api', rule, {
171171
code: `
172172
module.exports = {
173173
create(context) {
174-
context.report(theNode, theMessage);
174+
context.report(theNode, 'foo');
175175
}
176176
};
177177
`,
178178
output: `
179179
module.exports = {
180180
create(context) {
181-
context.report({node: theNode, message: theMessage});
181+
context.report({node: theNode, message: 'foo'});
182182
}
183183
};
184184
`,
@@ -195,7 +195,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
195195
output: `
196196
module.exports = {
197197
create(notContext) {
198-
notContext.report({node: theNode, message: theMessage, data: theData, fix: theFix});
198+
notContext.report(theNode, theMessage, theData, theFix);
199199
}
200200
};
201201
`,
@@ -204,12 +204,25 @@ ruleTester.run('no-deprecated-report-api', rule, {
204204
{
205205
code: `
206206
module.exports.create = context => {
207-
context.report(theNode, theMessage, theData, theFix);
207+
context.report(theNode, \`blah\`, theData, theFix);
208208
};
209209
`,
210210
output: `
211211
module.exports.create = context => {
212-
context.report({node: theNode, message: theMessage, data: theData, fix: theFix});
212+
context.report({node: theNode, message: \`blah\`, data: theData, fix: theFix});
213+
};
214+
`,
215+
errors: [ERROR],
216+
},
217+
{
218+
code: `
219+
module.exports.create = context => {
220+
context.report(theNode, 5, foo, bar);
221+
};
222+
`,
223+
output: `
224+
module.exports.create = context => {
225+
context.report({node: theNode, loc: 5, message: foo, data: bar});
213226
};
214227
`,
215228
errors: [ERROR],
@@ -225,7 +238,7 @@ ruleTester.run('no-deprecated-report-api', rule, {
225238
output: `
226239
module.exports = {
227240
create(context) {
228-
context.report({node: theNode, loc: theLocation, message: theMessage, data: theData, fix: theFix});
241+
context.report(theNode, theLocation, theMessage, theData, theFix, somethingElse, somethingElse, somethingElse);
229242
}
230243
};
231244
`,

Diff for: tests/lib/rules/require-meta-fixable.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,22 @@ ruleTester.run('require-meta-fixable', rule, {
110110
`,
111111
errors: [MISSING_ERROR],
112112
},
113-
],
114-
115-
invalid: [
116113
{
117114
code: `
118115
module.exports = {
119116
meta: {},
120-
create(context) { context.report({node, message, fix: foo}); }
117+
create(context) { context.report(node, message, data, fix); }
121118
};
122119
`,
123-
errors: [MISSING_ERROR],
124120
},
121+
],
122+
123+
invalid: [
125124
{
126125
code: `
127126
module.exports = {
128127
meta: {},
129-
create(context) { context.report(node, message, data, fix); }
128+
create(context) { context.report({node, message, fix: foo}); }
130129
};
131130
`,
132131
errors: [MISSING_ERROR],

0 commit comments

Comments
 (0)