Skip to content

Commit feb8dfc

Browse files
authored
[fix] add all global objects / functions (#7786)
1 parent f3f3d07 commit feb8dfc

File tree

5 files changed

+937
-66
lines changed

5 files changed

+937
-66
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Svelte changelog
22

3+
## Unreleased
4+
5+
* Add all global objects / functions ([#3805](https://github.com/sveltejs/svelte/issue/3805), [#7223](https://github.com/sveltejs/svelte/issue/7223), [#7240](https://github.com/sveltejs/svelte/issue/7240))
6+
37
## 3.50.0
48

59
* Add a11y warnings:

scripts/globals-extractor.mjs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/** ----------------------------------------------------------------------
2+
This script gets a list of global objects/functions of browser.
3+
This process is simple for now, so it is handled without AST parser.
4+
Please run `node scripts/globals-extractor.mjs` at the project root.
5+
6+
see: https://github.com/microsoft/TypeScript/tree/main/lib
7+
---------------------------------------------------------------------- */
8+
9+
import http from 'https';
10+
import fs from 'fs';
11+
12+
const GLOBAL_TS_PATH = './src/compiler/utils/globals.ts';
13+
14+
// MEMO: add additional objects/functions which existed in `src/compiler/utils/names.ts`
15+
// before this script was introduced but could not be retrieved by this process.
16+
const SPECIALS = ['global', 'globalThis', 'InternalError', 'process', 'undefined'];
17+
18+
const get_url = (name) => `https://raw.githubusercontent.com/microsoft/TypeScript/main/lib/lib.${name}.d.ts`;
19+
const extract_name = (split) => split.match(/^[a-zA-Z0-9_$]+/)[0];
20+
21+
const extract_functions_and_references = (name, data) => {
22+
const functions = [];
23+
const references = [];
24+
data.split('\n').forEach(line => {
25+
const trimmed = line.trim();
26+
const split = trimmed.replace(/[\s+]/, ' ').split(' ');
27+
if (split[0] === 'declare' && split[1] !== 'type') {
28+
functions.push(extract_name(split[2]));
29+
} else if (trimmed.startsWith('/// <reference')) {
30+
const matched = trimmed.match(/ lib="(.+)"/);
31+
const reference = matched && matched[1];
32+
if (reference) references.push(reference);
33+
}
34+
});
35+
return { functions, references };
36+
};
37+
38+
const do_get = (url) => new Promise((resolve, reject) => {
39+
http.get(url, (res) => {
40+
let body = '';
41+
res.setEncoding('utf8');
42+
res.on('data', (chunk) => body += chunk);
43+
res.on('end', () => resolve(body));
44+
}).on('error', (e) => {
45+
console.error(e.message);
46+
reject(e);
47+
});
48+
});
49+
50+
const fetched_names = new Set();
51+
const get_functions = async (name) => {
52+
const res = [];
53+
if (fetched_names.has(name)) return res;
54+
fetched_names.add(name);
55+
const body = await do_get(get_url(name));
56+
const { functions, references } = extract_functions_and_references(name, body);
57+
res.push(...functions);
58+
const chile_functions = await Promise.all(references.map(get_functions));
59+
chile_functions.forEach(i => res.push(...i));
60+
return res;
61+
};
62+
63+
const build_output = (functions) => {
64+
const sorted = Array.from(new Set(functions.sort()));
65+
return `\
66+
/** ----------------------------------------------------------------------
67+
This file is automatically generated by \`scripts/globals-extractor.mjs\`.
68+
Generated At: ${new Date().toISOString()}
69+
---------------------------------------------------------------------- */
70+
71+
export default new Set([
72+
${sorted.map((i) => `\t'${i}'`).join(',\n')}
73+
]);
74+
`;
75+
};
76+
77+
const get_exists_globals = () => {
78+
const regexp = /^\s*["'](.+)["'],?\s*$/;
79+
return fs.readFileSync(GLOBAL_TS_PATH, 'utf8')
80+
.split('\n')
81+
.filter(line => line.match(regexp))
82+
.map(line => line.match(regexp)[1]);
83+
};
84+
85+
(async () => {
86+
const globals = get_exists_globals();
87+
const new_globals = await get_functions('es2021.full');
88+
globals.forEach((g) => new_globals.push(g));
89+
SPECIALS.forEach((g) => new_globals.push(g));
90+
fs.writeFileSync(GLOBAL_TS_PATH, build_output(new_globals));
91+
})();

src/compiler/compile/Component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { walk } from 'estree-walker';
22
import { getLocator } from 'locate-character';
33
import Stats from '../Stats';
4-
import { globals, reserved, is_valid } from '../utils/names';
4+
import { reserved, is_valid } from '../utils/names';
5+
import globals from '../utils/globals';
56
import { namespaces, valid_namespaces } from '../utils/namespaces';
67
import create_module from './create_module';
78
import {

0 commit comments

Comments
 (0)