Skip to content

Commit f536234

Browse files
committed
add no-amd-name fixer
1 parent 213b0d5 commit f536234

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

lib/rules/no-amd-name.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,34 @@
1111

1212
module.exports = {
1313
meta: {
14-
type: 'problem',
14+
type: 'suggestion',
1515
messages: {
1616
noModuleName: 'No AMD module name should be provided'
17-
}
17+
},
18+
fixable: 'code'
1819
},
1920

2021
create: function(context) {
2122
return {
2223
'CallExpression[callee.name=define]': function(node) {
24+
if (node.arguments.length !== 3) {
25+
return;
26+
}
27+
28+
const arg1 = node.arguments[0];
29+
const arg2 = node.arguments[1];
30+
2331
if (
24-
node.arguments[0].type === 'Literal' &&
25-
typeof node.arguments[0].value === 'string'
32+
arg1 &&
33+
arg1.type === 'Literal' &&
34+
typeof arg1.value === 'string'
2635
) {
2736
context.report({
28-
node: node.arguments[0],
29-
messageId: 'noModuleName'
37+
node: arg1,
38+
messageId: 'noModuleName',
39+
fix: function(fixer) {
40+
return fixer.replaceTextRange([arg1.start, arg2.start], '');
41+
}
3042
});
3143
}
3244
}

tests/rules/no-amd-name.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ ruleTester.run('no-amd-name', rule, {
2929
},
3030
{
3131
code: 'define(["N/search"], function(search) {});'
32+
},
33+
{
34+
code: 'define("moduleName");'
35+
},
36+
{
37+
code: 'define("moduleName", ["N/search"]);'
3238
}
3339
],
3440

3541
invalid: [
3642
{
3743
code: 'define("moduleName", [], function() {});',
38-
errors: [{ messageId: 'noModuleName' }]
44+
errors: [{ messageId: 'noModuleName' }],
45+
output: 'define([], function() {});'
3946
},
4047
{
4148
code: 'define("moduleName", ["N/search"], function(search) {});',
42-
errors: [{ messageId: 'noModuleName' }]
43-
},
44-
{
45-
code: 'define("moduleName");',
46-
errors: [{ messageId: 'noModuleName' }]
47-
},
48-
{
49-
code: 'define("moduleName", ["N/search"]);',
50-
errors: [{ messageId: 'noModuleName' }]
49+
errors: [{ messageId: 'noModuleName' }],
50+
output: 'define(["N/search"], function(search) {});'
5151
}
5252
]
5353
});

0 commit comments

Comments
 (0)