refactor(eslint-plugin): migrate to TS-ESLint infrastructure (#7276)

* refactor(eslint-plugin): migrate to TS-ESLint infrastructure

* fix lock
This commit is contained in:
Joshua Chen 2022-04-30 17:57:57 +08:00 committed by GitHub
parent f063e9add5
commit afc72480ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 381 additions and 323 deletions

View file

@ -12,6 +12,7 @@ packages/lqip-loader/lib/
packages/docusaurus/lib/
packages/docusaurus-*/lib/*
packages/docusaurus-*/lib-next/
packages/eslint-plugin/lib/
packages/stylelint-copyright/lib/
copyUntypedFiles.mjs

View file

@ -316,7 +316,7 @@ module.exports = {
'@docusaurus/no-untranslated-text': [
WARNING,
{
ignoreStrings: [
ignoredStrings: [
'·',
'-',
'—',

1
.gitignore vendored
View file

@ -23,6 +23,7 @@ packages/create-docusaurus/lib/
packages/lqip-loader/lib/
packages/docusaurus/lib/
packages/docusaurus-*/lib/*
packages/eslint-plugin/lib/
packages/stylelint-copyright/lib/
packages/docusaurus-*/lib-next/

View file

@ -10,6 +10,7 @@ packages/docusaurus-*/lib/*
packages/docusaurus-*/lib-next/
packages/create-docusaurus/lib/*
packages/create-docusaurus/templates/*/docusaurus.config.js
packages/eslint-plugin/lib/
packages/stylelint-copyright/lib/
__fixtures__

View file

@ -91,6 +91,7 @@ export function Details({
// setOpen(false);
}
}}>
{/* eslint-disable-next-line @docusaurus/no-untranslated-text */}
{summary || <summary>Details</summary>}
<Collapsible

View file

@ -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');
}
},
};
},
};

View file

@ -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');
}
},
};
},
};

View file

@ -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,
};

View file

@ -17,8 +17,12 @@
"access": "public"
},
"license": "MIT",
"scripts": {
"build": "tsc"
},
"dependencies": {
"requireindex": "^1.2.0"
"@typescript-eslint/utils": "^5.21.0",
"tslib": "^2.4.0"
},
"devDependencies": {
"eslint-plugin-eslint-plugin": "^4.1.0"

View file

@ -5,10 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/
const requireIndex = require('requireindex');
import rules from './rules';
module.exports = {
rules: requireIndex(`${__dirname}/rules`),
export = {
rules,
configs: {
recommended: {
rules: {

View file

@ -5,69 +5,73 @@
* LICENSE file in the root directory of this source tree.
*/
const rule = require('../no-untranslated-text');
const {RuleTester} = require('eslint');
const {getCommonValidTests} = require('../../util');
import rule from '../no-untranslated-text';
import {getCommonValidTests, RuleTester} from './testUtils';
const errorsJSX = [{messageId: 'translateChildren', type: 'JSXElement'}];
const errorsJSX = [
{messageId: 'translateChildren', type: 'JSXElement'},
] as const;
const errorsJSXFragment = [
{messageId: 'translateChildren', type: 'JSXFragment'},
];
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2022,
ecmaFeatures: {jsx: true},
ecmaFeatures: {
jsx: true,
},
},
});
ruleTester.run('no-untranslated-text', rule, {
valid: [
...getCommonValidTests(),
{
code: '<Component>·</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>· </Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component> · </Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>· ·</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>· — ×</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>{"·"}</Component>',
options: [{ignoreStrings: ['·']}],
options: [{ignoredStrings: ['·']}],
},
{
code: "<Component>{'·'}</Component>",
options: [{ignoreStrings: ['·']}],
options: [{ignoredStrings: ['·']}],
},
{
code: '<Component>{`·`}</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>Docusaurus</Component>',
options: [{ignoreStrings: ['Docusaurus']}],
options: [{ignoredStrings: ['Docusaurus']}],
},
{
code: '<Component>&#8203;</Component>',
options: [{ignoreStrings: ['']}],
options: [{ignoredStrings: ['']}],
},
{
code: `<>
{' · '}
</>`,
options: [{ignoreStrings: ['·', "'"]}],
options: [{ignoredStrings: ['·']}],
},
],
@ -111,37 +115,37 @@ ruleTester.run('no-untranslated-text', rule, {
{
code: '<Component>· — ×</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—']}],
options: [{ignoredStrings: ['·', '—']}],
},
{
code: '<Component>··</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component> ·· </Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>"·"</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: "<Component>'·'</Component>",
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>`·`</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
options: [{ignoredStrings: ['·', '—', '×']}],
},
{
code: '<Component>Docusaurus</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['Docu', 'saurus']}],
options: [{ignoredStrings: ['Docu', 'saurus']}],
},
],
});

View file

@ -5,19 +5,25 @@
* LICENSE file in the root directory of this source tree.
*/
const rule = require('../string-literal-i18n-messages');
const {RuleTester} = require('eslint');
const {getCommonValidTests} = require('../../util');
import rule from '../string-literal-i18n-messages';
import {getCommonValidTests, RuleTester} from './testUtils';
const errorsJSX = [{messageId: 'translateChildren', type: 'JSXElement'}];
const errorsFunc = [{messageId: 'translateArg', type: 'Identifier'}];
const errorsJSX = [
{messageId: 'translateChildren', type: 'JSXElement'},
] as const;
const errorsFunc = [
{messageId: 'translateArg', type: 'CallExpression'},
] as const;
const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 2022,
ecmaFeatures: {jsx: true},
ecmaFeatures: {
jsx: true,
},
},
});
ruleTester.run('string-literal-i18n-messages', rule, {
valid: [...getCommonValidTests()],

View 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`})',
},
];

View 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,
};

View 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'});
}
},
};
},
});

View 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 {
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'});
}
},
};
},
});

View 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}`,
);

View file

@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
}
}

View file

@ -84,6 +84,7 @@ errnametoolong
esbenp
esbuild
eslintcache
estree
evaluable
externalwaiting
failfast
@ -303,6 +304,7 @@ treeifies
treeify
treosh
triaging
tses
typecheck
typechecks
typedoc

View file

@ -67,7 +67,7 @@ module.exports = {
rules: {
'@docusaurus/no-untranslated-text': [
'warn',
{ignoreStrings: ['·', '—', '×']},
{ignoredStrings: ['·', '—', '×']},
],
},
};

View file

@ -34,7 +34,7 @@ Accepted fields:
| 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>

View file

@ -3902,9 +3902,9 @@
semver "^7.3.5"
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"
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==
dependencies:
"@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"
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:
version "1.0.0"
resolved "https://registry.yarnpkg.com/requires-port/-/requires-port-1.0.0.tgz#925d2601d39ac485e091cf0da5c6e694dc3dcaff"