Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions docs/started.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ module.export = {
},
settings: {
'vue-i18n': {
localeDir: './path/to/locales/*.json' // extention is glob formatting!
localeDir: './path/to/locales/*.{json,json5,yaml,yml}}' // extension is glob formatting!
// or
// localeDir: {
// pattern: './path/to/locales/*.json', // extention is glob formatting!
// pattern: './path/to/locales/*.{json,json5,yaml,yml}', // extension is glob formatting!
// localeKey: 'file' // or 'key'
// }
}
Expand All @@ -59,13 +59,20 @@ See [the rule list](../rules/)

### Running ESLint from command line

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.
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.

Examples:

```bash
eslint --ext .js,.vue,.json src
eslint "src/**/*.{js,vue,json}"
# Specify the extension you use.
# - use YAML?
# eslint --ext .js,.vue,.yaml,.yml src
# eslint "src/**/*.{js,vue,yaml,yml}"
# - use JSON5?
# eslint --ext .js,.vue,.json5 src
# eslint "src/**/*.{js,vue,json5}"
```

#### How to use custom parser?
Expand Down
15 changes: 15 additions & 0 deletions lib/configs/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,21 @@ module.exports = {
parserOptions: {
parser: require.resolve('eslint-plugin-jsonc')
}
},
{
files: ['*.yaml', '*.yml'],
// TODO: If you do not use vue-eslint-parser, you will get an error in vue rules.
// see https://github.com/vuejs/eslint-plugin-vue/pull/1262
parser: require.resolve('vue-eslint-parser'),
parserOptions: {
parser: require.resolve('yaml-eslint-parser')
},
rules: {
// ESLint core rules known to cause problems with YAML.
// https://github.com/ota-meshi/eslint-plugin-yml/blob/4e468109b9d2f4376b8d4d1221adba27c6ee04b2/src/configs/base.ts#L7-L11
'no-irregular-whitespace': 'off',
'spaced-comment': 'off'
}
}
]
}
49 changes: 38 additions & 11 deletions lib/rules/no-html-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
const { extname } = require('path')
const parse5 = require('parse5')
const { getLocaleMessages } = require('../utils/index')
const { traverseNodes } = require('eslint-plugin-jsonc')
const debug = require('debug')('eslint-plugin-vue-i18n:no-html-messages')

/**
* @typedef {import('eslint-plugin-jsonc').AST.JSONLiteral} JSONLiteral
* @typedef {import('yaml-eslint-parser').AST.YAMLScalar} YAMLScalar
*/

function findHTMLNode (node) {
Expand Down Expand Up @@ -45,6 +45,27 @@ function create (context) {
})
}

/**
* @param {YAMLScalar} node
*/
function verifyYAMLScalar (node) {
const parent = node.parent
if (parent.type === 'YAMLPair' && parent.key === node) {
return
}
const htmlNode = parse5.parseFragment(`${node.value}`, { sourceCodeLocationInfo: true })
const foundNode = findHTMLNode(htmlNode)
if (!foundNode) { return }
const loc = {
line: node.loc.start.line,
column: node.loc.start.column + 1/* quote */ + foundNode.sourceCodeLocation.startOffset
}
context.report({
message: `used HTML localization message`,
loc
})
}

if (extname(filename) === '.vue') {
return {
Program (node) {
Expand All @@ -65,27 +86,33 @@ function create (context) {
if (!targetLocaleMessage) {
continue
}
const ast = targetLocaleMessage.getJsonAST()
if (!ast) {
continue
}

traverseNodes(ast, {
targetLocaleMessage.traverseNodes({
enterNode (node) {
if (node.type !== 'JSONLiteral') {
return
if (node.type === 'JSONLiteral') {
verifyJSONLiteral(node)
} else if (node.type === 'YAMLScalar') {
verifyYAMLScalar(node)
}
verifyJSONLiteral(node)
},
leaveNode () {}
})
}
}
}
} else if (context.parserServices.isJSON && getLocaleMessages(context).findExistLocaleMessage(filename)) {
} else if (context.parserServices.isJSON) {
if (!getLocaleMessages(context).findExistLocaleMessage(filename)) {
return {}
}
return {
JSONLiteral: verifyJSONLiteral
}
} else if (context.parserServices.isYAML) {
if (!getLocaleMessages(context).findExistLocaleMessage(filename)) {
return {}
}
return {
YAMLScalar: verifyYAMLScalar
}
} else {
debug(`ignore ${filename} in no-html-messages`)
return {}
Expand Down
Loading