Skip to content

Commit b345f8e

Browse files
committed
feat(typemap): refresh from mdn
1 parent f0bc8d4 commit b345f8e

File tree

15 files changed

+1233
-257
lines changed

15 files changed

+1233
-257
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Update Type Map
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 0 * * 0'
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
type-map-update:
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Harden Runner
18+
uses: step-security/harden-runner@a90bcbc6539c36a85cdfeb73f7e2f433735f215b # v2.15.0
19+
with:
20+
egress-policy: audit
21+
22+
- name: Git Checkout
23+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
24+
25+
- run: node scripts/update-type-map.mjs
26+
27+
- name: Open pull request
28+
uses: gr2m/create-or-update-pull-request-action@b65137ca591da0b9f43bad7b24df13050ea45d1b # v1.10.1
29+
# Creates a PR or update the Action's existing PR, or
30+
# no-op if the base branch is already up-to-date.
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
with:
34+
update-pull-request-title-and-body: true
35+
branch: chore/update-types
36+
body: |
37+
Updates the type map
38+
39+
cc @nodejs/web-infra
40+
41+
Check this workflow's logs at ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}.
42+
commit-message: 'meta: update type map'
43+
title: 'meta: update type map'
44+
draft: true

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ src/generators/web/template.html
99

1010
# Output
1111
out/
12+
13+
# Generated Files
14+
src/generators/metadata/maps/mdn.json

package-lock.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@node-core/doc-kit",
33
"type": "module",
4-
"version": "1.2.0",
4+
"version": "1.2.1",
55
"repository": {
66
"type": "git",
77
"url": "git+https://github.com/nodejs/doc-kit.git"
@@ -37,6 +37,7 @@
3737
"eslint-plugin-import-x": "^4.16.2",
3838
"eslint-plugin-jsdoc": "^62.8.0",
3939
"eslint-plugin-react-x": "^3.0.0",
40+
"globals": "~17.3.0",
4041
"husky": "^9.1.7",
4142
"lint-staged": "^16.4.0",
4243
"prettier": "3.8.1"
@@ -58,7 +59,6 @@
5859
"estree-util-visit": "^2.0.0",
5960
"github-slugger": "^2.0.0",
6061
"glob-parent": "^6.0.2",
61-
"globals": "^17.3.0",
6262
"hast-util-to-string": "^3.0.1",
6363
"hastscript": "^9.0.1",
6464
"lightningcss-wasm": "^1.32.0",

scripts/comparators/constants.mjs

Lines changed: 0 additions & 10 deletions
This file was deleted.

scripts/constants.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { fileURLToPath } from 'node:url';
2+
3+
// Comparator Constants
4+
export const BASE =
5+
process.env.BASE || fileURLToPath(import.meta.resolve('../base'));
6+
7+
export const HEAD =
8+
process.env.HEAD || fileURLToPath(import.meta.resolve('../out'));
9+
10+
export const TITLE =
11+
process.env.TITLE || `## \`${process.env.GENERATOR ?? '...'}\` Generator`;
12+
13+
// MDN Constants
14+
export const MDN_COMPAT_URL =
15+
'https://github.com/mdn/browser-compat-data/releases/latest/download/data.json';
16+
17+
export const MDN_TYPE_MAP = fileURLToPath(
18+
import.meta.resolve('../src/generators/metadata/maps/mdn.json')
19+
);

scripts/update-type-map.mjs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { writeFile } from 'node:fs/promises';
2+
3+
import { MDN_COMPAT_URL, MDN_TYPE_MAP } from './constants.mjs';
4+
import { loadFromURL } from '../src/utils/url.mjs';
5+
6+
const compat = JSON.parse(await loadFromURL(MDN_COMPAT_URL));
7+
8+
const creatingMapping = obj =>
9+
Object.fromEntries(
10+
Object.entries(obj)
11+
.map(([k, v]) => [k.toLowerCase(), v.__compat.mdn_url])
12+
.filter(([, v]) => v)
13+
);
14+
15+
const map = {
16+
...creatingMapping(compat.api),
17+
...creatingMapping(compat.javascript.builtins),
18+
};
19+
20+
writeFile(MDN_TYPE_MAP, JSON.stringify(map, null, 2));

src/generators/metadata/constants.mjs

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
// we don't need to check it for stability references
33
export const IGNORE_STABILITY_STEMS = ['documentation'];
44

5-
import globals from 'globals';
6-
75
// These are string replacements specific to Node.js API docs for anchor IDs
86
export const DOC_API_SLUGS_REPLACEMENTS = [
97
{ from: /node.js/i, to: 'nodejs' }, // Replace Node.js
@@ -14,21 +12,6 @@ export const DOC_API_SLUGS_REPLACEMENTS = [
1412
{ from: /^(?!-+$).*?(--+)/g, to: '-' }, // Replace multiple hyphens
1513
];
1614

17-
// This is the base URL of the MDN Web documentation
18-
export const DOC_MDN_BASE_URL = 'https://developer.mozilla.org/en-US/docs/Web/';
19-
20-
// This is the base URL of the Man7 documentation
21-
export const DOC_MAN_BASE_URL = 'http://man7.org/linux/man-pages/man';
22-
23-
// This is the base URL for the MDN JavaScript documentation
24-
export const DOC_MDN_BASE_URL_JS = `${DOC_MDN_BASE_URL}JavaScript/`;
25-
26-
// This is the base URL for the MDN JavaScript primitives documentation
27-
export const DOC_MDN_BASE_URL_JS_PRIMITIVES = `${DOC_MDN_BASE_URL_JS}Data_structures`;
28-
29-
// This is the base URL for the MDN JavaScript global objects documentation
30-
export const DOC_MDN_BASE_URL_JS_GLOBALS = `${DOC_MDN_BASE_URL_JS}Reference/Global_Objects/`;
31-
3215
// These are regular expressions used to determine if a given Markdown heading
3316
// is a specific type of API Doc entry (e.g., Event, Class, Method, etc)
3417
// and to extract the inner content of said Heading to be used as the API doc entry name
@@ -76,60 +59,5 @@ export const DOC_API_HEADING_TYPES = [
7659
// This regex is used to match basic TypeScript generic types (e.g., Promise<string>)
7760
export const TYPE_GENERIC_REGEX = /^([^<]+)<([^>]+)>$/;
7861

79-
// This is a mapping for types within the Markdown content and their respective
80-
// JavaScript primitive types within the MDN JavaScript docs
81-
// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Data_structures#primitive_values
82-
export const DOC_TYPES_MAPPING_PRIMITIVES = {
83-
null: 'null',
84-
undefined: 'undefined',
85-
boolean: 'boolean',
86-
number: 'number',
87-
bigint: 'bigint',
88-
string: 'string',
89-
symbol: 'symbol',
90-
integer: 'number',
91-
};
92-
93-
// This is a mapping for types within the Markdown content and their respective
94-
// JavaScript globals types within the MDN JavaScript docs
95-
// @see DOC_MDN_BASE_URL_JS_GLOBALS
96-
export const DOC_TYPES_MAPPING_GLOBALS = {
97-
// This is updated with every ES-spec, so, as long as the
98-
// `globals` package is up-to-date, so will our globals list.
99-
...Object.fromEntries(Object.keys(globals.builtin).map(e => [e, e])),
100-
AsyncGeneratorFunction: 'AsyncGeneratorFunction',
101-
AsyncIterator: 'AsyncIterator',
102-
AsyncFunction: 'AsyncFunction',
103-
TypedArray: 'TypedArray',
104-
ErrorEvent: 'ErrorEvent',
105-
'WebAssembly.Instance': 'WebAssembly/Instance',
106-
};
107-
108-
// This is a mapping for miscellaneous types within the Markdown content and their respective
109-
// external reference on appropriate 3rd-party vendors/documentation sites.
110-
export const DOC_TYPES_MAPPING_OTHER = {
111-
any: `${DOC_MDN_BASE_URL_JS_PRIMITIVES}#Data_types`,
112-
this: `${DOC_MDN_BASE_URL_JS}Reference/Operators/this`,
113-
114-
ArrayBufferView: `${DOC_MDN_BASE_URL}/API/ArrayBufferView`,
115-
116-
AsyncIterable: 'https://tc39.github.io/ecma262/#sec-asynciterable-interface',
117-
118-
'Module Namespace Object':
119-
'https://tc39.github.io/ecma262/#sec-module-namespace-exotic-objects',
120-
121-
Iterable: `${DOC_MDN_BASE_URL_JS}Reference/Iteration_protocols#The_iterable_protocol`,
122-
123-
CloseEvent: `${DOC_MDN_BASE_URL}/API/CloseEvent`,
124-
EventSource: `${DOC_MDN_BASE_URL}/API/EventSource`,
125-
MessageEvent: `${DOC_MDN_BASE_URL}/API/MessageEvent`,
126-
127-
DOMException: `${DOC_MDN_BASE_URL}/API/DOMException`,
128-
Storage: `${DOC_MDN_BASE_URL}/API/Storage`,
129-
WebSocket: `${DOC_MDN_BASE_URL}/API/WebSocket`,
130-
131-
FormData: `${DOC_MDN_BASE_URL}API/FormData`,
132-
Headers: `${DOC_MDN_BASE_URL}/API/Headers`,
133-
Response: `${DOC_MDN_BASE_URL}/API/Response`,
134-
Request: `${DOC_MDN_BASE_URL}/API/Request`,
135-
};
62+
// This is the base URL of the Man7 documentation
63+
export const DOC_MAN_BASE_URL = 'http://man7.org/linux/man-pages/man';

src/generators/metadata/generate.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
import { parseApiDoc } from './utils/parse.mjs';
4+
import { parseTypeMap } from '../../parsers/json.mjs';
45
import getConfig from '../../utils/configuration/index.mjs';
5-
import { importFromURL } from '../../utils/url.mjs';
66

77
/**
88
* Process a chunk of API doc files in a worker thread.
@@ -28,7 +28,7 @@ export async function processChunk(fullInput, itemIndices, typeMap) {
2828
export async function* generate(inputs, worker) {
2929
const { metadata: config } = getConfig();
3030

31-
const typeMap = await importFromURL(config.typeMap);
31+
const typeMap = await parseTypeMap(config.typeMap);
3232

3333
// Stream chunks as they complete - allows dependent generators
3434
// to start collecting/preparing while we're still processing

src/generators/metadata/index.mjs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,5 @@ export default createLazyGenerator({
1616

1717
dependsOn: 'ast',
1818

19-
defaultConfiguration: {
20-
typeMap: import.meta.resolve('./typeMap.json'),
21-
},
22-
2319
hasParallelProcessor: true,
2420
});

0 commit comments

Comments
 (0)