Skip to content

Commit

Permalink
Update dependencies (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Feb 19, 2024
1 parent 9b90306 commit 4ff98bc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expectType, expectError, expectAssignable} from 'tsd';
import {ReadonlyDeep} from 'type-fest';
import globals from '.';
import {type ReadonlyDeep} from 'type-fest';
import globals from './index.js';

expectAssignable<ReadonlyDeep<Record<string, Record<string, boolean>>>>(globals);
expectType<false>(globals.builtin.Array);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@
"environments"
],
"devDependencies": {
"ava": "^2.4.0",
"ava": "^6.1.1",
"cheerio": "^1.0.0-rc.12",
"tsd": "^0.30.4",
"type-fest": "^4.10.2",
"xo": "^0.36.1"
"xo": "^0.57.0"
},
"xo": {
"ignores": [
"get-browser-globals.js"
],
"rules": {
"node/no-unsupported-features/es-syntax": "off"
"unicorn/prefer-module": "off"
}
},
"tsd": {
Expand Down
14 changes: 6 additions & 8 deletions scripts/generate-types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const DATA_FILE = new URL('../globals.json', import.meta.url);

const globals = JSON.parse(await fs.readFile(DATA_FILE));

const getGroupTypeName = (group) => `Globals${group[0].toUpperCase() + group.slice(1).replaceAll('-', '')}`;
const getGroupTypeName = group => `Globals${group[0].toUpperCase() + group.slice(1).replaceAll('-', '')}`;

const groups = {};
const output = [
'// This file is autogenerated by scripts/generate-types.mjs',
'// Do NOT modify this file manually\n'
'// Do NOT modify this file manually\n',
];

for (const group in globals) {
for (const group of Object.keys(globals)) {
const groupType = getGroupTypeName(group);
groups[group] = groupType;

Expand All @@ -22,17 +22,15 @@ for (const group in globals) {
output.push(` readonly '${rule}': ${status};`);
}

output.push(`}\n`);
output.push('}\n');
}

output.push(`type Globals = {`);
output.push('type Globals = {');

for (const [group, groupType] of Object.entries(groups)) {
output.push(` readonly '${group}': ${groupType};`);
}

output.push(`}\n`);
output.push(`declare const globals: Globals;\n`);
output.push(`export = globals;`);
output.push('}\n', 'declare const globals: Globals;\n', 'export = globals;');

console.log(output.join('\n'));
39 changes: 20 additions & 19 deletions scripts/get-builtin-globals.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ const SPECIFICATION_URLS = [
const CACHE_FILE = new URL('../.cache/spec.html', import.meta.url);
const DATA_FILE = new URL('../globals.json', import.meta.url);

const getText = async (url) => {
const getText = async url => {
const response = await fetch(url);
const text = await response.text();
return text;
};

const any = async (asyncFunctions) => {
const any = async asyncFunctions => {
const errors = [];
for (const function_ of asyncFunctions) {
try {
// eslint-disable-next-line no-await-in-loop
return await function_();
} catch (error) {
errors.push(error);
Expand All @@ -42,32 +43,32 @@ const getSpecification = async () => {
await fs.rm(CACHE_FILE);
}

const text = await any(SPECIFICATION_URLS.map((url) => () => getText(url)));
const text = await any(SPECIFICATION_URLS.map(url => () => getText(url)));

await fs.mkdir(new URL('./', CACHE_FILE), { recursive: true });
await fs.mkdir(new URL('./', CACHE_FILE), {recursive: true});
await fs.writeFile(CACHE_FILE, text);

return text;
};

function* getGlobalObjects(specification) {
function * getGlobalObjects(specification) {
const $ = cheerio.load(specification);
for (const element of $('emu-clause#sec-global-object > emu-clause h1')) {
const property = $(element).text().trim().split(/\s/)[0];
const descriptor = Object.getOwnPropertyDescriptor(globalThis, property);
if (descriptor) {
yield { property, descriptor };
yield {property, descriptor};
}
}

// Annex B
yield* ['escape', 'unescape'].map((property) => ({
yield * ['escape', 'unescape'].map(property => ({
property,
descriptor: Object.getOwnPropertyDescriptor(globalThis, property),
}));
}

function* getObjectProperties(specification) {
function * getObjectProperties(specification) {
const $ = cheerio.load(specification);

for (const element of $('emu-clause#sec-properties-of-the-object-prototype-object > emu-clause > h1')) {
Expand All @@ -84,29 +85,29 @@ function* getObjectProperties(specification) {

const descriptor = Object.getOwnPropertyDescriptor(
Object.prototype,
property
property,
);
if (descriptor) {
yield { property, descriptor };
yield {property, descriptor};
}
}
}

let specification = await getSpecification();
const specification = await getSpecification();
const builtinGlobals = Object.fromEntries(
[
...getGlobalObjects(specification),
// `globalThis` is an object
...getObjectProperties(specification),
]
.sort(({ property: propertyA }, { property: propertyB }) =>
propertyA.localeCompare(propertyB)
.sort(({property: propertyA}, {property: propertyB}) =>
propertyA.localeCompare(propertyB),
)
.map(({ property }) => [
.map(({property}) => [
property,
// Most of these except `Infinity`, `NaN`, `undefined` are actually writable/configurable
false,
])
]),
);

const globals = JSON.parse(await fs.readFile(DATA_FILE));
Expand All @@ -116,18 +117,18 @@ globals.builtin = builtinGlobals;
await fs.writeFile(DATA_FILE, JSON.stringify(globals, undefined, '\t') + '\n');

const added = Object.keys(builtinGlobals).filter(
(property) => !originalGlobals.includes(property)
property => !originalGlobals.includes(property),
);
const removed = originalGlobals.filter(
(property) => !Object.hasOwn(builtinGlobals)
property => !Object.hasOwn(builtinGlobals, property),
);

console.log(`
✅ Builtin globals updated.
Added(${added.length}):
${added.map((property) => ` - ${property}`).join('\n') || 'None'}
${added.map(property => ` - ${property}`).join('\n') || 'None'}
Removed(${removed.length}):
${removed.map((property) => ` - ${property}`).join('\n') || 'None'}
${removed.map(property => ` - ${property}`).join('\n') || 'None'}
`);
16 changes: 8 additions & 8 deletions test.js → test.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'ava';
import globals from '.';
import globals from './index.js';

test('main', t => {
t.is(typeof globals, 'object');
Expand All @@ -10,31 +10,31 @@ test('ensure alphabetical order', t => {
for (const env of Object.keys(globals)) {
const keys = Object.keys(globals[env]);
t.deepEqual(
keys.slice(), keys.sort((a, b) => a.localeCompare(b)),
`The \`${env}\` keys don't have the correct alphabetical order`
[...keys], keys.sort((a, b) => a.localeCompare(b)),
`The \`${env}\` keys don't have the correct alphabetical order`,
);
}
});

test('`node` is `nodeBuiltin` with CommonJS arguments', t => {
// `globals.node` has `global`` which isn't a CommonJS argument and doesn't include
// `__filename` and `__dirname` which are.
const commonjsArgs = {
const commonjsArguments = {
__dirname: false,
__filename: false,
exports: true,
module: false,
require: false
require: false,
};

t.deepEqual({...globals.nodeBuiltin, ...commonjsArgs}, globals.node);
t.deepEqual({...globals.nodeBuiltin, ...commonjsArguments}, globals.node);

// Ensure that there's no overlap between true globals and the CommonJS arguments above.
for (const builtin of Object.keys(globals.nodeBuiltin)) {
t.is(
commonjsArgs[builtin],
commonjsArguments[builtin],
undefined,
`The builtin ${builtin} is not a CommonJS argument`
`The builtin ${builtin} is not a CommonJS argument`,
);
}
});

0 comments on commit 4ff98bc

Please sign in to comment.