mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-07 21:32:38 +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
|
@ -0,0 +1,151 @@
|
|||
/**
|
||||
* 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 rule from '../no-untranslated-text';
|
||||
import {getCommonValidTests, RuleTester} from './testUtils';
|
||||
|
||||
const errorsJSX = [
|
||||
{messageId: 'translateChildren', type: 'JSXElement'},
|
||||
] as const;
|
||||
const errorsJSXFragment = [
|
||||
{messageId: 'translateChildren', type: 'JSXFragment'},
|
||||
];
|
||||
|
||||
const ruleTester = new RuleTester({
|
||||
parser: '@typescript-eslint/parser',
|
||||
parserOptions: {
|
||||
ecmaFeatures: {
|
||||
jsx: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
ruleTester.run('no-untranslated-text', rule, {
|
||||
valid: [
|
||||
...getCommonValidTests(),
|
||||
{
|
||||
code: '<Component>·</Component>',
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>· </Component>',
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component> · </Component>',
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>· ·</Component>',
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>· — ×</Component>',
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>{"·"}</Component>',
|
||||
options: [{ignoredStrings: ['·']}],
|
||||
},
|
||||
{
|
||||
code: "<Component>{'·'}</Component>",
|
||||
options: [{ignoredStrings: ['·']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>{`·`}</Component>',
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>Docusaurus</Component>',
|
||||
options: [{ignoredStrings: ['Docusaurus']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>​</Component>',
|
||||
options: [{ignoredStrings: ['']}],
|
||||
},
|
||||
{
|
||||
code: `<>
|
||||
{' · '}
|
||||
</>`,
|
||||
options: [{ignoredStrings: ['·']}],
|
||||
},
|
||||
],
|
||||
|
||||
invalid: [
|
||||
{
|
||||
code: '<Component>text</Component>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Component> text </Component>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Component>"text"</Component>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: "<Component>'text'</Component>",
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Component>`text`</Component>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Component>{"text"}</Component>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: "<Component>{'text'}</Component>",
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Component>{`text`}</Component>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<>text</>',
|
||||
errors: errorsJSXFragment,
|
||||
},
|
||||
{
|
||||
code: '<Component>· — ×</Component>',
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['·', '—']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>··</Component>',
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component> ·· </Component>',
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>"·"</Component>',
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: "<Component>'·'</Component>",
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>`·`</Component>',
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['·', '—', '×']}],
|
||||
},
|
||||
{
|
||||
code: '<Component>Docusaurus</Component>',
|
||||
errors: errorsJSX,
|
||||
options: [{ignoredStrings: ['Docu', 'saurus']}],
|
||||
},
|
||||
],
|
||||
});
|
|
@ -0,0 +1,57 @@
|
|||
/**
|
||||
* 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 rule from '../string-literal-i18n-messages';
|
||||
import {getCommonValidTests, RuleTester} from './testUtils';
|
||||
|
||||
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: {
|
||||
ecmaFeatures: {
|
||||
jsx: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
ruleTester.run('string-literal-i18n-messages', rule, {
|
||||
valid: [...getCommonValidTests()],
|
||||
|
||||
invalid: [
|
||||
{
|
||||
code: '<Translate>{text}</Translate>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Translate>Hi {text} my friend</Translate>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Translate> {text} </Translate>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: '<Translate>`{text}`</Translate>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
// eslint-disable-next-line no-template-curly-in-string
|
||||
code: '<Translate>{`${text}`}</Translate>',
|
||||
errors: errorsJSX,
|
||||
},
|
||||
{
|
||||
code: 'translate({message: metaTitle})',
|
||||
errors: errorsFunc,
|
||||
},
|
||||
],
|
||||
});
|
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`})',
|
||||
},
|
||||
];
|
Loading…
Add table
Add a link
Reference in a new issue