docusaurus/packages/eslint-plugin/lib/rules/__tests__/no-untranslated-text.test.js
Elias Papavasileiou 3b1170eb44
feat: Docusaurus ESLint plugin to enforce best Docusaurus practices (#7206)
* feat: add eslint plugin

* refactor

* add tests

* fixups!

* fix(no-dynamic-i18n-messages): make translate() recognize template literals

* refactor: rename rule no-dynamic-i18n-messages --> string-literal-i18n-messages

* feat: add ignoreStrings option and refactor

* docs: migrate docs to /docs/api/plugins

* docs: fix anchor links in README.md

* fix: add some ignored strings

* docs: update eslint-plugin docs

* fix: update README link

* docs: various updates

- Reorder sidebar entries
- Fix title size
- Use Markdown file paths
- Simplify relative links

* address reviews

* wording polish

* add npmignore

* fix all internal warnings

* doc improvements

* fix test

Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
2022-04-30 00:04:25 +08:00

147 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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 rule = require('../no-untranslated-text');
const {RuleTester} = require('eslint');
const {getCommonValidTests} = require('../../util');
const errorsJSX = [{messageId: 'translateChildren', type: 'JSXElement'}];
const errorsJSXFragment = [
{messageId: 'translateChildren', type: 'JSXFragment'},
];
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 2022,
ecmaFeatures: {jsx: true},
},
});
ruleTester.run('no-untranslated-text', rule, {
valid: [
...getCommonValidTests(),
{
code: '<Component>·</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>· </Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component> · </Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>· ·</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>· — ×</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>{"·"}</Component>',
options: [{ignoreStrings: ['·']}],
},
{
code: "<Component>{'·'}</Component>",
options: [{ignoreStrings: ['·']}],
},
{
code: '<Component>{`·`}</Component>',
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>Docusaurus</Component>',
options: [{ignoreStrings: ['Docusaurus']}],
},
{
code: '<Component>&#8203;</Component>',
options: [{ignoreStrings: ['']}],
},
{
code: `<>
{' · '}
</>`,
options: [{ignoreStrings: ['·', "'"]}],
},
],
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: [{ignoreStrings: ['·', '—']}],
},
{
code: '<Component>··</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component> ·· </Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>"·"</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: "<Component>'·'</Component>",
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>`·`</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['·', '—', '×']}],
},
{
code: '<Component>Docusaurus</Component>',
errors: errorsJSX,
options: [{ignoreStrings: ['Docu', 'saurus']}],
},
],
});