2017-08-25 18:15:43 -07:00
|
|
|
'use strict';
|
|
|
|
|
2018-02-18 20:19:20 +01:00
|
|
|
const { isDefiningError } = require('./rules-utils.js');
|
|
|
|
|
2017-09-23 17:41:04 -04:00
|
|
|
const prefix = 'Out of ASCIIbetical order - ';
|
|
|
|
const opStr = ' >= ';
|
2017-08-25 18:15:43 -07:00
|
|
|
|
|
|
|
function errorForNode(node) {
|
|
|
|
return node.expression.arguments[0].value;
|
|
|
|
}
|
|
|
|
|
2024-04-23 19:05:38 +02:00
|
|
|
const requireInternalErrorsSelector =
|
|
|
|
'VariableDeclarator' +
|
|
|
|
'[init.type="CallExpression"]' +
|
|
|
|
'[init.callee.name="require"]' +
|
|
|
|
'[init.arguments.length=1]' +
|
|
|
|
'[init.arguments.0.value="internal/errors"]';
|
|
|
|
const codesSelector = requireInternalErrorsSelector + '>*>Property[key.name="codes"]';
|
|
|
|
|
2017-08-25 18:15:43 -07:00
|
|
|
module.exports = {
|
2024-04-23 19:05:38 +02:00
|
|
|
meta: {
|
|
|
|
schema: [{
|
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
checkErrorDeclarations: { type: 'boolean' },
|
|
|
|
},
|
|
|
|
additionalProperties: false,
|
|
|
|
}],
|
|
|
|
},
|
2017-08-25 18:15:43 -07:00
|
|
|
create: function(context) {
|
2017-09-23 17:41:04 -04:00
|
|
|
let previousNode;
|
2017-08-25 18:15:43 -07:00
|
|
|
return {
|
|
|
|
ExpressionStatement: function(node) {
|
2024-04-23 19:05:38 +02:00
|
|
|
if (context.options.every((option) => option.checkErrorDeclarations !== true) || !isDefiningError(node)) return;
|
2017-09-23 17:41:04 -04:00
|
|
|
if (!previousNode) {
|
2017-08-25 18:15:43 -07:00
|
|
|
previousNode = node;
|
2017-09-23 17:41:04 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const prev = errorForNode(previousNode);
|
|
|
|
const curr = errorForNode(node);
|
|
|
|
previousNode = node;
|
|
|
|
if (prev >= curr) {
|
|
|
|
const message = [prefix, prev, opStr, curr].join('');
|
|
|
|
context.report({ node, message });
|
2017-08-25 18:15:43 -07:00
|
|
|
}
|
2022-12-18 17:39:39 +01:00
|
|
|
},
|
2024-04-23 19:05:38 +02:00
|
|
|
|
|
|
|
[requireInternalErrorsSelector](node) {
|
|
|
|
if (node.id.type !== 'ObjectPattern') {
|
|
|
|
context.report({ node, message: 'Use destructuring to define error codes used in the file' });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[codesSelector](node) {
|
|
|
|
if (node.value.type !== 'ObjectPattern') {
|
|
|
|
context.report({ node, message: 'Use destructuring to define error codes used in the file' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (node.value.loc.start.line === node.value.loc.end.line) {
|
|
|
|
context.report({ node, message: 'Use multiline destructuring for error codes' });
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
[requireInternalErrorsSelector + ' Property:not(:first-child)'](node) {
|
|
|
|
const { properties } = node.parent;
|
|
|
|
const prev = properties[properties.indexOf(node) - 1].key.name;
|
|
|
|
const curr = node.key.name;
|
|
|
|
if (prev >= curr) {
|
|
|
|
const message = [prefix, prev, opStr, curr].join('');
|
|
|
|
context.report({ node, message });
|
|
|
|
}
|
|
|
|
},
|
2017-08-25 18:15:43 -07:00
|
|
|
};
|
2022-12-18 17:39:39 +01:00
|
|
|
},
|
2017-08-25 18:15:43 -07:00
|
|
|
};
|