Skip to content

Commit ecb8201

Browse files
authored
Change to support YAML and JSON5. (#94)
1 parent 08cbd06 commit ecb8201

File tree

22 files changed

+1098
-302
lines changed

22 files changed

+1098
-302
lines changed

docs/started.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ module.export = {
3434
},
3535
settings: {
3636
'vue-i18n': {
37-
localeDir: './path/to/locales/*.json' // extention is glob formatting!
37+
localeDir: './path/to/locales/*.{json,json5,yaml,yml}}' // extension is glob formatting!
3838
// or
3939
// localeDir: {
40-
// pattern: './path/to/locales/*.json', // extention is glob formatting!
40+
// pattern: './path/to/locales/*.{json,json5,yaml,yml}', // extension is glob formatting!
4141
// localeKey: 'file' // or 'key'
4242
// }
4343
}
@@ -59,13 +59,20 @@ See [the rule list](../rules/)
5959

6060
### Running ESLint from command line
6161

62-
If you want to run `eslint` from command line, make sure you include the `.vue` and `.json` extension using [the `--ext` option](https://eslint.org/docs/user-guide/configuring#specifying-file-extensions-to-lint) or a glob pattern because ESLint targets only `.js` files by default.
62+
If you want to run `eslint` from command line, make sure you include the `.vue`, `.json`, `.json5`, `.yaml` and `.yml` extension using [the `--ext` option](https://eslint.org/docs/user-guide/configuring#specifying-file-extensions-to-lint) or a glob pattern because ESLint targets only `.js` files by default.
6363

6464
Examples:
6565

6666
```bash
6767
eslint --ext .js,.vue,.json src
6868
eslint "src/**/*.{js,vue,json}"
69+
# Specify the extension you use.
70+
# - use YAML?
71+
# eslint --ext .js,.vue,.yaml,.yml src
72+
# eslint "src/**/*.{js,vue,yaml,yml}"
73+
# - use JSON5?
74+
# eslint --ext .js,.vue,.json5 src
75+
# eslint "src/**/*.{js,vue,json5}"
6976
```
7077

7178
#### How to use custom parser?

lib/configs/base.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ module.exports = {
1212
parserOptions: {
1313
parser: require.resolve('eslint-plugin-jsonc')
1414
}
15+
},
16+
{
17+
files: ['*.yaml', '*.yml'],
18+
// TODO: If you do not use vue-eslint-parser, you will get an error in vue rules.
19+
// see https://github.com/vuejs/eslint-plugin-vue/pull/1262
20+
parser: require.resolve('vue-eslint-parser'),
21+
parserOptions: {
22+
parser: require.resolve('yaml-eslint-parser')
23+
},
24+
rules: {
25+
// ESLint core rules known to cause problems with YAML.
26+
// https://github.com/ota-meshi/eslint-plugin-yml/blob/4e468109b9d2f4376b8d4d1221adba27c6ee04b2/src/configs/base.ts#L7-L11
27+
'no-irregular-whitespace': 'off',
28+
'spaced-comment': 'off'
29+
}
1530
}
1631
]
1732
}

lib/rules/no-html-messages.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
const { extname } = require('path')
77
const parse5 = require('parse5')
88
const { getLocaleMessages } = require('../utils/index')
9-
const { traverseNodes } = require('eslint-plugin-jsonc')
109
const debug = require('debug')('eslint-plugin-vue-i18n:no-html-messages')
1110

1211
/**
1312
* @typedef {import('eslint-plugin-jsonc').AST.JSONLiteral} JSONLiteral
13+
* @typedef {import('yaml-eslint-parser').AST.YAMLScalar} YAMLScalar
1414
*/
1515

1616
function findHTMLNode (node) {
@@ -45,6 +45,27 @@ function create (context) {
4545
})
4646
}
4747

48+
/**
49+
* @param {YAMLScalar} node
50+
*/
51+
function verifyYAMLScalar (node) {
52+
const parent = node.parent
53+
if (parent.type === 'YAMLPair' && parent.key === node) {
54+
return
55+
}
56+
const htmlNode = parse5.parseFragment(`${node.value}`, { sourceCodeLocationInfo: true })
57+
const foundNode = findHTMLNode(htmlNode)
58+
if (!foundNode) { return }
59+
const loc = {
60+
line: node.loc.start.line,
61+
column: node.loc.start.column + 1/* quote */ + foundNode.sourceCodeLocation.startOffset
62+
}
63+
context.report({
64+
message: `used HTML localization message`,
65+
loc
66+
})
67+
}
68+
4869
if (extname(filename) === '.vue') {
4970
return {
5071
Program (node) {
@@ -65,27 +86,33 @@ function create (context) {
6586
if (!targetLocaleMessage) {
6687
continue
6788
}
68-
const ast = targetLocaleMessage.getJsonAST()
69-
if (!ast) {
70-
continue
71-
}
72-
73-
traverseNodes(ast, {
89+
targetLocaleMessage.traverseNodes({
7490
enterNode (node) {
75-
if (node.type !== 'JSONLiteral') {
76-
return
91+
if (node.type === 'JSONLiteral') {
92+
verifyJSONLiteral(node)
93+
} else if (node.type === 'YAMLScalar') {
94+
verifyYAMLScalar(node)
7795
}
78-
verifyJSONLiteral(node)
7996
},
8097
leaveNode () {}
8198
})
8299
}
83100
}
84101
}
85-
} else if (context.parserServices.isJSON && getLocaleMessages(context).findExistLocaleMessage(filename)) {
102+
} else if (context.parserServices.isJSON) {
103+
if (!getLocaleMessages(context).findExistLocaleMessage(filename)) {
104+
return {}
105+
}
86106
return {
87107
JSONLiteral: verifyJSONLiteral
88108
}
109+
} else if (context.parserServices.isYAML) {
110+
if (!getLocaleMessages(context).findExistLocaleMessage(filename)) {
111+
return {}
112+
}
113+
return {
114+
YAMLScalar: verifyYAMLScalar
115+
}
89116
} else {
90117
debug(`ignore ${filename} in no-html-messages`)
91118
return {}

0 commit comments

Comments
 (0)