Skip to content

Commit 5c54d94

Browse files
committed
Use arrow functions
1 parent 0cc308f commit 5c54d94

File tree

10 files changed

+205
-225
lines changed

10 files changed

+205
-225
lines changed

lib/rules/api-version.js

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,43 @@ module.exports = {
1313
},
1414
},
1515

16-
create: function (context) {
17-
return {
18-
Program: function () {
19-
const sourceCode = context.getSourceCode();
20-
const comment = sourceCode
21-
.getAllComments()
22-
.find((node) => VERSION_TAG_REGEX.test(node.value));
16+
create: (context) => ({
17+
Program: () => {
18+
const sourceCode = context.getSourceCode();
19+
const comment = sourceCode
20+
.getAllComments()
21+
.find((node) => VERSION_TAG_REGEX.test(node.value));
2322

24-
if (!comment || comment.type !== 'Block') {
25-
return;
26-
}
23+
if (!comment || comment.type !== 'Block') {
24+
return;
25+
}
2726

28-
const version = comment.value.match(VERSION_TAG_REGEX)[1];
29-
const commentIndex = sourceCode.getIndexFromLoc(comment.loc.start) + 1;
30-
const tagIndex = commentIndex + comment.value.indexOf(VERSION_TAG) + 1;
27+
const version = comment.value.match(VERSION_TAG_REGEX)[1];
28+
const commentIndex = sourceCode.getIndexFromLoc(comment.loc.start) + 1;
29+
const tagIndex = commentIndex + comment.value.indexOf(VERSION_TAG) + 1;
3130

32-
if (!version) {
33-
context.report({
34-
messageId: 'noValue',
35-
loc: {
36-
start: sourceCode.getLocFromIndex(tagIndex),
37-
end: sourceCode.getLocFromIndex(tagIndex + VERSION_TAG.length),
38-
},
39-
});
40-
} else if (!VERSIONS.includes(version)) {
41-
const typeIndex = tagIndex + VERSION_TAG.length + 1;
31+
if (!version) {
32+
context.report({
33+
messageId: 'noValue',
34+
loc: {
35+
start: sourceCode.getLocFromIndex(tagIndex),
36+
end: sourceCode.getLocFromIndex(tagIndex + VERSION_TAG.length),
37+
},
38+
});
39+
} else if (!VERSIONS.includes(version)) {
40+
const typeIndex = tagIndex + VERSION_TAG.length + 1;
4241

43-
context.report({
44-
messageId: 'invalidValue',
45-
data: {
46-
value: version,
47-
},
48-
loc: {
49-
start: sourceCode.getLocFromIndex(typeIndex),
50-
end: sourceCode.getLocFromIndex(typeIndex + version.length),
51-
},
52-
});
53-
}
54-
},
55-
};
56-
},
42+
context.report({
43+
messageId: 'invalidValue',
44+
data: {
45+
value: version,
46+
},
47+
loc: {
48+
start: sourceCode.getLocFromIndex(typeIndex),
49+
end: sourceCode.getLocFromIndex(typeIndex + version.length),
50+
},
51+
});
52+
}
53+
},
54+
}),
5755
};

lib/rules/entry-points.js

Lines changed: 55 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
const { getScriptType } = require('../util/metadata');
4-
const { scriptTypes } = require('../util/scriptTypes');
4+
const { scriptTypeMap, scriptTypes } = require('../util/scriptTypes');
55

66
module.exports = {
77
meta: {
@@ -11,75 +11,71 @@ module.exports = {
1111
},
1212
},
1313

14-
create: function (context) {
15-
return {
16-
'CallExpression[callee.name=define]': function (node) {
17-
let hasValidEntryPoint = false;
18-
let scriptType = getScriptType(context);
14+
create: (context) => ({
15+
'CallExpression[callee.name=define]': (node) => {
16+
let hasValidEntryPoint = false;
17+
let scriptType = getScriptType(context);
1918

20-
scriptType = scriptType && scriptType.value;
19+
scriptType = scriptType && scriptType.value;
2120

22-
if (!scriptType || !scriptTypes.includes(scriptType)) {
23-
return;
24-
}
25-
26-
const argsCount = node.arguments.length;
27-
const callback = node.arguments[argsCount - 1];
28-
29-
if (!callback || callback.type !== 'FunctionExpression') {
30-
return;
31-
}
21+
if (!scriptType || !scriptTypes.includes(scriptType)) {
22+
return;
23+
}
3224

33-
const callbackBody = callback.body.body;
34-
const returnStatement =
35-
callbackBody.length > 0 &&
36-
callbackBody.find((n) => n.type === 'ReturnStatement');
37-
const returnArgument = returnStatement && returnStatement.argument;
25+
const argsCount = node.arguments.length;
26+
const callback = node.arguments[argsCount - 1];
3827

39-
if (returnArgument && returnArgument.type === 'ObjectExpression') {
40-
for (const property of returnArgument.properties) {
41-
if (
42-
scriptTypeMap[scriptType].entryPoints.includes(property.key.name) &&
43-
(property.value.type === 'Identifier' ||
44-
property.value.type === 'FunctionExpression')
45-
) {
46-
hasValidEntryPoint = true;
47-
break;
48-
}
49-
}
50-
}
28+
if (!callback || callback.type !== 'FunctionExpression') {
29+
return;
30+
}
5131

52-
if (returnArgument && returnArgument.type === 'Identifier') {
53-
const returnAssignments = callbackBody.filter(function (n) {
54-
return (
55-
n.type === 'ExpressionStatement' &&
56-
n.expression.left.type === 'MemberExpression' &&
57-
n.expression.left.object.name === returnArgument.name &&
58-
scriptTypeMap[scriptType].entryPoints.includes(
59-
n.expression.left.property.name
60-
)
61-
);
62-
});
32+
const callbackBody = callback.body.body;
33+
const returnStatement =
34+
callbackBody.length > 0 && callbackBody.find((n) => n.type === 'ReturnStatement');
35+
const returnArgument = returnStatement && returnStatement.argument;
6336

64-
if (returnAssignments.length > 0) {
37+
if (returnArgument && returnArgument.type === 'ObjectExpression') {
38+
for (const property of returnArgument.properties) {
39+
if (
40+
scriptTypeMap[scriptType].entryPoints.includes(property.key.name) &&
41+
(property.value.type === 'Identifier' ||
42+
property.value.type === 'FunctionExpression')
43+
) {
6544
hasValidEntryPoint = true;
45+
break;
6646
}
6747
}
48+
}
6849

69-
if (scriptTypeMap[scriptType].entryPoints.length === 0) {
50+
if (returnArgument && returnArgument.type === 'Identifier') {
51+
const returnAssignments = callbackBody.filter(
52+
(n) =>
53+
n.type === 'ExpressionStatement' &&
54+
n.expression.left.type === 'MemberExpression' &&
55+
n.expression.left.object.name === returnArgument.name &&
56+
scriptTypeMap[scriptType].entryPoints.includes(
57+
n.expression.left.property.name
58+
)
59+
);
60+
61+
if (returnAssignments.length > 0) {
7062
hasValidEntryPoint = true;
7163
}
64+
}
7265

73-
if (!hasValidEntryPoint) {
74-
context.report({
75-
node: returnStatement || callback,
76-
messageId: 'returnEntryPoint',
77-
data: {
78-
type: scriptType,
79-
},
80-
});
81-
}
82-
},
83-
};
84-
},
66+
if (scriptTypeMap[scriptType].entryPoints.length === 0) {
67+
hasValidEntryPoint = true;
68+
}
69+
70+
if (!hasValidEntryPoint) {
71+
context.report({
72+
node: returnStatement || callback,
73+
messageId: 'returnEntryPoint',
74+
data: {
75+
type: scriptType,
76+
},
77+
});
78+
}
79+
},
80+
}),
8581
};

lib/rules/log-args.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ module.exports = {
2828
],
2929
},
3030

31-
create: function (context) {
31+
create: (context) => {
3232
let logModule;
3333

3434
return {
35-
'CallExpression[callee.name=define]': function (node) {
35+
'CallExpression[callee.name=define]': (node) => {
3636
logModule = moduleUtil.getModuleNodePair(node, 'N/log');
3737
},
38-
'CallExpression[callee.object.type=Identifier]': function (node) {
38+
'CallExpression[callee.object.type=Identifier]': (node) => {
3939
let config = context.options[0] || { requireTitle: true, requireDetails: true };
4040
const args = node.arguments;
4141

lib/rules/module-vars.js

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,33 @@ module.exports = {
1818
],
1919
},
2020

21-
create: function (context) {
22-
return {
23-
'CallExpression[callee.name=define]': function (node) {
24-
if (!context.options[0]) return;
25-
26-
const config = context.options[0];
27-
const modules = moduleUtil.getModules(node);
28-
29-
if (modules.varCount === 0) return;
30-
31-
for (const module of modules.list) {
32-
const configVar = config[module.name];
33-
34-
if (configVar && configVar !== module.variable) {
35-
context.report({
36-
node: module.nodes.variable,
37-
messageId: 'useCorrectName',
38-
data: {
39-
module: module.name,
40-
id: configVar,
41-
},
42-
});
43-
}
21+
create: (context) => ({
22+
'CallExpression[callee.name=define]': (node) => {
23+
if (!context.options[0]) return;
24+
25+
const config = context.options[0];
26+
const modules = moduleUtil.getModules(node);
27+
28+
if (modules.varCount === 0) return;
29+
30+
for (const module of modules.list) {
31+
const configVar = config[module.name];
32+
33+
if (configVar && configVar !== module.variable) {
34+
context.report({
35+
node: module.nodes.variable,
36+
messageId: 'useCorrectName',
37+
data: {
38+
module: module.name,
39+
id: configVar,
40+
},
41+
});
4442
}
45-
},
46-
};
47-
},
43+
}
44+
},
45+
}),
4846
};
4947

50-
//--------------------
51-
// Helpers
52-
//--------------------
53-
5448
/**
5549
* Get schema properties from module names
5650
* @returns {Object}

lib/rules/no-amd-name.js

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ module.exports = {
99
fixable: 'code',
1010
},
1111

12-
create: function (context) {
13-
return {
14-
'CallExpression[callee.name=define]': function (node) {
15-
if (node.arguments.length !== 3) {
16-
return;
17-
}
12+
create: (context) => ({
13+
'CallExpression[callee.name=define]': (node) => {
14+
if (node.arguments.length !== 3) {
15+
return;
16+
}
1817

19-
const arg1 = node.arguments[0];
20-
const arg2 = node.arguments[1];
18+
const arg1 = node.arguments[0];
19+
const arg2 = node.arguments[1];
2120

22-
if (arg1 && arg1.type === 'Literal' && typeof arg1.value === 'string') {
23-
context.report({
24-
node: arg1,
25-
messageId: 'noModuleName',
26-
fix: function (fixer) {
27-
return fixer.replaceTextRange([arg1.range[0], arg2.range[0]], '');
28-
},
29-
});
30-
}
31-
},
32-
};
33-
},
21+
if (arg1 && arg1.type === 'Literal' && typeof arg1.value === 'string') {
22+
context.report({
23+
node: arg1,
24+
messageId: 'noModuleName',
25+
fix: function (fixer) {
26+
return fixer.replaceTextRange([arg1.range[0], arg2.range[0]], '');
27+
},
28+
});
29+
}
30+
},
31+
}),
3432
};

0 commit comments

Comments
 (0)