|
| 1 | +import type { Expression } from "estree" |
| 2 | +import { |
| 3 | + getAllowedCharRanges, |
| 4 | + inRange, |
| 5 | + getAllowedCharValueSchema, |
| 6 | +} from "../utils/char-ranges" |
| 7 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 8 | +import { |
| 9 | + createRule, |
| 10 | + defineRegexpVisitor, |
| 11 | + getRegexpLocation, |
| 12 | + isControlEscape, |
| 13 | + isEscapeSequence, |
| 14 | + isHexadecimalEscape, |
| 15 | + isOctalEscape, |
| 16 | +} from "../utils" |
| 17 | + |
| 18 | +export default createRule("no-obscure-range", { |
| 19 | + meta: { |
| 20 | + docs: { |
| 21 | + description: "disallow obscure character ranges", |
| 22 | + // TODO Switch to recommended in the major version. |
| 23 | + // recommended: true, |
| 24 | + recommended: false, |
| 25 | + }, |
| 26 | + schema: [ |
| 27 | + { |
| 28 | + type: "object", |
| 29 | + properties: { |
| 30 | + allowed: getAllowedCharValueSchema(), |
| 31 | + }, |
| 32 | + additionalProperties: false, |
| 33 | + }, |
| 34 | + ], |
| 35 | + messages: { |
| 36 | + unexpected: |
| 37 | + "Unexpected obscure character range. The characters of '{{range}}' ({{unicode}}) are not obvious.", |
| 38 | + }, |
| 39 | + type: "suggestion", // "problem", |
| 40 | + }, |
| 41 | + create(context) { |
| 42 | + const allowedRanges = getAllowedCharRanges( |
| 43 | + context.options[0]?.allowed, |
| 44 | + context, |
| 45 | + ) |
| 46 | + const sourceCode = context.getSourceCode() |
| 47 | + |
| 48 | + /** |
| 49 | + * Create visitor |
| 50 | + * @param node |
| 51 | + */ |
| 52 | + function createVisitor(node: Expression): RegExpVisitor.Handlers { |
| 53 | + return { |
| 54 | + onCharacterClassRangeEnter(rNode) { |
| 55 | + const { min, max } = rNode |
| 56 | + |
| 57 | + if (min.value === max.value) { |
| 58 | + // we don't deal with that |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + if (isControlEscape(min.raw) && isControlEscape(max.raw)) { |
| 63 | + // both min and max are control escapes |
| 64 | + return |
| 65 | + } |
| 66 | + if (isOctalEscape(min.raw) && isOctalEscape(max.raw)) { |
| 67 | + // both min and max are either octal |
| 68 | + return |
| 69 | + } |
| 70 | + if ( |
| 71 | + (isHexadecimalEscape(min.raw) || min.value === 0) && |
| 72 | + isHexadecimalEscape(max.raw) |
| 73 | + ) { |
| 74 | + // both min and max are hexadecimal (with a small exception for \0) |
| 75 | + return |
| 76 | + } |
| 77 | + |
| 78 | + if ( |
| 79 | + !isEscapeSequence(min.raw) && |
| 80 | + !isEscapeSequence(max.raw) && |
| 81 | + inRange(allowedRanges, min.value, max.value) |
| 82 | + ) { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + const uMin = `U+${min.value.toString(16).padStart(4, "0")}` |
| 87 | + const uMax = `U+${max.value.toString(16).padStart(4, "0")}` |
| 88 | + |
| 89 | + context.report({ |
| 90 | + node, |
| 91 | + loc: getRegexpLocation(sourceCode, node, rNode), |
| 92 | + messageId: "unexpected", |
| 93 | + data: { |
| 94 | + range: rNode.raw, |
| 95 | + unicode: `${uMin} - ${uMax}`, |
| 96 | + }, |
| 97 | + }) |
| 98 | + }, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return defineRegexpVisitor(context, { |
| 103 | + createVisitor, |
| 104 | + }) |
| 105 | + }, |
| 106 | +}) |
0 commit comments