docusaurus/packages/eslint-plugin/lib/rules/no-untranslated-text.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

71 lines
1.9 KiB
JavaScript

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