Skip to content

Commit 7869e74

Browse files
authored
refactor(pwa): simplify registerSW code, fix ESLint errors (#7579)
1 parent bada5c1 commit 7869e74

File tree

14 files changed

+204
-247
lines changed

14 files changed

+204
-247
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ module.exports = {
191191
'no-template-curly-in-string': WARNING,
192192
'no-unused-expressions': [WARNING, {allowTaggedTemplates: true}],
193193
'no-useless-escape': WARNING,
194+
'no-void': [ERROR, {allowAsStatement: true}],
194195
'prefer-destructuring': WARNING,
195196
'prefer-named-capture-group': WARNING,
196197
'prefer-template': WARNING,

packages/docusaurus-mdx-loader/src/remark/admonitions/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ const plugin: Plugin = function plugin(
7070
}
7171

7272
const now = eat.now();
73-
const [opening, keyword, title] = match;
73+
const [opening, keyword, title] = match as string[] as [
74+
string,
75+
string,
76+
string,
77+
];
7478
const food = [];
7579
const content = [];
7680

@@ -169,7 +173,7 @@ const plugin: Plugin = function plugin(
169173
visit(
170174
root,
171175
(node: unknown): node is Literal =>
172-
(node as Literal)?.type !== admonitionNodeType,
176+
(node as Literal | undefined)?.type !== admonitionNodeType,
173177
(node: Literal) => {
174178
if (node.value) {
175179
node.value = node.value.replace(escapeTag, options.tag);

packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import visit from 'unist-util-visit';
2121
import escapeHtml from 'escape-html';
2222
import sizeOf from 'image-size';
2323
import type {Transformer} from 'unified';
24+
import type {Parent} from 'unist';
2425
import type {Image, Literal} from 'mdast';
2526

2627
const {
@@ -36,12 +37,13 @@ type Context = PluginOptions & {
3637
filePath: string;
3738
};
3839

40+
type Target = [node: Image, index: number, parent: Parent];
41+
3942
async function toImageRequireNode(
40-
node: Image,
43+
[node, index, parent]: Target,
4144
imagePath: string,
4245
filePath: string,
4346
) {
44-
const jsxNode = node as Literal & Partial<Image>;
4547
let relativeImagePath = posixPath(
4648
path.relative(path.dirname(filePath), imagePath),
4749
);
@@ -75,12 +77,12 @@ ${(err as Error).message}`;
7577
}
7678
}
7779

78-
Object.keys(jsxNode).forEach(
79-
(key) => delete jsxNode[key as keyof typeof jsxNode],
80-
);
80+
const jsxNode: Literal = {
81+
type: 'jsx',
82+
value: `<img ${alt}src={${src}}${title}${width}${height} />`,
83+
};
8184

82-
(jsxNode as Literal).type = 'jsx';
83-
jsxNode.value = `<img ${alt}src={${src}}${title}${width}${height} />`;
85+
parent.children.splice(index, 1, jsxNode);
8486
}
8587

8688
async function ensureImageFileExist(imagePath: string, sourceFilePath: string) {
@@ -129,7 +131,8 @@ async function getImageAbsolutePath(
129131
return imageFilePath;
130132
}
131133

132-
async function processImageNode(node: Image, context: Context) {
134+
async function processImageNode(target: Target, context: Context) {
135+
const [node] = target;
133136
if (!node.url) {
134137
throw new Error(
135138
`Markdown image URL is mandatory in "${toMessageRelativeFilePath(
@@ -151,15 +154,18 @@ async function processImageNode(node: Image, context: Context) {
151154
// We try to convert image urls without protocol to images with require calls
152155
// going through webpack ensures that image assets exist at build time
153156
const imagePath = await getImageAbsolutePath(parsedUrl.pathname, context);
154-
await toImageRequireNode(node, imagePath, context.filePath);
157+
await toImageRequireNode(target, imagePath, context.filePath);
155158
}
156159

157160
export default function plugin(options: PluginOptions): Transformer {
158161
return async (root, vfile) => {
159162
const promises: Promise<void>[] = [];
160-
visit(root, 'image', (node: Image) => {
163+
visit(root, 'image', (node: Image, index, parent) => {
161164
promises.push(
162-
processImageNode(node, {...options, filePath: vfile.path!}),
165+
processImageNode([node, index, parent!], {
166+
...options,
167+
filePath: vfile.path!,
168+
}),
163169
);
164170
});
165171
await Promise.all(promises);

packages/docusaurus-mdx-loader/src/remark/transformLinks/index.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import visit from 'unist-util-visit';
1919
import escapeHtml from 'escape-html';
2020
import {stringifyContent} from '../utils';
2121
import type {Transformer} from 'unified';
22+
import type {Parent} from 'unist';
2223
import type {Link, Literal} from 'mdast';
2324

2425
const {
@@ -34,16 +35,20 @@ type Context = PluginOptions & {
3435
filePath: string;
3536
};
3637

38+
type Target = [node: Link, index: number, parent: Parent];
39+
3740
/**
3841
* Transforms the link node to a JSX `<a>` element with a `require()` call.
3942
*/
40-
function toAssetRequireNode(node: Link, assetPath: string, filePath: string) {
41-
const jsxNode = node as Literal & Partial<Link>;
42-
let relativeAssetPath = posixPath(
43-
path.relative(path.dirname(filePath), assetPath),
44-
);
43+
function toAssetRequireNode(
44+
[node, index, parent]: Target,
45+
assetPath: string,
46+
filePath: string,
47+
) {
4548
// require("assets/file.pdf") means requiring from a package called assets
46-
relativeAssetPath = `./${relativeAssetPath}`;
49+
const relativeAssetPath = `./${posixPath(
50+
path.relative(path.dirname(filePath), assetPath),
51+
)}`;
4752

4853
const parsedUrl = url.parse(node.url);
4954
const hash = parsedUrl.hash ?? '';
@@ -60,12 +65,12 @@ function toAssetRequireNode(node: Link, assetPath: string, filePath: string) {
6065
const children = stringifyContent(node);
6166
const title = node.title ? ` title="${escapeHtml(node.title)}"` : '';
6267

63-
Object.keys(jsxNode).forEach(
64-
(key) => delete jsxNode[key as keyof typeof jsxNode],
65-
);
68+
const jsxNode: Literal = {
69+
type: 'jsx',
70+
value: `<a target="_blank" href={${href}}${title}>${children}</a>`,
71+
};
6672

67-
(jsxNode as Literal).type = 'jsx';
68-
jsxNode.value = `<a target="_blank" href={${href}}${title}>${children}</a>`;
73+
parent.children.splice(index, 1, jsxNode);
6974
}
7075

7176
async function ensureAssetFileExist(assetPath: string, sourceFilePath: string) {
@@ -106,7 +111,8 @@ async function getAssetAbsolutePath(
106111
return null;
107112
}
108113

109-
async function processLinkNode(node: Link, context: Context) {
114+
async function processLinkNode(target: Target, context: Context) {
115+
const [node] = target;
110116
if (!node.url) {
111117
// Try to improve error feedback
112118
// see https://github.com/facebook/docusaurus/issues/3309#issuecomment-690371675
@@ -138,15 +144,20 @@ async function processLinkNode(node: Link, context: Context) {
138144
context,
139145
);
140146
if (assetPath) {
141-
toAssetRequireNode(node, assetPath, context.filePath);
147+
toAssetRequireNode(target, assetPath, context.filePath);
142148
}
143149
}
144150

145151
export default function plugin(options: PluginOptions): Transformer {
146152
return async (root, vfile) => {
147153
const promises: Promise<void>[] = [];
148-
visit(root, 'link', (node: Link) => {
149-
promises.push(processLinkNode(node, {...options, filePath: vfile.path!}));
154+
visit(root, 'link', (node: Link, index, parent) => {
155+
promises.push(
156+
processLinkNode([node, index, parent!], {
157+
...options,
158+
filePath: vfile.path!,
159+
}),
160+
);
150161
});
151162
await Promise.all(promises);
152163
};

packages/docusaurus-migrate/bin/index.mjs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ cli
3434
.option('--mdx', 'try to migrate MD to MDX too')
3535
.option('--page', 'try to migrate pages too')
3636
.description('Migrate between versions of Docusaurus website.')
37-
.action((siteDir = '.', newDir = '.', {mdx, page} = {}) => {
37+
.action(async (siteDir = '.', newDir = '.', {mdx, page} = {}) => {
3838
const sitePath = path.resolve(siteDir);
3939
const newSitePath = path.resolve(newDir);
40-
migrateDocusaurusProject(sitePath, newSitePath, mdx, page);
40+
await migrateDocusaurusProject(sitePath, newSitePath, mdx, page);
4141
});
4242

4343
cli
4444
.command('mdx [siteDir] [newDir]')
4545
.description('Migrate markdown files to MDX.')
46-
.action((siteDir = '.', newDir = '.') => {
46+
.action(async (siteDir = '.', newDir = '.') => {
4747
const sitePath = path.resolve(siteDir);
4848
const newSitePath = path.resolve(newDir);
49-
migrateMDToMDX(sitePath, newSitePath);
49+
await migrateMDToMDX(sitePath, newSitePath);
5050
});
5151

5252
cli.parse(process.argv);

packages/docusaurus-plugin-content-docs/src/__tests__/lastUpdate.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('getFileLastUpdate', () => {
2222
const lastUpdateData = await getFileLastUpdate(existingFilePath);
2323
expect(lastUpdateData).not.toBeNull();
2424

25-
const {author, timestamp} = lastUpdateData;
25+
const {author, timestamp} = lastUpdateData!;
2626
expect(author).not.toBeNull();
2727
expect(typeof author).toBe('string');
2828

@@ -38,7 +38,7 @@ describe('getFileLastUpdate', () => {
3838
const lastUpdateData = await getFileLastUpdate(filePathWithSpace);
3939
expect(lastUpdateData).not.toBeNull();
4040

41-
const {author, timestamp} = lastUpdateData;
41+
const {author, timestamp} = lastUpdateData!;
4242
expect(author).not.toBeNull();
4343
expect(typeof author).toBe('string');
4444

@@ -61,8 +61,6 @@ describe('getFileLastUpdate', () => {
6161
expect(consoleMock).toHaveBeenLastCalledWith(
6262
expect.stringMatching(/because the file does not exist./),
6363
);
64-
await expect(getFileLastUpdate(null)).resolves.toBeNull();
65-
await expect(getFileLastUpdate(undefined)).resolves.toBeNull();
6664
consoleMock.mockRestore();
6765
});
6866

packages/docusaurus-plugin-content-docs/src/lastUpdate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ let showedGitRequirementError = false;
1616
let showedFileNotTrackedError = false;
1717

1818
export async function getFileLastUpdate(
19-
filePath?: string,
19+
filePath: string,
2020
): Promise<{timestamp: number; author: string} | null> {
2121
if (!filePath) {
2222
return null;

0 commit comments

Comments
 (0)