refactor: install eslint-plugin-regexp (#6906)

* refactor: install eslint-plugin-regexp

* simplify

* simplify

* fix
This commit is contained in:
Joshua Chen 2022-03-13 18:32:17 +08:00 committed by GitHub
parent 127183e70e
commit 3a4b9b4c30
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
23 changed files with 145 additions and 83 deletions

View file

@ -231,7 +231,7 @@ describe('docsVersion', () => {
expect(versions).toEqual(['1.0.0']);
expect(consoleMock).toHaveBeenCalledWith(
expect.stringMatching(
/.*\[SUCCESS\].* .*\[docs\].*: version .*1\.0\.0.* created!.*/,
/.*\[SUCCESS\].*\[docs\].*: version .*1\.0\.0.* created!.*/,
),
);
@ -286,7 +286,7 @@ describe('docsVersion', () => {
expect(versions).toEqual(['2.0.0', '1.0.1', '1.0.0', 'withSlugs']);
expect(consoleMock).toHaveBeenCalledWith(
expect.stringMatching(
/.*\[SUCCESS\].* .*\[docs\].*: version .*2\.0\.0.* created!.*/,
/.*\[SUCCESS\].*\[docs\].*: version .*2\.0\.0.* created!.*/,
),
);
@ -339,7 +339,7 @@ describe('docsVersion', () => {
expect(versions).toEqual(['2.0.0', '1.0.0']);
expect(consoleMock).toHaveBeenCalledWith(
expect.stringMatching(
/.*\[SUCCESS\].* .*\[community\].*: version .*2.0.0.* created!.*/,
/.*\[SUCCESS\].*\[community\].*: version .*2.0.0.* created!.*/,
),
);

View file

@ -88,7 +88,7 @@ export async function cliDocsVersionCommand(
// Since we are going to create `version-${version}` folder, we need to make
// sure it's a valid pathname.
// eslint-disable-next-line no-control-regex
if (/[<>:"|?*\x00-\x1F]/g.test(version)) {
if (/[<>:"|?*\x00-\x1F]/.test(version)) {
throw new Error(
`${pluginIdLogPrefix}: invalid version tag specified! Please ensure its a valid pathname too. Try something like: 1.0.0.`,
);

View file

@ -8,43 +8,33 @@
import type {NumberPrefixParser} from '@docusaurus/plugin-content-docs';
// Best-effort to avoid parsing some patterns as number prefix
const IgnoredPrefixPatterns = (() => {
// ignore common date-like patterns: https://github.com/facebook/docusaurus/issues/4640
const DateLikePrefixRegex =
/^(?:\d{2}|\d{4})[-_.]\d{2}(?:[-_.](?:\d{2}|\d{4}))?.*$/;
// ignore common date-like patterns: https://github.com/facebook/docusaurus/issues/4640
// ignore common versioning patterns: https://github.com/facebook/docusaurus/issues/4653
// Both of them would look like 7.0-foo or 2021-11-foo
// note: we could try to parse float numbers in filenames, but that is probably
// not worth it, as a version such as "8.0" can be interpreted as either a
// version or a float. User can configure her own NumberPrefixParser if she
// wants 8.0 to be interpreted as a float
const ignoredPrefixPattern = /^\d+[-_.]\d+/;
// ignore common versioning patterns: https://github.com/facebook/docusaurus/issues/4653
// note: we could try to parse float numbers in filenames but that is
// probably not worth it as a version such as "8.0" can be interpreted as both
// a version and a float. User can configure her own NumberPrefixParser if
// she wants 8.0 to be interpreted as a float
const VersionLikePrefixRegex = /^\d+[-_.]\d+.*$/;
return new RegExp(
`${DateLikePrefixRegex.source}|${VersionLikePrefixRegex.source}`,
);
})();
const NumberPrefixRegex =
/^(?<numberPrefix>\d+)(?<separator>\s*[-_.]+\s*)(?<suffix>.*)$/;
const numberPrefixPattern =
/^(?<numberPrefix>\d+)\s*[-_.]+\s*(?<suffix>[^-_.\s].*)$/;
// 0-myDoc => {filename: myDoc, numberPrefix: 0}
// 003 - myDoc => {filename: myDoc, numberPrefix: 3}
export const DefaultNumberPrefixParser: NumberPrefixParser = (
filename: string,
) => {
if (IgnoredPrefixPatterns.exec(filename)) {
if (ignoredPrefixPattern.test(filename)) {
return {filename, numberPrefix: undefined};
}
const match = numberPrefixPattern.exec(filename);
if (!match) {
return {filename, numberPrefix: undefined};
}
const match = NumberPrefixRegex.exec(filename);
const cleanFileName = match?.groups?.suffix ?? filename;
const numberPrefixString = match?.groups?.numberPrefix;
const numberPrefix = numberPrefixString
? parseInt(numberPrefixString, 10)
: undefined;
return {
filename: cleanFileName,
numberPrefix,
filename: match.groups!.suffix!,
numberPrefix: parseInt(match.groups!.numberPrefix!, 10),
};
};

View file

@ -44,7 +44,7 @@ describe('DefaultSidebarItemsGenerator', () => {
expect(sidebarSlice).toEqual([]);
expect(consoleWarn).toHaveBeenCalledWith(
expect.stringMatching(
/.*\[WARNING\].* No docs found in .*\..*: can't auto-generate a sidebar\..*/,
/.*\[WARNING\].* No docs found in [^.]*\..*: can't auto-generate a sidebar\..*/,
),
);
});

View file

@ -134,12 +134,12 @@ describe('loadSidebars', () => {
).rejects.toThrowErrorMatchingInlineSnapshot(`"\\"foo\\" is not allowed"`);
expect(consoleWarnMock).toBeCalledWith(
expect.stringMatching(
/.*\[WARNING].* There are more than one category metadata files for .*foo.*: foo\/_category_.json, foo\/_category_.yml. The behavior is undetermined./,
/.*\[WARNING\].* There are more than one category metadata files for .*foo.*: foo\/_category_.json, foo\/_category_.yml. The behavior is undetermined./,
),
);
expect(consoleErrorMock).toBeCalledWith(
expect.stringMatching(
/.*\[ERROR].* The docs sidebar category metadata file .*foo\/_category_.json.* looks invalid!/,
/.*\[ERROR\].* The docs sidebar category metadata file .*foo\/_category_.json.* looks invalid!/,
),
);
});