|
| 1 | +import { Linter } from "eslint" |
| 2 | +import * as parser from "@typescript-eslint/parser" |
| 3 | +// @ts-expect-error -- ignore |
| 4 | +import { rules } from "../../lib/index" |
| 5 | +import assert from "assert" |
| 6 | +import { createRule, defineRegexpVisitor } from "../../lib/utils" |
| 7 | +import type { RegExpVisitor } from "regexpp/visitor" |
| 8 | +import type { Expression } from "estree" |
| 9 | + |
| 10 | +const TEST_RULE = createRule("test", { |
| 11 | + meta: { |
| 12 | + docs: { |
| 13 | + description: "", |
| 14 | + recommended: false, |
| 15 | + }, |
| 16 | + schema: [], |
| 17 | + messages: {}, |
| 18 | + type: "suggestion", // "problem", |
| 19 | + }, |
| 20 | + |
| 21 | + create(context) { |
| 22 | + function createVisitor(node: Expression): RegExpVisitor.Handlers { |
| 23 | + return { |
| 24 | + onPatternEnter() { |
| 25 | + context.report({ |
| 26 | + node, |
| 27 | + message: "Foo", |
| 28 | + }) |
| 29 | + }, |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return defineRegexpVisitor(context, { |
| 34 | + createVisitor, |
| 35 | + }) |
| 36 | + }, |
| 37 | +}) |
| 38 | + |
| 39 | +describe("Don't crash even if with unknown flag.", () => { |
| 40 | + const code = "var foo = /a/abcdefgg; new RegExp('a', 'abcdefgg')" |
| 41 | + |
| 42 | + for (const key of Object.keys(rules)) { |
| 43 | + const rule = rules[key] |
| 44 | + const ruleId = rule.meta.docs.ruleId |
| 45 | + |
| 46 | + it(ruleId, () => { |
| 47 | + const linter = new Linter() |
| 48 | + const config: Linter.Config = { |
| 49 | + parser: "@typescript-eslint/parser", |
| 50 | + parserOptions: { |
| 51 | + ecmaVersion: 2020, |
| 52 | + }, |
| 53 | + rules: { |
| 54 | + [ruleId]: "error", |
| 55 | + "regexp/test": "error", |
| 56 | + }, |
| 57 | + } |
| 58 | + // @ts-expect-error -- ignore |
| 59 | + linter.defineParser("@typescript-eslint/parser", parser) |
| 60 | + // @ts-expect-error -- ignore |
| 61 | + linter.defineRule(ruleId, rule) |
| 62 | + |
| 63 | + linter.defineRule( |
| 64 | + "regexp/test", |
| 65 | + // @ts-expect-error -- ignore |
| 66 | + TEST_RULE, |
| 67 | + ) |
| 68 | + const resultVue = linter.verifyAndFix(code, config, "test.js") |
| 69 | + |
| 70 | + assert.deepStrictEqual( |
| 71 | + resultVue.messages.map((m) => ({ |
| 72 | + ruleId: m.ruleId, |
| 73 | + message: m.message, |
| 74 | + })), |
| 75 | + [ |
| 76 | + { ruleId: "regexp/test", message: "Foo" }, |
| 77 | + { ruleId: "regexp/test", message: "Foo" }, |
| 78 | + ], |
| 79 | + ) |
| 80 | + }) |
| 81 | + } |
| 82 | +}) |
| 83 | + |
| 84 | +// describe("Don't crash even if with unknown flag in core rules", () => { |
| 85 | +// const code = "var foo = /a/abcdefgg;\n new RegExp('a', 'abcdefgg')" |
| 86 | + |
| 87 | +// for (const ruleId of new Set([ |
| 88 | +// ...Object.keys(configs.recommended.rules), |
| 89 | +// "no-empty-character-class", |
| 90 | +// ])) { |
| 91 | +// if (ruleId.startsWith("regexp/")) { |
| 92 | +// continue |
| 93 | +// } |
| 94 | + |
| 95 | +// it(ruleId, () => { |
| 96 | +// const linter = new Linter() |
| 97 | +// const config: Linter.Config = { |
| 98 | +// parser: "@typescript-eslint/parser", |
| 99 | +// parserOptions: { |
| 100 | +// ecmaVersion: 2020, |
| 101 | +// }, |
| 102 | +// rules: { |
| 103 | +// [ruleId]: "error", |
| 104 | +// }, |
| 105 | +// } |
| 106 | +// // @ts-expect-error -- ignore |
| 107 | +// linter.defineParser("@typescript-eslint/parser", parser) |
| 108 | +// const resultVue = linter.verifyAndFix(code, config, "test.js") |
| 109 | + |
| 110 | +// const msgs = resultVue.messages.map((m) => ({ |
| 111 | +// ruleId: m.ruleId, |
| 112 | +// line: m.line, |
| 113 | +// })) |
| 114 | +// if (ruleId === "no-invalid-regexp") { |
| 115 | +// assert.deepStrictEqual(msgs, [ |
| 116 | +// { ruleId: "no-invalid-regexp", line: 2 }, |
| 117 | +// ]) |
| 118 | +// } else if (ruleId === "prefer-regex-literals") { |
| 119 | +// assert.deepStrictEqual(msgs, [ |
| 120 | +// { ruleId: "prefer-regex-literals", line: 2 }, |
| 121 | +// ]) |
| 122 | +// } else { |
| 123 | +// assert.deepStrictEqual(msgs, []) |
| 124 | +// } |
| 125 | +// }) |
| 126 | +// } |
| 127 | +// }) |
0 commit comments