New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[fix] add all global objects/functions #7786
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
99cc26d
add global objects/interfaces extractor
baseballyama 07a2aa0
remove typescript interfaces
baseballyama 86d1f80
exract all globals and output to ts file
baseballyama 3878915
prevent to fetch same file multiple times
baseballyama 29080a0
remove useless globals
baseballyama 433996c
stop to read declare type and support backward compatible
baseballyama 0a62807
use backslash
baseballyama b5de456
Merge branch 'master' into chore/lib-dom
baseballyama dda3784
update changelog
baseballyama File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** ---------------------------------------------------------------------- | ||
This script gets a list of global objects/functions of browser. | ||
This process is simple for now, so it is handled without AST parser. | ||
Please run `node scripts/globals-extractor.mjs` at the project root. | ||
|
||
see: https://github.com/microsoft/TypeScript/tree/main/lib | ||
---------------------------------------------------------------------- */ | ||
|
||
import http from 'https'; | ||
import fs from 'fs'; | ||
|
||
// MEMO: add additional objects/functions which existed in `src/compiler/utils/names.ts` | ||
// before this script was introduced but could not be retrieved by this process. | ||
const SPECIALS = ['global', 'globalThis', 'InternalError', 'process', 'undefined']; | ||
|
||
const get_url = (name) => `https://raw.githubusercontent.com/microsoft/TypeScript/main/lib/lib.${name}.d.ts`; | ||
const extract_name = (split) => split.match(/^[a-zA-Z0-9_$]+/)[0]; | ||
|
||
const extract_functions_and_references = (name, data) => { | ||
const functions = []; | ||
const references = []; | ||
data.split('\n').forEach(line => { | ||
const trimmed = line.trim(); | ||
const split = trimmed.replace(/[\s+]/, ' ').split(' '); | ||
if (split[0] === 'declare') { | ||
// MEMO: ignore `declare type xxx` statement in lib.es5.d.ts. | ||
// Because all of these are TypeScript types. (not exists in runtime.) | ||
if (name !== 'es5' || split[1] !== 'type') functions.push(extract_name(split[2])); | ||
} else if (trimmed.startsWith('/// <reference')) { | ||
const matched = trimmed.match(/ lib="(.+)"/); | ||
const reference = matched && matched[1]; | ||
if (reference) references.push(reference); | ||
} | ||
}); | ||
return { functions, references }; | ||
}; | ||
|
||
const do_get = (url) => new Promise((resolve, reject) => { | ||
http.get(url, (res) => { | ||
let body = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', (chunk) => body += chunk); | ||
res.on('end', () => resolve(body)); | ||
}).on('error', (e) => { | ||
console.error(e.message); | ||
reject(e); | ||
}); | ||
}); | ||
|
||
const fetched_names = new Set(); | ||
const get_functions = async (name) => { | ||
const res = []; | ||
if (fetched_names.has(name)) return res; | ||
fetched_names.add(name); | ||
const body = await do_get(get_url(name)); | ||
const { functions, references } = extract_functions_and_references(name, body); | ||
res.push(...functions); | ||
const chile_functions = await Promise.all(references.map(get_functions)); | ||
chile_functions.forEach(i => res.push(...i)); | ||
return res; | ||
}; | ||
|
||
const build_output = (functions) => { | ||
const sorted = Array.from(new Set(functions.sort())); | ||
return ` | ||
/** ---------------------------------------------------------------------- | ||
This file is automatically generated by \`scripts/globals-extractor.mjs\`. | ||
Generated At: ${new Date().toISOString()} | ||
---------------------------------------------------------------------- */ | ||
|
||
export default new Set([ | ||
${sorted.map((i) => `\t'${i}'`).join(',\n')} | ||
]); | ||
`.substring(1); | ||
}; | ||
|
||
(async () => { | ||
const functions = await get_functions('es2021.full'); | ||
functions.push(...SPECIALS); | ||
fs.writeFileSync('src/compiler/utils/globals.ts', build_output(functions)); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tiny nit: we can start with a backslash to prevent the substring call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed! 0a62807