mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-18 10:42:31 +02:00
refactor(eslint-plugin): migrate to TS-ESLint infrastructure (#7276)
* refactor(eslint-plugin): migrate to TS-ESLint infrastructure * fix lock
This commit is contained in:
parent
f063e9add5
commit
afc72480ab
22 changed files with 381 additions and 323 deletions
|
@ -12,6 +12,7 @@ packages/lqip-loader/lib/
|
||||||
packages/docusaurus/lib/
|
packages/docusaurus/lib/
|
||||||
packages/docusaurus-*/lib/*
|
packages/docusaurus-*/lib/*
|
||||||
packages/docusaurus-*/lib-next/
|
packages/docusaurus-*/lib-next/
|
||||||
|
packages/eslint-plugin/lib/
|
||||||
packages/stylelint-copyright/lib/
|
packages/stylelint-copyright/lib/
|
||||||
copyUntypedFiles.mjs
|
copyUntypedFiles.mjs
|
||||||
|
|
||||||
|
|
|
@ -316,7 +316,7 @@ module.exports = {
|
||||||
'@docusaurus/no-untranslated-text': [
|
'@docusaurus/no-untranslated-text': [
|
||||||
WARNING,
|
WARNING,
|
||||||
{
|
{
|
||||||
ignoreStrings: [
|
ignoredStrings: [
|
||||||
'·',
|
'·',
|
||||||
'-',
|
'-',
|
||||||
'—',
|
'—',
|
||||||
|
|
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -23,6 +23,7 @@ packages/create-docusaurus/lib/
|
||||||
packages/lqip-loader/lib/
|
packages/lqip-loader/lib/
|
||||||
packages/docusaurus/lib/
|
packages/docusaurus/lib/
|
||||||
packages/docusaurus-*/lib/*
|
packages/docusaurus-*/lib/*
|
||||||
|
packages/eslint-plugin/lib/
|
||||||
packages/stylelint-copyright/lib/
|
packages/stylelint-copyright/lib/
|
||||||
packages/docusaurus-*/lib-next/
|
packages/docusaurus-*/lib-next/
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ packages/docusaurus-*/lib/*
|
||||||
packages/docusaurus-*/lib-next/
|
packages/docusaurus-*/lib-next/
|
||||||
packages/create-docusaurus/lib/*
|
packages/create-docusaurus/lib/*
|
||||||
packages/create-docusaurus/templates/*/docusaurus.config.js
|
packages/create-docusaurus/templates/*/docusaurus.config.js
|
||||||
|
packages/eslint-plugin/lib/
|
||||||
packages/stylelint-copyright/lib/
|
packages/stylelint-copyright/lib/
|
||||||
__fixtures__
|
__fixtures__
|
||||||
|
|
||||||
|
|
|
@ -91,6 +91,7 @@ export function Details({
|
||||||
// setOpen(false);
|
// setOpen(false);
|
||||||
}
|
}
|
||||||
}}>
|
}}>
|
||||||
|
{/* eslint-disable-next-line @docusaurus/no-untranslated-text */}
|
||||||
{summary || <summary>Details</summary>}
|
{summary || <summary>Details</summary>}
|
||||||
|
|
||||||
<Collapsible
|
<Collapsible
|
||||||
|
|
|
@ -1,71 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const {isTextLabelChild, report} = require('../util');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {import('eslint').Rule.RuleModule}
|
|
||||||
*/
|
|
||||||
module.exports = {
|
|
||||||
meta: {
|
|
||||||
type: 'suggestion',
|
|
||||||
docs: {
|
|
||||||
description:
|
|
||||||
'enforce text labels in JSX to be wrapped by translate calls',
|
|
||||||
category: 'Suggestions',
|
|
||||||
url: 'https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin/no-untranslated-text',
|
|
||||||
},
|
|
||||||
schema: [
|
|
||||||
{
|
|
||||||
type: 'object',
|
|
||||||
properties: {
|
|
||||||
ignoreStrings: {
|
|
||||||
type: 'array',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
additionalProperties: false,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
messages: {
|
|
||||||
translateChildren:
|
|
||||||
'All text labels in JSX should be wrapped by translate calls',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
create(context) {
|
|
||||||
const stringsToIgnore = context.options[0]?.ignoreStrings ?? [];
|
|
||||||
|
|
||||||
const isParentTranslate = ({child, isParentFragment}) =>
|
|
||||||
!isParentFragment &&
|
|
||||||
child.parent.openingElement.name.name === 'Translate';
|
|
||||||
|
|
||||||
const isChildValid = ({child, isParentFragment}) => {
|
|
||||||
if (!isTextLabelChild({child, ignoreWhitespace: true, stringsToIgnore})) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return isParentTranslate({child, isParentFragment});
|
|
||||||
};
|
|
||||||
|
|
||||||
const isNodeValid = ({node, isFragment = false} = {}) =>
|
|
||||||
node.children.every((child) =>
|
|
||||||
isChildValid({child, isParentFragment: isFragment}),
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
|
||||||
'JSXElement[openingElement.selfClosing=false]': (node) => {
|
|
||||||
if (!isNodeValid({node})) {
|
|
||||||
report(context, node, 'translateChildren');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
'JSXFragment[openingFragment]': (node) => {
|
|
||||||
if (!isNodeValid({node, isFragment: true})) {
|
|
||||||
report(context, node, 'translateChildren');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
|
@ -1,62 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const {
|
|
||||||
isTextLabelChild,
|
|
||||||
report,
|
|
||||||
isStringWithoutExpressions,
|
|
||||||
} = require('../util');
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @type {import('eslint').Rule.RuleModule}
|
|
||||||
*/
|
|
||||||
module.exports = {
|
|
||||||
meta: {
|
|
||||||
type: 'problem',
|
|
||||||
docs: {
|
|
||||||
description: 'enforce translate APIs to be called on plain text labels',
|
|
||||||
category: 'Possible Problems',
|
|
||||||
url: 'https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin/string-literal-i18n-messages',
|
|
||||||
},
|
|
||||||
schema: [],
|
|
||||||
messages: {
|
|
||||||
translateChildren:
|
|
||||||
'<Translate> children must be hardcoded strings. You can have in-string dynamic placeholders using the values prop.',
|
|
||||||
translateArg:
|
|
||||||
'translation message must be a hardcoded string. You can have in-string dynamic placeholders using the values argument.',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
|
|
||||||
create(context) {
|
|
||||||
const isNodeValid = (node) =>
|
|
||||||
node.children.every((child) => isTextLabelChild({child}));
|
|
||||||
|
|
||||||
return {
|
|
||||||
"JSXElement[openingElement.name.name='Translate']": (node) => {
|
|
||||||
if (!isNodeValid(node)) {
|
|
||||||
report(context, node, 'translateChildren');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"CallExpression > Identifier[name='translate']": (node) => {
|
|
||||||
const messageProperty = node.parent.arguments[0].properties.find(
|
|
||||||
(property) => property.key.name === 'message',
|
|
||||||
);
|
|
||||||
if (!messageProperty) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isStringWithoutExpressions({
|
|
||||||
text: messageProperty.value,
|
|
||||||
})
|
|
||||||
) {
|
|
||||||
report(context, node, 'translateArg');
|
|
||||||
}
|
|
||||||
},
|
|
||||||
};
|
|
||||||
},
|
|
||||||
};
|
|
|
@ -1,145 +0,0 @@
|
||||||
/**
|
|
||||||
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
||||||
*
|
|
||||||
* This source code is licensed under the MIT license found in the
|
|
||||||
* LICENSE file in the root directory of this source tree.
|
|
||||||
*/
|
|
||||||
|
|
||||||
const isMadeOfIgnoredStrings = ({text, stringsToIgnore}) =>
|
|
||||||
text
|
|
||||||
.trim()
|
|
||||||
.split(/\s+/)
|
|
||||||
.every((string) => stringsToIgnore.includes(string));
|
|
||||||
|
|
||||||
const isWhitespace = (text) => !text || !text.trim();
|
|
||||||
|
|
||||||
const isTextValid = ({text, ignoreWhitespace, stringsToIgnore}) =>
|
|
||||||
!!text &&
|
|
||||||
!(ignoreWhitespace && isWhitespace(text)) &&
|
|
||||||
!isMadeOfIgnoredStrings({
|
|
||||||
text,
|
|
||||||
stringsToIgnore,
|
|
||||||
});
|
|
||||||
|
|
||||||
const isStringWithoutExpressions = ({
|
|
||||||
text,
|
|
||||||
ignoreWhitespace = false,
|
|
||||||
stringsToIgnore = [],
|
|
||||||
} = {}) => {
|
|
||||||
switch (text.type) {
|
|
||||||
case 'Literal':
|
|
||||||
return isTextValid({text: text.value, ignoreWhitespace, stringsToIgnore});
|
|
||||||
case 'TemplateLiteral':
|
|
||||||
return (
|
|
||||||
!text.expressions.length &&
|
|
||||||
isTextValid({
|
|
||||||
text: text.quasis[0].value.raw,
|
|
||||||
ignoreWhitespace,
|
|
||||||
stringsToIgnore,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const isTextLabelChild = ({
|
|
||||||
child,
|
|
||||||
ignoreWhitespace = false,
|
|
||||||
stringsToIgnore = [],
|
|
||||||
} = {}) => {
|
|
||||||
switch (child.type) {
|
|
||||||
case 'JSXText':
|
|
||||||
return isTextValid({
|
|
||||||
text: child.value,
|
|
||||||
ignoreWhitespace,
|
|
||||||
stringsToIgnore,
|
|
||||||
});
|
|
||||||
case 'JSXExpressionContainer':
|
|
||||||
return isStringWithoutExpressions({
|
|
||||||
text: child.expression,
|
|
||||||
ignoreWhitespace,
|
|
||||||
stringsToIgnore,
|
|
||||||
});
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const report = (context, node, messageId) => {
|
|
||||||
context.report({
|
|
||||||
node,
|
|
||||||
messageId,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getCommonValidTests = () => [
|
|
||||||
{
|
|
||||||
code: '<Translate>text</Translate>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Translate> text </Translate>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Translate>"text"</Translate>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "<Translate>'text'</Translate>",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Translate>`text`</Translate>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Translate>{"text"}</Translate>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "<Translate>{'text'}</Translate>",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Translate>{`text`}</Translate>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Component>{text}</Component>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: '<Component> {text} </Component>',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: 'translate({message: `My page meta title`})',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: `<Translate
|
|
||||||
id="homepage.title"
|
|
||||||
description="The homepage welcome message">
|
|
||||||
Welcome to my website
|
|
||||||
</Translate>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: `<Translate
|
|
||||||
values={{firstName: 'Sébastien'}}>
|
|
||||||
{'Welcome, {firstName}! How are you?'}
|
|
||||||
</Translate>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: `<Translate>{'This'} is {\`valid\`}</Translate>`,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "translate({message: 'My page meta title'})",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: "translate({message: 'The logo of site {siteName}'}, {siteName: 'Docusaurus'})",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: 'translate({otherProp: metaTitle})',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
code: 'translate({otherProp: `My page meta title`})',
|
|
||||||
},
|
|
||||||
];
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
isTextLabelChild,
|
|
||||||
report,
|
|
||||||
getCommonValidTests,
|
|
||||||
isStringWithoutExpressions,
|
|
||||||
};
|
|
|
@ -17,8 +17,12 @@
|
||||||
"access": "public"
|
"access": "public"
|
||||||
},
|
},
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc"
|
||||||
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"requireindex": "^1.2.0"
|
"@typescript-eslint/utils": "^5.21.0",
|
||||||
|
"tslib": "^2.4.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"eslint-plugin-eslint-plugin": "^4.1.0"
|
"eslint-plugin-eslint-plugin": "^4.1.0"
|
||||||
|
|
|
@ -5,10 +5,10 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const requireIndex = require('requireindex');
|
import rules from './rules';
|
||||||
|
|
||||||
module.exports = {
|
export = {
|
||||||
rules: requireIndex(`${__dirname}/rules`),
|
rules,
|
||||||
configs: {
|
configs: {
|
||||||
recommended: {
|
recommended: {
|
||||||
rules: {
|
rules: {
|
|
@ -5,69 +5,73 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const rule = require('../no-untranslated-text');
|
import rule from '../no-untranslated-text';
|
||||||
const {RuleTester} = require('eslint');
|
import {getCommonValidTests, RuleTester} from './testUtils';
|
||||||
const {getCommonValidTests} = require('../../util');
|
|
||||||
|
|
||||||
const errorsJSX = [{messageId: 'translateChildren', type: 'JSXElement'}];
|
const errorsJSX = [
|
||||||
|
{messageId: 'translateChildren', type: 'JSXElement'},
|
||||||
|
] as const;
|
||||||
const errorsJSXFragment = [
|
const errorsJSXFragment = [
|
||||||
{messageId: 'translateChildren', type: 'JSXFragment'},
|
{messageId: 'translateChildren', type: 'JSXFragment'},
|
||||||
];
|
];
|
||||||
|
|
||||||
const ruleTester = new RuleTester({
|
const ruleTester = new RuleTester({
|
||||||
|
parser: '@typescript-eslint/parser',
|
||||||
parserOptions: {
|
parserOptions: {
|
||||||
ecmaVersion: 2022,
|
ecmaFeatures: {
|
||||||
ecmaFeatures: {jsx: true},
|
jsx: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
ruleTester.run('no-untranslated-text', rule, {
|
ruleTester.run('no-untranslated-text', rule, {
|
||||||
valid: [
|
valid: [
|
||||||
...getCommonValidTests(),
|
...getCommonValidTests(),
|
||||||
{
|
{
|
||||||
code: '<Component>·</Component>',
|
code: '<Component>·</Component>',
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>· </Component>',
|
code: '<Component>· </Component>',
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component> · </Component>',
|
code: '<Component> · </Component>',
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>· ·</Component>',
|
code: '<Component>· ·</Component>',
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>· — ×</Component>',
|
code: '<Component>· — ×</Component>',
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>{"·"}</Component>',
|
code: '<Component>{"·"}</Component>',
|
||||||
options: [{ignoreStrings: ['·']}],
|
options: [{ignoredStrings: ['·']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "<Component>{'·'}</Component>",
|
code: "<Component>{'·'}</Component>",
|
||||||
options: [{ignoreStrings: ['·']}],
|
options: [{ignoredStrings: ['·']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>{`·`}</Component>',
|
code: '<Component>{`·`}</Component>',
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>Docusaurus</Component>',
|
code: '<Component>Docusaurus</Component>',
|
||||||
options: [{ignoreStrings: ['Docusaurus']}],
|
options: [{ignoredStrings: ['Docusaurus']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>​</Component>',
|
code: '<Component>​</Component>',
|
||||||
options: [{ignoreStrings: ['']}],
|
options: [{ignoredStrings: ['']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: `<>
|
code: `<>
|
||||||
{' · '}
|
{' · '}
|
||||||
</>`,
|
</>`,
|
||||||
options: [{ignoreStrings: ['·', "'"]}],
|
options: [{ignoredStrings: ['·']}],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
|
||||||
|
@ -111,37 +115,37 @@ ruleTester.run('no-untranslated-text', rule, {
|
||||||
{
|
{
|
||||||
code: '<Component>· — ×</Component>',
|
code: '<Component>· — ×</Component>',
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['·', '—']}],
|
options: [{ignoredStrings: ['·', '—']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>··</Component>',
|
code: '<Component>··</Component>',
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component> ·· </Component>',
|
code: '<Component> ·· </Component>',
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>"·"</Component>',
|
code: '<Component>"·"</Component>',
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: "<Component>'·'</Component>",
|
code: "<Component>'·'</Component>",
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>`·`</Component>',
|
code: '<Component>`·`</Component>',
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['·', '—', '×']}],
|
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
code: '<Component>Docusaurus</Component>',
|
code: '<Component>Docusaurus</Component>',
|
||||||
errors: errorsJSX,
|
errors: errorsJSX,
|
||||||
options: [{ignoreStrings: ['Docu', 'saurus']}],
|
options: [{ignoredStrings: ['Docu', 'saurus']}],
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
|
@ -5,19 +5,25 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const rule = require('../string-literal-i18n-messages');
|
import rule from '../string-literal-i18n-messages';
|
||||||
const {RuleTester} = require('eslint');
|
import {getCommonValidTests, RuleTester} from './testUtils';
|
||||||
const {getCommonValidTests} = require('../../util');
|
|
||||||
|
|
||||||
const errorsJSX = [{messageId: 'translateChildren', type: 'JSXElement'}];
|
const errorsJSX = [
|
||||||
const errorsFunc = [{messageId: 'translateArg', type: 'Identifier'}];
|
{messageId: 'translateChildren', type: 'JSXElement'},
|
||||||
|
] as const;
|
||||||
|
const errorsFunc = [
|
||||||
|
{messageId: 'translateArg', type: 'CallExpression'},
|
||||||
|
] as const;
|
||||||
|
|
||||||
const ruleTester = new RuleTester({
|
const ruleTester = new RuleTester({
|
||||||
|
parser: '@typescript-eslint/parser',
|
||||||
parserOptions: {
|
parserOptions: {
|
||||||
ecmaVersion: 2022,
|
ecmaFeatures: {
|
||||||
ecmaFeatures: {jsx: true},
|
jsx: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
ruleTester.run('string-literal-i18n-messages', rule, {
|
ruleTester.run('string-literal-i18n-messages', rule, {
|
||||||
valid: [...getCommonValidTests()],
|
valid: [...getCommonValidTests()],
|
||||||
|
|
76
packages/eslint-plugin/src/rules/__tests__/testUtils.ts
Normal file
76
packages/eslint-plugin/src/rules/__tests__/testUtils.ts
Normal file
|
@ -0,0 +1,76 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {ESLintUtils} from '@typescript-eslint/utils';
|
||||||
|
|
||||||
|
const {RuleTester} = ESLintUtils;
|
||||||
|
|
||||||
|
export {RuleTester};
|
||||||
|
|
||||||
|
export const getCommonValidTests = (): {code: string}[] => [
|
||||||
|
{
|
||||||
|
code: '<Translate>text</Translate>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Translate> text </Translate>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Translate>"text"</Translate>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "<Translate>'text'</Translate>",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Translate>`text`</Translate>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Translate>{"text"}</Translate>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "<Translate>{'text'}</Translate>",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Translate>{`text`}</Translate>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Component>{text}</Component>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: '<Component> {text} </Component>',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'translate({message: `My page meta title`})',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: `<Translate
|
||||||
|
id="homepage.title"
|
||||||
|
description="The homepage welcome message">
|
||||||
|
Welcome to my website
|
||||||
|
</Translate>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: `<Translate
|
||||||
|
values={{firstName: 'Sébastien'}}>
|
||||||
|
{'Welcome, {firstName}! How are you?'}
|
||||||
|
</Translate>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: `<Translate>{'This'} is {\`valid\`}</Translate>`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "translate({message: 'My page meta title'})",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: "translate({message: 'The logo of site {siteName}'}, {siteName: 'Docusaurus'})",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'translate({otherProp: metaTitle})',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'translate({otherProp: `My page meta title`})',
|
||||||
|
},
|
||||||
|
];
|
14
packages/eslint-plugin/src/rules/index.ts
Normal file
14
packages/eslint-plugin/src/rules/index.ts
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import noUntranslatedText from './no-untranslated-text';
|
||||||
|
import stringLiteralI18nMessages from './string-literal-i18n-messages';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
'no-untranslated-text': noUntranslatedText,
|
||||||
|
'string-literal-i18n-messages': stringLiteralI18nMessages,
|
||||||
|
};
|
79
packages/eslint-plugin/src/rules/no-untranslated-text.ts
Normal file
79
packages/eslint-plugin/src/rules/no-untranslated-text.ts
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {isTextLabelChild, createRule} from '../util';
|
||||||
|
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree';
|
||||||
|
|
||||||
|
type Options = [
|
||||||
|
{
|
||||||
|
ignoredStrings: string[];
|
||||||
|
},
|
||||||
|
];
|
||||||
|
type MessageIds = 'translateChildren';
|
||||||
|
|
||||||
|
export default createRule<Options, MessageIds>({
|
||||||
|
name: 'no-untranslated-text',
|
||||||
|
meta: {
|
||||||
|
type: 'suggestion',
|
||||||
|
docs: {
|
||||||
|
description:
|
||||||
|
'enforce text labels in JSX to be wrapped by translate calls',
|
||||||
|
recommended: false,
|
||||||
|
},
|
||||||
|
schema: [
|
||||||
|
{
|
||||||
|
type: 'object',
|
||||||
|
properties: {
|
||||||
|
ignoredStrings: {
|
||||||
|
type: 'array',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
additionalProperties: false,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
messages: {
|
||||||
|
translateChildren:
|
||||||
|
'All text labels in JSX should be wrapped by translate calls',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultOptions: [
|
||||||
|
{
|
||||||
|
ignoredStrings: [],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
|
||||||
|
create(context, [options]) {
|
||||||
|
const {ignoredStrings} = options;
|
||||||
|
return {
|
||||||
|
JSXElement(node) {
|
||||||
|
if (
|
||||||
|
node.openingElement.selfClosing ||
|
||||||
|
(node.openingElement.name as TSESTree.JSXIdentifier).name ===
|
||||||
|
'Translate'
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (
|
||||||
|
node.children.some((child) =>
|
||||||
|
isTextLabelChild(child, {ignoredStrings}),
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
context.report({node, messageId: 'translateChildren'});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
JSXFragment(node) {
|
||||||
|
if (
|
||||||
|
node.children.some((child) =>
|
||||||
|
isTextLabelChild(child, {ignoredStrings}),
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
context.report({node, messageId: 'translateChildren'});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
|
@ -0,0 +1,76 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {
|
||||||
|
isTextLabelChild,
|
||||||
|
isStringWithoutExpressions,
|
||||||
|
createRule,
|
||||||
|
} from '../util';
|
||||||
|
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree';
|
||||||
|
|
||||||
|
type Options = [];
|
||||||
|
type MessageIds = 'translateChildren' | 'translateArg';
|
||||||
|
|
||||||
|
export default createRule<Options, MessageIds>({
|
||||||
|
name: 'string-literal-i18n-messages',
|
||||||
|
meta: {
|
||||||
|
type: 'problem',
|
||||||
|
docs: {
|
||||||
|
description: 'enforce translate APIs to be called on plain text labels',
|
||||||
|
recommended: 'error',
|
||||||
|
},
|
||||||
|
schema: [],
|
||||||
|
messages: {
|
||||||
|
translateChildren:
|
||||||
|
'<Translate> children must be hardcoded strings. You can have in-string dynamic placeholders using the values prop.',
|
||||||
|
translateArg:
|
||||||
|
'translation message must be a hardcoded string. You can have in-string dynamic placeholders using the values argument.',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
defaultOptions: [],
|
||||||
|
|
||||||
|
create(context) {
|
||||||
|
return {
|
||||||
|
JSXElement(node) {
|
||||||
|
if (
|
||||||
|
(node.openingElement.name as TSESTree.JSXIdentifier).name !==
|
||||||
|
'Translate'
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (node.children.some((child) => !isTextLabelChild(child))) {
|
||||||
|
context.report({node, messageId: 'translateChildren'});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
CallExpression(node) {
|
||||||
|
if (
|
||||||
|
node.callee.type !== 'Identifier' ||
|
||||||
|
node.callee.name !== 'translate'
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const param = node.arguments[0];
|
||||||
|
if (!param || param.type !== 'ObjectExpression') {
|
||||||
|
context.report({node, messageId: 'translateArg'});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const messageProperty = param.properties.find(
|
||||||
|
(property): property is TSESTree.Property =>
|
||||||
|
property.type === 'Property' &&
|
||||||
|
(property.key as TSESTree.Identifier).name === 'message',
|
||||||
|
);
|
||||||
|
if (!messageProperty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isStringWithoutExpressions(messageProperty.value)) {
|
||||||
|
context.report({node, messageId: 'translateArg'});
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
},
|
||||||
|
});
|
67
packages/eslint-plugin/src/util.ts
Normal file
67
packages/eslint-plugin/src/util.ts
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
/**
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import {ESLintUtils} from '@typescript-eslint/utils';
|
||||||
|
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree';
|
||||||
|
|
||||||
|
type CheckTranslateChildOptions = {
|
||||||
|
ignoredStrings?: string[];
|
||||||
|
};
|
||||||
|
|
||||||
|
const isMadeOfIgnoredStrings = (text: string, ignoredStrings: string[]) =>
|
||||||
|
text
|
||||||
|
.trim()
|
||||||
|
.split(/\s+/)
|
||||||
|
.every((string) => !string || ignoredStrings.includes(string));
|
||||||
|
|
||||||
|
const isValidTranslationLabel = (
|
||||||
|
text: unknown,
|
||||||
|
{ignoredStrings}: CheckTranslateChildOptions = {},
|
||||||
|
) => {
|
||||||
|
if (!ignoredStrings) {
|
||||||
|
return typeof text === 'string';
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
typeof text === 'string' && !isMadeOfIgnoredStrings(text, ignoredStrings)
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export function isStringWithoutExpressions(
|
||||||
|
text: TSESTree.Node,
|
||||||
|
options?: CheckTranslateChildOptions,
|
||||||
|
): boolean {
|
||||||
|
switch (text.type) {
|
||||||
|
case 'Literal':
|
||||||
|
return isValidTranslationLabel(text.value, options);
|
||||||
|
case 'TemplateLiteral':
|
||||||
|
return (
|
||||||
|
text.expressions.length === 0 &&
|
||||||
|
isValidTranslationLabel(text.quasis[0]!.value.raw, options)
|
||||||
|
);
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isTextLabelChild(
|
||||||
|
child: TSESTree.JSXChild,
|
||||||
|
options?: CheckTranslateChildOptions,
|
||||||
|
): boolean {
|
||||||
|
switch (child.type) {
|
||||||
|
case 'JSXText':
|
||||||
|
return isValidTranslationLabel(child.value, options);
|
||||||
|
case 'JSXExpressionContainer':
|
||||||
|
return isStringWithoutExpressions(child.expression, options);
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const createRule = ESLintUtils.RuleCreator(
|
||||||
|
(name) =>
|
||||||
|
`https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin/${name}`,
|
||||||
|
);
|
9
packages/eslint-plugin/tsconfig.json
Normal file
9
packages/eslint-plugin/tsconfig.json
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
{
|
||||||
|
"extends": "../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"incremental": true,
|
||||||
|
"tsBuildInfoFile": "./lib/.tsbuildinfo",
|
||||||
|
"rootDir": "src",
|
||||||
|
"outDir": "lib"
|
||||||
|
}
|
||||||
|
}
|
|
@ -84,6 +84,7 @@ errnametoolong
|
||||||
esbenp
|
esbenp
|
||||||
esbuild
|
esbuild
|
||||||
eslintcache
|
eslintcache
|
||||||
|
estree
|
||||||
evaluable
|
evaluable
|
||||||
externalwaiting
|
externalwaiting
|
||||||
failfast
|
failfast
|
||||||
|
@ -303,6 +304,7 @@ treeifies
|
||||||
treeify
|
treeify
|
||||||
treosh
|
treosh
|
||||||
triaging
|
triaging
|
||||||
|
tses
|
||||||
typecheck
|
typecheck
|
||||||
typechecks
|
typechecks
|
||||||
typedoc
|
typedoc
|
||||||
|
|
|
@ -67,7 +67,7 @@ module.exports = {
|
||||||
rules: {
|
rules: {
|
||||||
'@docusaurus/no-untranslated-text': [
|
'@docusaurus/no-untranslated-text': [
|
||||||
'warn',
|
'warn',
|
||||||
{ignoreStrings: ['·', '—', '×']},
|
{ignoredStrings: ['·', '—', '×']},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
@ -34,7 +34,7 @@ Accepted fields:
|
||||||
|
|
||||||
| Option | Type | Default | Description |
|
| Option | Type | Default | Description |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| `ignoreStrings` | `string[]` | `[]` | Text labels that only contain strings in this list will not be reported. |
|
| `ignoredStrings` | `string[]` | `[]` | Text labels that only contain strings in this list will not be reported. |
|
||||||
|
|
||||||
</APITable>
|
</APITable>
|
||||||
|
|
||||||
|
|
|
@ -3902,9 +3902,9 @@
|
||||||
semver "^7.3.5"
|
semver "^7.3.5"
|
||||||
tsutils "^3.21.0"
|
tsutils "^3.21.0"
|
||||||
|
|
||||||
"@typescript-eslint/utils@5.21.0", "@typescript-eslint/utils@^5.10.0":
|
"@typescript-eslint/utils@5.21.0", "@typescript-eslint/utils@^5.10.0", "@typescript-eslint/utils@^5.21.0":
|
||||||
version "5.21.0"
|
version "5.21.0"
|
||||||
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.21.0.tgz#51d7886a6f0575e23706e5548c7e87bce42d7c18"
|
resolved "https://registry.npmmirror.com/@typescript-eslint/utils/-/utils-5.21.0.tgz#51d7886a6f0575e23706e5548c7e87bce42d7c18"
|
||||||
integrity sha512-q/emogbND9wry7zxy7VYri+7ydawo2HDZhRZ5k6yggIvXa7PvBbAAZ4PFH/oZLem72ezC4Pr63rJvDK/sTlL8Q==
|
integrity sha512-q/emogbND9wry7zxy7VYri+7ydawo2HDZhRZ5k6yggIvXa7PvBbAAZ4PFH/oZLem72ezC4Pr63rJvDK/sTlL8Q==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@types/json-schema" "^7.0.9"
|
"@types/json-schema" "^7.0.9"
|
||||||
|
@ -13177,11 +13177,6 @@ require-from-string@^2.0.2:
|
||||||
resolved "https://registry.yarnpkg.com/require-like/-/require-like-0.1.2.tgz#ad6f30c13becd797010c468afa775c0c0a6b47fa"
|
resolved "https://registry.yarnpkg.com/require-like/-/require-like-0.1.2.tgz#ad6f30c13becd797010c468afa775c0c0a6b47fa"
|
||||||
integrity sha512-oyrU88skkMtDdauHDuKVrgR+zuItqr6/c//FXzvmxRGMexSDc6hNvJInGW3LL46n+8b50RykrvwSUIIQH2LQ5A==
|
integrity sha512-oyrU88skkMtDdauHDuKVrgR+zuItqr6/c//FXzvmxRGMexSDc6hNvJInGW3LL46n+8b50RykrvwSUIIQH2LQ5A==
|
||||||
|
|
||||||
requireindex@^1.2.0:
|
|
||||||
version "1.2.0"
|
|
||||||
resolved "https://registry.yarnpkg.com/requireindex/-/requireindex-1.2.0.tgz#3463cdb22ee151902635aa6c9535d4de9c2ef1ef"
|
|
||||||
integrity sha512-L9jEkOi3ASd9PYit2cwRfyppc9NoABujTP8/5gFcbERmo5jUoAKovIC3fsF17pkTnGsrByysqX+Kxd2OTNI1ww==
|
|
||||||
|
|
||||||
requires-port@^1.0.0:
|
requires-port@^1.0.0:
|
||||||
version "1.0.0"
|
version "1.0.0"
|
||||||
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
|
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue