|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + */ |
| 4 | +import type { AST as JSONAST } from 'jsonc-eslint-parser' |
| 5 | +import { getStaticJSONValue } from 'jsonc-eslint-parser' |
| 6 | +import type { AST as YAMLAST } from 'yaml-eslint-parser' |
| 7 | +import { getStaticYAMLValue } from 'yaml-eslint-parser' |
| 8 | +import { extname } from 'path' |
| 9 | +import { defineCustomBlocksVisitor, getLocaleMessages } from '../utils/index' |
| 10 | +import debugBuilder from 'debug' |
| 11 | +import type { RuleContext, RuleListener } from '../types' |
| 12 | +import { |
| 13 | + getMessageSyntaxVersions, |
| 14 | + getReportIndex |
| 15 | +} from '../utils/message-compiler/utils' |
| 16 | +import { parse } from '../utils/message-compiler/parser' |
| 17 | +import { parse as parseForV8 } from '../utils/message-compiler/parser-v8' |
| 18 | +import type { CompileError } from '@intlify/message-compiler' |
| 19 | +const debug = debugBuilder('eslint-plugin-vue-i18n:valid-message-syntax') |
| 20 | + |
| 21 | +function create(context: RuleContext): RuleListener { |
| 22 | + const filename = context.getFilename() |
| 23 | + const sourceCode = context.getSourceCode() |
| 24 | + const allowNotString = Boolean(context.options[0]?.allowNotString) |
| 25 | + const messageSyntaxVersions = getMessageSyntaxVersions(context) |
| 26 | + |
| 27 | + function* extractMessageErrors(message: string) { |
| 28 | + if (messageSyntaxVersions.v9) { |
| 29 | + yield* parse(message).errors |
| 30 | + } |
| 31 | + if (messageSyntaxVersions.v8) { |
| 32 | + yield* parseForV8(message).errors |
| 33 | + } |
| 34 | + } |
| 35 | + function verifyMessage( |
| 36 | + message: string | number | undefined | null | boolean | bigint | RegExp, |
| 37 | + reportNode: JSONAST.JSONNode | YAMLAST.YAMLNode, |
| 38 | + getReportOffset: ((error: CompileError) => number | null) | null |
| 39 | + ) { |
| 40 | + if (typeof message !== 'string') { |
| 41 | + if (!allowNotString) { |
| 42 | + const type = |
| 43 | + message === null |
| 44 | + ? 'null' |
| 45 | + : message instanceof RegExp |
| 46 | + ? 'RegExp' |
| 47 | + : typeof message |
| 48 | + context.report({ |
| 49 | + message: `Unexpected '${type}' message`, |
| 50 | + loc: reportNode.loc |
| 51 | + }) |
| 52 | + } |
| 53 | + } else { |
| 54 | + for (const error of extractMessageErrors(message)) { |
| 55 | + messageSyntaxVersions.reportIfMissingSetting() |
| 56 | + |
| 57 | + const reportOffset = getReportOffset?.(error) |
| 58 | + context.report({ |
| 59 | + message: error.message, |
| 60 | + loc: |
| 61 | + reportOffset != null |
| 62 | + ? sourceCode.getLocFromIndex(reportOffset) |
| 63 | + : reportNode.loc |
| 64 | + }) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + /** |
| 69 | + * Create node visitor for JSON |
| 70 | + */ |
| 71 | + function createVisitorForJson(): RuleListener { |
| 72 | + function verifyExpression( |
| 73 | + node: JSONAST.JSONExpression | null, |
| 74 | + parent: JSONAST.JSONNode |
| 75 | + ) { |
| 76 | + let message |
| 77 | + let getReportOffset: |
| 78 | + | ((error: CompileError) => number | null) |
| 79 | + | null = null |
| 80 | + if (node) { |
| 81 | + if ( |
| 82 | + node.type === 'JSONArrayExpression' || |
| 83 | + node.type === 'JSONObjectExpression' |
| 84 | + ) { |
| 85 | + return |
| 86 | + } |
| 87 | + message = getStaticJSONValue(node) |
| 88 | + getReportOffset = error => |
| 89 | + getReportIndex(node, error.location!.start.offset) |
| 90 | + } else { |
| 91 | + message = null |
| 92 | + } |
| 93 | + |
| 94 | + verifyMessage(message, node || parent, getReportOffset) |
| 95 | + } |
| 96 | + return { |
| 97 | + JSONProperty(node: JSONAST.JSONProperty) { |
| 98 | + verifyExpression(node.value, node) |
| 99 | + }, |
| 100 | + JSONArrayExpression(node: JSONAST.JSONArrayExpression) { |
| 101 | + for (const element of node.elements) { |
| 102 | + verifyExpression(element, node) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Create node visitor for YAML |
| 110 | + */ |
| 111 | + function createVisitorForYaml(): RuleListener { |
| 112 | + const yamlKeyNodes = new Set<YAMLAST.YAMLContent | YAMLAST.YAMLWithMeta>() |
| 113 | + function withinKey(node: YAMLAST.YAMLNode) { |
| 114 | + for (const keyNode of yamlKeyNodes) { |
| 115 | + if ( |
| 116 | + keyNode.range[0] <= node.range[0] && |
| 117 | + node.range[0] < keyNode.range[1] |
| 118 | + ) { |
| 119 | + return true |
| 120 | + } |
| 121 | + } |
| 122 | + return false |
| 123 | + } |
| 124 | + function verifyContent( |
| 125 | + node: YAMLAST.YAMLContent | YAMLAST.YAMLWithMeta | null, |
| 126 | + parent: YAMLAST.YAMLNode |
| 127 | + ) { |
| 128 | + let message |
| 129 | + let getReportOffset: |
| 130 | + | ((error: CompileError) => number | null) |
| 131 | + | null = null |
| 132 | + if (node) { |
| 133 | + const valueNode = node.type === 'YAMLWithMeta' ? node.value : node |
| 134 | + if ( |
| 135 | + !valueNode || |
| 136 | + valueNode.type === 'YAMLMapping' || |
| 137 | + valueNode.type === 'YAMLSequence' |
| 138 | + ) { |
| 139 | + return |
| 140 | + } |
| 141 | + message = getStaticYAMLValue(node) // Calculate the value including the tag. |
| 142 | + getReportOffset = error => |
| 143 | + getReportIndex(valueNode, error.location!.start.offset) |
| 144 | + } else { |
| 145 | + message = null |
| 146 | + } |
| 147 | + if (message != null && typeof message === 'object') { |
| 148 | + return |
| 149 | + } |
| 150 | + |
| 151 | + verifyMessage(message, node || parent, getReportOffset) |
| 152 | + } |
| 153 | + return { |
| 154 | + YAMLPair(node: YAMLAST.YAMLPair) { |
| 155 | + if (withinKey(node)) { |
| 156 | + return |
| 157 | + } |
| 158 | + if (node.key != null) { |
| 159 | + yamlKeyNodes.add(node.key) |
| 160 | + } |
| 161 | + |
| 162 | + verifyContent(node.value, node) |
| 163 | + }, |
| 164 | + YAMLSequence(node: YAMLAST.YAMLSequence) { |
| 165 | + if (withinKey(node)) { |
| 166 | + return |
| 167 | + } |
| 168 | + for (const entry of node.entries) { |
| 169 | + verifyContent(entry, node) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + if (extname(filename) === '.vue') { |
| 176 | + return defineCustomBlocksVisitor( |
| 177 | + context, |
| 178 | + createVisitorForJson, |
| 179 | + createVisitorForYaml |
| 180 | + ) |
| 181 | + } else if (context.parserServices.isJSON || context.parserServices.isYAML) { |
| 182 | + const localeMessages = getLocaleMessages(context) |
| 183 | + const targetLocaleMessage = localeMessages.findExistLocaleMessage(filename) |
| 184 | + if (!targetLocaleMessage) { |
| 185 | + debug(`ignore ${filename} in valid-message-syntax`) |
| 186 | + return {} |
| 187 | + } |
| 188 | + |
| 189 | + if (context.parserServices.isJSON) { |
| 190 | + return createVisitorForJson() |
| 191 | + } else if (context.parserServices.isYAML) { |
| 192 | + return createVisitorForYaml() |
| 193 | + } |
| 194 | + return {} |
| 195 | + } else { |
| 196 | + debug(`ignore ${filename} in valid-message-syntax`) |
| 197 | + return {} |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +export = { |
| 202 | + meta: { |
| 203 | + type: 'layout', |
| 204 | + docs: { |
| 205 | + description: 'disallow invalid message syntax', |
| 206 | + category: 'Recommended', |
| 207 | + // TODO To avoid breaking changes, include it in the configuration at the time of version upgrade. |
| 208 | + recommended: false |
| 209 | + }, |
| 210 | + fixable: null, |
| 211 | + schema: [ |
| 212 | + { |
| 213 | + type: 'object', |
| 214 | + properties: { |
| 215 | + allowNotString: { |
| 216 | + type: 'boolean' |
| 217 | + } |
| 218 | + }, |
| 219 | + additionalProperties: false |
| 220 | + } |
| 221 | + ] |
| 222 | + }, |
| 223 | + create |
| 224 | +} |
0 commit comments