-
Notifications
You must be signed in to change notification settings - Fork 5
style: forbid { ns: literal } for t calls via ESLint #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
oeninghe-dataport
wants to merge
1
commit into
vue3/improve-import-lint
from
vue3/lint-i18next-namespace
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import importStyle from './import-style.js' | ||
| import noLiteralNsInT from './no-literal-ns-in-t.js' | ||
|
|
||
| export default { | ||
| rules: { | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| 'import-style': importStyle, | ||
| 'no-literal-ns-in-t': noLiteralNsInT, | ||
| /* eslint-enable @typescript-eslint/naming-convention */ | ||
| }, | ||
| } |
This file contains hidden or 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,63 @@ | ||
| import type { Rule } from 'eslint' | ||
|
|
||
| /** | ||
| * Enforces that the `ns` option in `t`/`$t` calls uses a constant identifier | ||
| * (e.g. `PluginId` or `CoreId`) instead of a hard-coded string literal. | ||
| * | ||
| * Bad: `t(($) => $.key, { ns: 'filter' })` | ||
| * Good: `t(($) => $.key, { ns: PluginId })` | ||
| */ | ||
| const noLiteralNsInT: Rule.RuleModule = { | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: | ||
| 'Enforce using `PluginId`/`CoreId` identifier instead of a string literal for the `ns` option in `t`/`$t` calls', | ||
| }, | ||
| messages: { | ||
| useLiteralId: | ||
| 'Use the `PluginId` (or `CoreId`) constant instead of the string literal "{{ ns }}" as the `ns` option.', | ||
| }, | ||
| schema: [], | ||
| }, | ||
| create(context) { | ||
| return { | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| CallExpression(node) { | ||
| const { callee } = node | ||
| if ( | ||
| callee.type !== 'Identifier' || | ||
| (callee.name !== 't' && callee.name !== '$t') | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| for (const arg of node.arguments) { | ||
| if (arg.type !== 'ObjectExpression') { | ||
| continue | ||
| } | ||
|
|
||
| for (const prop of arg.properties) { | ||
| if ( | ||
| prop.type === 'Property' && | ||
| !prop.computed && | ||
| ((prop.key.type === 'Identifier' && prop.key.name === 'ns') || | ||
| (prop.key.type === 'Literal' && prop.key.value === 'ns')) && | ||
| prop.value.type === 'Literal' && | ||
| typeof prop.value.value === 'string' | ||
| ) { | ||
| context.report({ | ||
| node: prop.value, | ||
| messageId: 'useLiteralId', | ||
| data: { ns: prop.value.value }, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| /* eslint-enable @typescript-eslint/naming-convention */ | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| export default noLiteralNsInT |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this be made auto-fixable? I'm sure @warm-coolguy would really enjoy that!
Also, if I add a literal in a template, this rule does not raise an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I would really enjoy that, I don't think it's plausible to invest that amount of time. Also confirming it doesn't work in templates and there's a lot of templates with the issue present.
Suggestion: We drop the extra rule and just search for
ns: 'and replace it in every file and call it a day.Pro: Saves time and we have another nice thing to nag about in future PRs.
Con: There is none and it won't stop anybody from just declaring the string locally anyway. I'll just find creative ways to annoy you if you proceed.