-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
no-deprecated-modulo-syntax
rule (#499)
* feat: node-deprecated-modulo-syntax rule * refactor * fix: add modulo to AST node * fix: unit test timeout * docs: add no-deprecated-modulo-syntax * fix: update docs docs and lib with generate script * test: fix: add message syntax version * Create grumpy-forks-brake.md * docs: fix * Update grumpy-forks-brake.md --------- Co-authored-by: Yosuke Ota <[email protected]>
- Loading branch information
Showing
18 changed files
with
556 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@intlify/eslint-plugin-vue-i18n": minor | ||
--- | ||
|
||
feat: `no-deprecated-modulo-syntax` rule |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
--- | ||
title: '@intlify/vue-i18n/no-deprecated-modulo-syntax' | ||
description: enforce modulo interpolation to be named interpolation | ||
since: v3.0.0 | ||
--- | ||
|
||
# @intlify/vue-i18n/no-deprecated-modulo-syntax | ||
|
||
> enforce modulo interpolation to be named interpolation | ||
- :black_nib:️ The `--fix` option on the [command line](http://eslint.org/docs/user-guide/command-line-interface#fix) can automatically fix some of the problems reported by this rule. | ||
|
||
This rule enforces modulo interpolation to be named interpolation | ||
|
||
## :book: Rule Details | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
locale messages: | ||
|
||
<eslint-code-block fix language="json"> | ||
|
||
```json | ||
/* eslint @intlify/vue-i18n/no-deprecated-modulo-syntax: 'error' */ | ||
{ | ||
/* ✗ BAD */ | ||
"hello": "%{msg} world" | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
locale messages (for vue-i18n v9+): | ||
|
||
<eslint-code-block fix message-syntax-version="^9" language="json"> | ||
|
||
```json | ||
/* eslint @intlify/vue-i18n/no-deprecated-modulo-syntax: 'error' */ | ||
{ | ||
/* ✓ GOOD */ | ||
"hello": "{msg} world" | ||
} | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in `@intlify/eslint-plugin-vue-i18n` v3.0.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/intlify/eslint-plugin-vue-i18n/blob/master/lib/rules/no-deprecated-modulo-syntax.ts) | ||
- [Test source](https://github.com/intlify/eslint-plugin-vue-i18n/tree/master/tests/lib/rules/no-deprecated-modulo-syntax.ts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* @author kazuya kawaguchi (a.k.a. kazupon) | ||
*/ | ||
import type { AST as JSONAST } from 'jsonc-eslint-parser' | ||
import type { AST as YAMLAST } from 'yaml-eslint-parser' | ||
import type { RuleContext, RuleListener } from '../types' | ||
import type { GetReportOffset } from '../utils/rule' | ||
import type { CustomBlockVisitorFactory } from '../types/vue-parser-services' | ||
import { extname } from 'node:path' | ||
import debugBuilder from 'debug' | ||
import { defineCustomBlocksVisitor, getLocaleMessages } from '../utils/index' | ||
import { | ||
getMessageSyntaxVersions, | ||
NodeTypes | ||
} from '../utils/message-compiler/utils' | ||
import { parse } from '../utils/message-compiler/parser' | ||
import { traverseNode } from '../utils/message-compiler/traverser' | ||
import { | ||
createRule, | ||
defineCreateVisitorForJson, | ||
defineCreateVisitorForYaml | ||
} from '../utils/rule' | ||
import { getFilename, getSourceCode } from '../utils/compat' | ||
|
||
const debug = debugBuilder('eslint-plugin-vue-i18n:no-deprecated-modulo-syntax') | ||
|
||
function create(context: RuleContext): RuleListener { | ||
const filename = getFilename(context) | ||
const sourceCode = getSourceCode(context) | ||
const messageSyntaxVersions = getMessageSyntaxVersions(context) | ||
|
||
function verifyForV9( | ||
message: string, | ||
reportNode: JSONAST.JSONStringLiteral | YAMLAST.YAMLScalar, | ||
getReportOffset: GetReportOffset | ||
) { | ||
const { ast, errors } = parse(message) | ||
if (errors.length) { | ||
return | ||
} | ||
traverseNode(ast, node => { | ||
if (node.type !== NodeTypes.Named || !node.modulo) { | ||
return | ||
} | ||
let range: [number, number] | null = null | ||
const start = getReportOffset(node.loc!.start.offset) | ||
const end = getReportOffset(node.loc!.end.offset) | ||
if (start != null && end != null) { | ||
// Subtract `%` length (1), because we want to fix modulo | ||
range = [start - 1, end] | ||
} | ||
context.report({ | ||
loc: range | ||
? { | ||
start: sourceCode.getLocFromIndex(range[0]), | ||
end: sourceCode.getLocFromIndex(range[1]) | ||
} | ||
: reportNode.loc, | ||
message: | ||
'The modulo interpolation must be enforced to named interpolation.', | ||
fix(fixer) { | ||
return range ? fixer.removeRange([range[0], range[0] + 1]) : null | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
function verifyMessage( | ||
message: string, | ||
reportNode: JSONAST.JSONStringLiteral | YAMLAST.YAMLScalar, | ||
getReportOffset: GetReportOffset | ||
) { | ||
if (messageSyntaxVersions.reportIfMissingSetting()) { | ||
return | ||
} | ||
if (messageSyntaxVersions.v9) { | ||
verifyForV9(message, reportNode, getReportOffset) | ||
} else if (messageSyntaxVersions.v8) { | ||
return | ||
} | ||
} | ||
|
||
const createVisitorForJson = defineCreateVisitorForJson(verifyMessage) | ||
const createVisitorForYaml = defineCreateVisitorForYaml(verifyMessage) | ||
|
||
if (extname(filename) === '.vue') { | ||
return defineCustomBlocksVisitor( | ||
context, | ||
createVisitorForJson, | ||
createVisitorForYaml | ||
) | ||
} else if ( | ||
sourceCode.parserServices.isJSON || | ||
sourceCode.parserServices.isYAML | ||
) { | ||
const localeMessages = getLocaleMessages(context) | ||
const targetLocaleMessage = localeMessages.findExistLocaleMessage(filename) | ||
if (!targetLocaleMessage) { | ||
debug(`ignore ${filename} in no-deprecated-modulo-syntax`) | ||
return {} | ||
} | ||
|
||
if (sourceCode.parserServices.isJSON) { | ||
return createVisitorForJson( | ||
context as Parameters<CustomBlockVisitorFactory>[0] | ||
) | ||
} else if (sourceCode.parserServices.isYAML) { | ||
return createVisitorForYaml( | ||
context as Parameters<CustomBlockVisitorFactory>[0] | ||
) | ||
} | ||
return {} | ||
} else { | ||
debug(`ignore ${filename} in no-deprecated-modulo-syntax`) | ||
return {} | ||
} | ||
} | ||
|
||
export = createRule({ | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce modulo interpolation to be named interpolation', | ||
category: 'Recommended', | ||
url: 'https://eslint-plugin-vue-i18n.intlify.dev/rules/no-deprecated-modulo-syntax.html', | ||
recommended: false | ||
}, | ||
fixable: 'code', | ||
schema: [] | ||
}, | ||
create | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.