123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- "use strict";
- function isForStatementUpdate(node) {
- const parent = node.parent;
- return parent.type === "ForStatement" && parent.update === node;
- }
- function isForLoopAfterthought(node) {
- const parent = node.parent;
- if (parent.type === "SequenceExpression") {
- return isForLoopAfterthought(parent);
- }
- return isForStatementUpdate(node);
- }
- module.exports = {
- meta: {
- type: "suggestion",
- defaultOptions: [{
- allowForLoopAfterthoughts: false
- }],
- docs: {
- description: "Disallow the unary operators `++` and `--`",
- recommended: false,
- url: "https://eslint.org/docs/latest/rules/no-plusplus"
- },
- schema: [
- {
- type: "object",
- properties: {
- allowForLoopAfterthoughts: {
- type: "boolean"
- }
- },
- additionalProperties: false
- }
- ],
- messages: {
- unexpectedUnaryOp: "Unary operator '{{operator}}' used."
- }
- },
- create(context) {
- const [{ allowForLoopAfterthoughts }] = context.options;
- return {
- UpdateExpression(node) {
- if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) {
- return;
- }
- context.report({
- node,
- messageId: "unexpectedUnaryOp",
- data: {
- operator: node.operator
- }
- });
- }
- };
- }
- };
|