Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
34 changed files
with
885 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
module.exports = { | ||
extends: ['../../.eslintrc.js', 'plugin:eslint-plugin/recommended'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
copyUntypedFiles.mjs | ||
.tsbuildinfo | ||
tsconfig* | ||
__tests__ | ||
|
||
.eslintrc.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# `@docusaurus/eslint-plugin` | ||
|
||
ESLint plugin to enforce best Docusaurus practices. | ||
|
||
## Usage | ||
|
||
See [eslint-plugin documentation](https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* 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 requireIndex = require('requireindex'); | ||
|
||
module.exports = { | ||
rules: requireIndex(`${__dirname}/rules`), | ||
configs: { | ||
recommended: { | ||
rules: { | ||
'@docusaurus/string-literal-i18n-messages': 'error', | ||
}, | ||
}, | ||
all: { | ||
rules: { | ||
'@docusaurus/string-literal-i18n-messages': 'error', | ||
'@docusaurus/no-untranslated-text': 'warn', | ||
}, | ||
}, | ||
}, | ||
}; |
147 changes: 147 additions & 0 deletions
147
packages/eslint-plugin/lib/rules/__tests__/no-untranslated-text.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/** | ||
* 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>​</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']}], | ||
}, | ||
], | ||
}); |
51 changes: 51 additions & 0 deletions
51
packages/eslint-plugin/lib/rules/__tests__/string-literal-i18n-messages.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* 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('../string-literal-i18n-messages'); | ||
const {RuleTester} = require('eslint'); | ||
const {getCommonValidTests} = require('../../util'); | ||
|
||
const errorsJSX = [{messageId: 'translateChildren', type: 'JSXElement'}]; | ||
const errorsFunc = [{messageId: 'translateArg', type: 'Identifier'}]; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2022, | ||
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, | ||
}, | ||
], | ||
}); |
Oops, something went wrong.