@docusaurus/eslint-plugin 0.0.0-4925

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Facebook, Inc. and its affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,7 @@
1
+ # `@docusaurus/eslint-plugin`
2
+
3
+ ESLint plugin to enforce best Docusaurus practices.
4
+
5
+ ## Usage
6
+
7
+ See [eslint-plugin documentation](https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin).
package/lib/index.js ADDED
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ const requireIndex = require('requireindex');
9
+
10
+ module.exports = {
11
+ rules: requireIndex(`${__dirname}/rules`),
12
+ configs: {
13
+ recommended: {
14
+ rules: {
15
+ '@docusaurus/string-literal-i18n-messages': 'error',
16
+ },
17
+ },
18
+ all: {
19
+ rules: {
20
+ '@docusaurus/string-literal-i18n-messages': 'error',
21
+ '@docusaurus/no-untranslated-text': 'warn',
22
+ },
23
+ },
24
+ },
25
+ };
@@ -0,0 +1,71 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ const {isTextLabelChild, report} = require('../util');
9
+
10
+ /**
11
+ * @type {import('eslint').Rule.RuleModule}
12
+ */
13
+ module.exports = {
14
+ meta: {
15
+ type: 'suggestion',
16
+ docs: {
17
+ description:
18
+ 'enforce text labels in JSX to be wrapped by translate calls',
19
+ category: 'Suggestions',
20
+ url: 'https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin/no-untranslated-text',
21
+ },
22
+ schema: [
23
+ {
24
+ type: 'object',
25
+ properties: {
26
+ ignoreStrings: {
27
+ type: 'array',
28
+ },
29
+ },
30
+ additionalProperties: false,
31
+ },
32
+ ],
33
+ messages: {
34
+ translateChildren:
35
+ 'All text labels in JSX should be wrapped by translate calls',
36
+ },
37
+ },
38
+
39
+ create(context) {
40
+ const stringsToIgnore = context.options[0]?.ignoreStrings ?? [];
41
+
42
+ const isParentTranslate = ({child, isParentFragment}) =>
43
+ !isParentFragment &&
44
+ child.parent.openingElement.name.name === 'Translate';
45
+
46
+ const isChildValid = ({child, isParentFragment}) => {
47
+ if (!isTextLabelChild({child, ignoreWhitespace: true, stringsToIgnore})) {
48
+ return true;
49
+ }
50
+ return isParentTranslate({child, isParentFragment});
51
+ };
52
+
53
+ const isNodeValid = ({node, isFragment = false} = {}) =>
54
+ node.children.every((child) =>
55
+ isChildValid({child, isParentFragment: isFragment}),
56
+ );
57
+
58
+ return {
59
+ 'JSXElement[openingElement.selfClosing=false]': (node) => {
60
+ if (!isNodeValid({node})) {
61
+ report(context, node, 'translateChildren');
62
+ }
63
+ },
64
+ 'JSXFragment[openingFragment]': (node) => {
65
+ if (!isNodeValid({node, isFragment: true})) {
66
+ report(context, node, 'translateChildren');
67
+ }
68
+ },
69
+ };
70
+ },
71
+ };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ const {
9
+ isTextLabelChild,
10
+ report,
11
+ isStringWithoutExpressions,
12
+ } = require('../util');
13
+
14
+ /**
15
+ * @type {import('eslint').Rule.RuleModule}
16
+ */
17
+ module.exports = {
18
+ meta: {
19
+ type: 'problem',
20
+ docs: {
21
+ description: 'enforce translate APIs to be called on plain text labels',
22
+ category: 'Possible Problems',
23
+ url: 'https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin/string-literal-i18n-messages',
24
+ },
25
+ schema: [],
26
+ messages: {
27
+ translateChildren:
28
+ '<Translate> children must be hardcoded strings. You can have in-string dynamic placeholders using the values prop.',
29
+ translateArg:
30
+ 'translation message must be a hardcoded string. You can have in-string dynamic placeholders using the values argument.',
31
+ },
32
+ },
33
+
34
+ create(context) {
35
+ const isNodeValid = (node) =>
36
+ node.children.every((child) => isTextLabelChild({child}));
37
+
38
+ return {
39
+ "JSXElement[openingElement.name.name='Translate']": (node) => {
40
+ if (!isNodeValid(node)) {
41
+ report(context, node, 'translateChildren');
42
+ }
43
+ },
44
+ "CallExpression > Identifier[name='translate']": (node) => {
45
+ const messageProperty = node.parent.arguments[0].properties.find(
46
+ (property) => property.key.name === 'message',
47
+ );
48
+ if (!messageProperty) {
49
+ return;
50
+ }
51
+
52
+ if (
53
+ !isStringWithoutExpressions({
54
+ text: messageProperty.value,
55
+ })
56
+ ) {
57
+ report(context, node, 'translateArg');
58
+ }
59
+ },
60
+ };
61
+ },
62
+ };
package/lib/util.js ADDED
@@ -0,0 +1,145 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ const isMadeOfIgnoredStrings = ({text, stringsToIgnore}) =>
9
+ text
10
+ .trim()
11
+ .split(/\s+/)
12
+ .every((string) => stringsToIgnore.includes(string));
13
+
14
+ const isWhitespace = (text) => !text || !text.trim();
15
+
16
+ const isTextValid = ({text, ignoreWhitespace, stringsToIgnore}) =>
17
+ !!text &&
18
+ !(ignoreWhitespace && isWhitespace(text)) &&
19
+ !isMadeOfIgnoredStrings({
20
+ text,
21
+ stringsToIgnore,
22
+ });
23
+
24
+ const isStringWithoutExpressions = ({
25
+ text,
26
+ ignoreWhitespace = false,
27
+ stringsToIgnore = [],
28
+ } = {}) => {
29
+ switch (text.type) {
30
+ case 'Literal':
31
+ return isTextValid({text: text.value, ignoreWhitespace, stringsToIgnore});
32
+ case 'TemplateLiteral':
33
+ return (
34
+ !text.expressions.length &&
35
+ isTextValid({
36
+ text: text.quasis[0].value.raw,
37
+ ignoreWhitespace,
38
+ stringsToIgnore,
39
+ })
40
+ );
41
+ default:
42
+ return false;
43
+ }
44
+ };
45
+
46
+ const isTextLabelChild = ({
47
+ child,
48
+ ignoreWhitespace = false,
49
+ stringsToIgnore = [],
50
+ } = {}) => {
51
+ switch (child.type) {
52
+ case 'JSXText':
53
+ return isTextValid({
54
+ text: child.value,
55
+ ignoreWhitespace,
56
+ stringsToIgnore,
57
+ });
58
+ case 'JSXExpressionContainer':
59
+ return isStringWithoutExpressions({
60
+ text: child.expression,
61
+ ignoreWhitespace,
62
+ stringsToIgnore,
63
+ });
64
+ default:
65
+ return false;
66
+ }
67
+ };
68
+
69
+ const report = (context, node, messageId) => {
70
+ context.report({
71
+ node,
72
+ messageId,
73
+ });
74
+ };
75
+
76
+ const getCommonValidTests = () => [
77
+ {
78
+ code: '<Translate>text</Translate>',
79
+ },
80
+ {
81
+ code: '<Translate> text </Translate>',
82
+ },
83
+ {
84
+ code: '<Translate>"text"</Translate>',
85
+ },
86
+ {
87
+ code: "<Translate>'text'</Translate>",
88
+ },
89
+ {
90
+ code: '<Translate>`text`</Translate>',
91
+ },
92
+ {
93
+ code: '<Translate>{"text"}</Translate>',
94
+ },
95
+ {
96
+ code: "<Translate>{'text'}</Translate>",
97
+ },
98
+ {
99
+ code: '<Translate>{`text`}</Translate>',
100
+ },
101
+ {
102
+ code: '<Component>{text}</Component>',
103
+ },
104
+ {
105
+ code: '<Component> {text} </Component>',
106
+ },
107
+ {
108
+ code: 'translate({message: `My page meta title`})',
109
+ },
110
+ {
111
+ code: `<Translate
112
+ id="homepage.title"
113
+ description="The homepage welcome message">
114
+ Welcome to my website
115
+ </Translate>`,
116
+ },
117
+ {
118
+ code: `<Translate
119
+ values={{firstName: 'Sébastien'}}>
120
+ {'Welcome, {firstName}! How are you?'}
121
+ </Translate>`,
122
+ },
123
+ {
124
+ code: `<Translate>{'This'} is {\`valid\`}</Translate>`,
125
+ },
126
+ {
127
+ code: "translate({message: 'My page meta title'})",
128
+ },
129
+ {
130
+ code: "translate({message: 'The logo of site {siteName}'}, {siteName: 'Docusaurus'})",
131
+ },
132
+ {
133
+ code: 'translate({otherProp: metaTitle})',
134
+ },
135
+ {
136
+ code: 'translate({otherProp: `My page meta title`})',
137
+ },
138
+ ];
139
+
140
+ module.exports = {
141
+ isTextLabelChild,
142
+ report,
143
+ getCommonValidTests,
144
+ isStringWithoutExpressions,
145
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@docusaurus/eslint-plugin",
3
+ "version": "0.0.0-4925",
4
+ "description": "ESLint plugin to enforce best Docusaurus practices.",
5
+ "main": "lib/index.js",
6
+ "keywords": [
7
+ "eslint",
8
+ "eslintplugin",
9
+ "eslint-plugin"
10
+ ],
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "https://github.com/facebook/docusaurus.git",
14
+ "directory": "packages/eslint-plugin"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "license": "MIT",
20
+ "dependencies": {
21
+ "requireindex": "^1.2.0"
22
+ },
23
+ "devDependencies": {
24
+ "eslint-plugin-eslint-plugin": "^4.1.0"
25
+ },
26
+ "peerDependencies": {
27
+ "eslint": ">=6"
28
+ },
29
+ "engines": {
30
+ "node": ">=14"
31
+ },
32
+ "gitHead": "bfcd4b0c2f1149c0a9809cd37b45ee8cccb2c4c2"
33
+ }