|
| 1 | +import type { AST } from "jsonc-eslint-parser" |
| 2 | +import type { RuleListener } from "../types" |
| 3 | +import { createRule } from "../utils" |
| 4 | +import type { CasingKind } from "../utils/casing" |
| 5 | +import { getChecker, allowedCaseOptions } from "../utils/casing" |
| 6 | + |
| 7 | +type Option = { |
| 8 | + [key in CasingKind]?: boolean |
| 9 | +} & { |
| 10 | + ignores?: string[] |
| 11 | +} |
| 12 | + |
| 13 | +export default createRule("key-name-casing", { |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: "enforce naming convention to property key names", |
| 17 | + recommended: null, |
| 18 | + extensionRule: false, |
| 19 | + }, |
| 20 | + schema: [ |
| 21 | + { |
| 22 | + type: "object", |
| 23 | + properties: { |
| 24 | + camelCase: { |
| 25 | + type: "boolean", |
| 26 | + default: true, |
| 27 | + }, |
| 28 | + // eslint-disable-next-line @typescript-eslint/naming-convention -- option |
| 29 | + PascalCase: { |
| 30 | + type: "boolean", |
| 31 | + default: false, |
| 32 | + }, |
| 33 | + // eslint-disable-next-line @typescript-eslint/naming-convention -- option |
| 34 | + SCREAMING_SNAKE_CASE: { |
| 35 | + type: "boolean", |
| 36 | + default: false, |
| 37 | + }, |
| 38 | + // eslint-disable-next-line @typescript-eslint/naming-convention -- option |
| 39 | + "kebab-case": { |
| 40 | + type: "boolean", |
| 41 | + default: false, |
| 42 | + }, |
| 43 | + // eslint-disable-next-line @typescript-eslint/naming-convention -- option |
| 44 | + snake_case: { |
| 45 | + type: "boolean", |
| 46 | + default: false, |
| 47 | + }, |
| 48 | + ignores: { |
| 49 | + type: "array", |
| 50 | + items: { |
| 51 | + type: "string", |
| 52 | + }, |
| 53 | + uniqueItems: true, |
| 54 | + additionalItems: false, |
| 55 | + }, |
| 56 | + }, |
| 57 | + additionalProperties: false, |
| 58 | + }, |
| 59 | + ], |
| 60 | + messages: { |
| 61 | + doesNotMatchFormat: |
| 62 | + "Property name `{{name}}` must match one of the following formats: {{formats}}", |
| 63 | + }, |
| 64 | + type: "layout", |
| 65 | + }, |
| 66 | + create(context) { |
| 67 | + if (!context.parserServices.isJSON) { |
| 68 | + return {} as RuleListener |
| 69 | + } |
| 70 | + const sourceCode = context.getSourceCode() |
| 71 | + const option: Option = { ...context.options[0] } |
| 72 | + if (option.camelCase !== false) { |
| 73 | + option.camelCase = true |
| 74 | + } |
| 75 | + const ignores = option.ignores |
| 76 | + ? option.ignores.map((ignore) => new RegExp(ignore)) |
| 77 | + : [] |
| 78 | + const formats = Object.keys(option) |
| 79 | + .filter((key): key is CasingKind => |
| 80 | + allowedCaseOptions.includes(key as CasingKind), |
| 81 | + ) |
| 82 | + .filter((key) => option[key]) |
| 83 | + |
| 84 | + const checkers: ((str: string) => boolean)[] = formats.map(getChecker) |
| 85 | + |
| 86 | + /** |
| 87 | + * Check whether a given name is a valid. |
| 88 | + */ |
| 89 | + function isValid(name: string): boolean { |
| 90 | + if (ignores.some((regex) => regex.test(name))) { |
| 91 | + return true |
| 92 | + } |
| 93 | + return checkers.length ? checkers.some((c) => c(name)) : true |
| 94 | + } |
| 95 | + |
| 96 | + return { |
| 97 | + JSONProperty(node: AST.JSONProperty) { |
| 98 | + const name = |
| 99 | + node.key.type === "JSONLiteral" && |
| 100 | + typeof node.key.value === "string" |
| 101 | + ? node.key.value |
| 102 | + : sourceCode.text.slice(...node.key.range) |
| 103 | + if (!isValid(name)) { |
| 104 | + context.report({ |
| 105 | + loc: node.key.loc, |
| 106 | + messageId: "doesNotMatchFormat", |
| 107 | + data: { |
| 108 | + name, |
| 109 | + formats: formats.join(", "), |
| 110 | + }, |
| 111 | + }) |
| 112 | + } |
| 113 | + }, |
| 114 | + } |
| 115 | + }, |
| 116 | +}) |
0 commit comments