Skip to content

Commit fc6ee39

Browse files
BridgeARaddaleax
authored andcommitted
tools: fix custom eslint rule errors
This fixes a few rules by making sure the input is actually ready to be checked. Otherwise those can throw TypeErrors or result in faulty error messages. PR-URL: #18853 Reviewed-By: Luigi Pinca <[email protected]>
1 parent 38797b5 commit fc6ee39

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

tools/eslint-rules/alphabetize-errors.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
'use strict';
22

3+
const { isDefiningError } = require('./rules-utils.js');
4+
35
const prefix = 'Out of ASCIIbetical order - ';
46
const opStr = ' >= ';
57

68
function errorForNode(node) {
79
return node.expression.arguments[0].value;
810
}
911

10-
function isDefiningError(node) {
11-
return node.expression &&
12-
node.expression.type === 'CallExpression' &&
13-
node.expression.callee &&
14-
node.expression.callee.name === 'E';
15-
}
16-
1712
module.exports = {
1813
create: function(context) {
1914
let previousNode;

tools/eslint-rules/documented-errors.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const fs = require('fs');
44
const path = require('path');
5+
const { isDefiningError } = require('./rules-utils.js');
56

67
const doc = fs.readFileSync(path.resolve(__dirname, '../../doc/api/errors.md'),
78
'utf8');
@@ -18,18 +19,11 @@ function errorForNode(node) {
1819
return node.expression.arguments[0].value;
1920
}
2021

21-
function isDefiningError(node) {
22-
return node.expression &&
23-
node.expression.type === 'CallExpression' &&
24-
node.expression.callee &&
25-
node.expression.callee.name === 'E';
26-
}
27-
2822
module.exports = {
2923
create: function(context) {
3024
return {
3125
ExpressionStatement: function(node) {
32-
if (!isDefiningError(node)) return;
26+
if (!isDefiningError(node) || !errorForNode(node)) return;
3327
const code = errorForNode(node);
3428
if (!isInDoc(code)) {
3529
const message = `"${code}" is not documented in doc/api/errors.md`;

tools/eslint-rules/prefer-util-format-errors.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const { isDefiningError } = require('./rules-utils.js');
4+
35
const errMsg = 'Please use a printf-like formatted string that util.format' +
46
' can consume.';
57

@@ -8,18 +10,11 @@ function isArrowFunctionWithTemplateLiteral(node) {
810
node.body.type === 'TemplateLiteral';
911
}
1012

11-
function isDefiningError(node) {
12-
return node.expression &&
13-
node.expression.type === 'CallExpression' &&
14-
node.expression.callee &&
15-
node.expression.callee.name === 'E';
16-
}
17-
1813
module.exports = {
1914
create: function(context) {
2015
return {
2116
ExpressionStatement: function(node) {
22-
if (!isDefiningError(node))
17+
if (!isDefiningError(node) || node.expression.arguments.length < 2)
2318
return;
2419

2520
const msg = node.expression.arguments[1];

tools/eslint-rules/rules-utils.js

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
*/
44
'use strict';
55

6+
module.exports.isDefiningError = function(node) {
7+
return node.expression &&
8+
node.expression.type === 'CallExpression' &&
9+
node.expression.callee &&
10+
node.expression.callee.name === 'E' &&
11+
node.expression.arguments.length !== 0;
12+
};
13+
614
/**
715
* Returns true if any of the passed in modules are used in
816
* require calls.

0 commit comments

Comments
 (0)