mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-17 10:12:25 +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
67
packages/eslint-plugin/src/util.ts
Normal file
67
packages/eslint-plugin/src/util.ts
Normal file
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* 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';
|
||||
import type {TSESTree} from '@typescript-eslint/types/dist/ts-estree';
|
||||
|
||||
type CheckTranslateChildOptions = {
|
||||
ignoredStrings?: string[];
|
||||
};
|
||||
|
||||
const isMadeOfIgnoredStrings = (text: string, ignoredStrings: string[]) =>
|
||||
text
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.every((string) => !string || ignoredStrings.includes(string));
|
||||
|
||||
const isValidTranslationLabel = (
|
||||
text: unknown,
|
||||
{ignoredStrings}: CheckTranslateChildOptions = {},
|
||||
) => {
|
||||
if (!ignoredStrings) {
|
||||
return typeof text === 'string';
|
||||
}
|
||||
return (
|
||||
typeof text === 'string' && !isMadeOfIgnoredStrings(text, ignoredStrings)
|
||||
);
|
||||
};
|
||||
|
||||
export function isStringWithoutExpressions(
|
||||
text: TSESTree.Node,
|
||||
options?: CheckTranslateChildOptions,
|
||||
): boolean {
|
||||
switch (text.type) {
|
||||
case 'Literal':
|
||||
return isValidTranslationLabel(text.value, options);
|
||||
case 'TemplateLiteral':
|
||||
return (
|
||||
text.expressions.length === 0 &&
|
||||
isValidTranslationLabel(text.quasis[0]!.value.raw, options)
|
||||
);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function isTextLabelChild(
|
||||
child: TSESTree.JSXChild,
|
||||
options?: CheckTranslateChildOptions,
|
||||
): boolean {
|
||||
switch (child.type) {
|
||||
case 'JSXText':
|
||||
return isValidTranslationLabel(child.value, options);
|
||||
case 'JSXExpressionContainer':
|
||||
return isStringWithoutExpressions(child.expression, options);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export const createRule = ESLintUtils.RuleCreator(
|
||||
(name) =>
|
||||
`https://docusaurus.io/docs/api/misc/@docusaurus/eslint-plugin/${name}`,
|
||||
);
|
Loading…
Add table
Add a link
Reference in a new issue