|
| 1 | +import type { Rule } from "eslint" |
| 2 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 3 | +import type { Quantifier } from "regexpp/ast" |
| 4 | +import type { RegExpContext } from "../utils" |
| 5 | +import { canUnwrapped, createRule, defineRegexpVisitor } from "../utils" |
| 6 | +import { isEmpty, isPotentiallyEmpty, isZeroLength } from "regexp-ast-analysis" |
| 7 | + |
| 8 | +export default createRule("no-useless-quantifier", { |
| 9 | + meta: { |
| 10 | + docs: { |
| 11 | + description: "disallow quantifiers that can be removed", |
| 12 | + // TODO Switch to recommended in the major version. |
| 13 | + // recommended: true, |
| 14 | + recommended: false, |
| 15 | + }, |
| 16 | + fixable: "code", |
| 17 | + schema: [], |
| 18 | + messages: { |
| 19 | + constOne: "Unexpected useless quantifier.", |
| 20 | + empty: |
| 21 | + "Unexpected useless quantifier. The quantified element doesn't consume or assert characters.", |
| 22 | + emptyQuestionMark: |
| 23 | + "Unexpected useless quantifier. The quantified element can already accept the empty string, so this quantifier is redundant.", |
| 24 | + zeroLength: |
| 25 | + "Unexpected useless quantifier. The quantified element doesn't consume characters.", |
| 26 | + |
| 27 | + // suggestions |
| 28 | + remove: "Remove the '{{quant}}' quantifier.", |
| 29 | + }, |
| 30 | + type: "suggestion", // "problem", |
| 31 | + }, |
| 32 | + create(context) { |
| 33 | + /** |
| 34 | + * Create visitor |
| 35 | + */ |
| 36 | + function createVisitor( |
| 37 | + regexpContext: RegExpContext, |
| 38 | + ): RegExpVisitor.Handlers { |
| 39 | + const { node, getRegexpLocation, fixReplaceNode } = regexpContext |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns a fix that replaces the given quantifier with its |
| 43 | + * quantified element |
| 44 | + */ |
| 45 | + function fixRemoveQuant(qNode: Quantifier) { |
| 46 | + return fixReplaceNode(qNode, () => { |
| 47 | + const text = qNode.element.raw |
| 48 | + return canUnwrapped(qNode, text) ? text : null |
| 49 | + }) |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns a suggestion that replaces the given quantifier with its |
| 54 | + * quantified element |
| 55 | + */ |
| 56 | + function suggestRemoveQuant( |
| 57 | + qNode: Quantifier, |
| 58 | + ): Rule.SuggestionReportDescriptor { |
| 59 | + const quant = qNode.raw.slice(qNode.element.end - qNode.start) |
| 60 | + |
| 61 | + return { |
| 62 | + messageId: "remove", |
| 63 | + data: { quant }, |
| 64 | + fix: fixReplaceNode(qNode, () => { |
| 65 | + const text = qNode.element.raw |
| 66 | + return canUnwrapped(qNode, text) ? text : null |
| 67 | + }), |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + onQuantifierEnter(qNode) { |
| 73 | + // trivial case |
| 74 | + // e.g. a{1} |
| 75 | + if (qNode.min === 1 && qNode.max === 1) { |
| 76 | + context.report({ |
| 77 | + node, |
| 78 | + loc: getRegexpLocation(qNode), |
| 79 | + messageId: "constOne", |
| 80 | + fix: fixRemoveQuant(qNode), |
| 81 | + }) |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + // the quantified element already accepts the empty string |
| 86 | + // e.g. (||)* |
| 87 | + if (isEmpty(qNode.element)) { |
| 88 | + context.report({ |
| 89 | + node, |
| 90 | + loc: getRegexpLocation(qNode), |
| 91 | + messageId: "empty", |
| 92 | + suggest: [suggestRemoveQuant(qNode)], |
| 93 | + }) |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + // the quantified element already accepts the empty string |
| 98 | + // e.g. (a?)? |
| 99 | + if ( |
| 100 | + qNode.min === 0 && |
| 101 | + qNode.max === 1 && |
| 102 | + qNode.greedy && |
| 103 | + isPotentiallyEmpty(qNode.element) |
| 104 | + ) { |
| 105 | + context.report({ |
| 106 | + node, |
| 107 | + loc: getRegexpLocation(qNode), |
| 108 | + messageId: "emptyQuestionMark", |
| 109 | + suggest: [suggestRemoveQuant(qNode)], |
| 110 | + }) |
| 111 | + return |
| 112 | + } |
| 113 | + |
| 114 | + // the quantified is zero length |
| 115 | + // e.g. (\b){5} |
| 116 | + if (qNode.min >= 1 && isZeroLength(qNode.element)) { |
| 117 | + context.report({ |
| 118 | + node, |
| 119 | + loc: getRegexpLocation(qNode), |
| 120 | + messageId: "zeroLength", |
| 121 | + suggest: [suggestRemoveQuant(qNode)], |
| 122 | + }) |
| 123 | + } |
| 124 | + }, |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + return defineRegexpVisitor(context, { |
| 129 | + createVisitor, |
| 130 | + }) |
| 131 | + }, |
| 132 | +}) |
0 commit comments