From f6bea85422dbeab0331c84967acc4d990b30dd58 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 4 May 2023 22:40:41 -0700 Subject: [PATCH] Remove Object.fromEntries It's not supported across our current browser matrix. That said, our matrix is pretty outdated and we should see about updating it. For example, Object.fromEntries was added in Chrome 73 (March 2019), but we currently build on Chrome 70. --- build/broccoli/strip-glimmer-utilities.js | 31 +++++++++++++++++++++- package.json | 7 +++-- packages/@glimmer/syntax/test/support.ts | 10 +++---- packages/@glimmer/util/index.ts | 2 +- packages/@glimmer/util/lib/object-utils.ts | 18 +++++++++++++ pnpm-lock.yaml | 11 ++++++-- 6 files changed, 66 insertions(+), 13 deletions(-) diff --git a/build/broccoli/strip-glimmer-utilities.js b/build/broccoli/strip-glimmer-utilities.js index 7e8e7a192f..a8540d5298 100644 --- a/build/broccoli/strip-glimmer-utilities.js +++ b/build/broccoli/strip-glimmer-utilities.js @@ -28,7 +28,7 @@ module.exports = function (jsTree) { [ stripGlimmerUtils, { - bindings: ['expect', 'unwrap', 'castToSimple', 'castToBrowser'], + bindings: ['expect', 'unwrap', 'castToSimple', 'castToBrowser', 'asPresentArray'], source: '@glimmer/util', }, ], @@ -82,8 +82,37 @@ function stripFlags(glimmerUtils) { }, 'nuke-vm-metadata', ]); + glimmerUtils.push([ + nuke, + { + source: '@glimmer/util', + delegate: replaceEmpties, + }, + 'replace-empties', + ]); } +/** + * @param {string} bindingName + * @param {import("@babel/traverse").NodePath} path + * @param {import("@babel/types")} t + */ +function replaceEmpties(bindingName, path, t) { + if ( + bindingName === 'EMPTY_STRING_ARRAY' || + bindingName === 'EMPTY_NUMBER_ARRAY' || + bindingName === 'EMPTY_ARRAY' + ) { + path.parentPath.replaceWith(t.arrayExpression()); + } +} + +/** + * + * @param {string} bindingName + * @param {import("@babel/traverse").NodePath} path + * @param {import("@babel/types")} t + */ function removeMetaData(bindingName, path, t) { if (bindingName === 'METADATA') { path.parentPath.replaceWith(t.nullLiteral()); diff --git a/package.json b/package.json index 9b377d59be..d02746e11e 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,11 @@ "@babel/core": "^7.21.8", "@babel/plugin-transform-modules-commonjs": "^7.21.5", "@babel/preset-env": "^7.21.5", + "@babel/traverse": "^7.21.5", + "@babel/types": "^7.21.5", "@glimmer/env": "0.1.7", + "@rollup/plugin-terser": "^0.4.1", + "@types/babel__traverse": "^7.18.5", "@types/node": "^18.16.3", "@types/qunit": "^2.19.5", "@typescript-eslint/eslint-plugin": "^5.59.2", @@ -88,11 +92,11 @@ "eslint-config-prettier": "^8.8.0", "eslint-import-resolver-typescript": "^3.5.5", "eslint-plugin-import": "^2.27.5", - "eslint-plugin-unused-imports": "^2.0.0", "eslint-plugin-n": "^15.7.0", "eslint-plugin-prettier": "^4.2.1", "eslint-plugin-qunit": "^7.3.4", "eslint-plugin-simple-import-sort": "^10.0.0", + "eslint-plugin-unused-imports": "^2.0.0", "execa": "^7.1.1", "glob": "^10.2.2", "js-yaml": "^4.1.0", @@ -104,7 +108,6 @@ "rimraf": "^5.0.0", "rollup": "^3.21.3", "rollup-plugin-sourcemaps": "^0.6.3", - "@rollup/plugin-terser": "^0.4.1", "semver": "^7.5.0", "testem-failure-only-reporter": "^1.0.0", "toml": "^3.0.0", diff --git a/packages/@glimmer/syntax/test/support.ts b/packages/@glimmer/syntax/test/support.ts index 38fce21d58..919042b6fe 100644 --- a/packages/@glimmer/syntax/test/support.ts +++ b/packages/@glimmer/syntax/test/support.ts @@ -1,4 +1,4 @@ -import { entries } from '@glimmer/util'; +import { isObject, mapObject } from '@glimmer/util'; import { AST, preprocess as parse, PreprocessOptions } from '..'; @@ -7,15 +7,11 @@ function normalizeNode(obj: AST.Node | Array): AST.Node | Array(obj: T): T { - if (obj && typeof obj === 'object') { + if (isObject(obj)) { if (Array.isArray(obj)) { return obj.map(normalizeValue) as T; } else { - return Object.fromEntries( - entries(obj).flatMap(([key, value]) => - key === 'loc' ? [] : [[key, normalizeValue(value)]] - ) - ) as T; + return mapObject(obj, (value) => normalizeValue(value)) as T; } } else { return obj; diff --git a/packages/@glimmer/util/index.ts b/packages/@glimmer/util/index.ts index 5ab33e0308..e64b8f56cc 100644 --- a/packages/@glimmer/util/index.ts +++ b/packages/@glimmer/util/index.ts @@ -10,7 +10,7 @@ export { isSerializationFirstNode, SERIALIZATION_FIRST_NODE_STRING, } from './lib/is-serialization-first-node'; -export { assign, entries, fillNulls, values } from './lib/object-utils'; +export { assign, entries, fillNulls, mapObject, values } from './lib/object-utils'; export * from './lib/platform-utils'; export * from './lib/present'; export { diff --git a/packages/@glimmer/util/lib/object-utils.ts b/packages/@glimmer/util/lib/object-utils.ts index 3a1bc08b12..029849027f 100644 --- a/packages/@glimmer/util/lib/object-utils.ts +++ b/packages/@glimmer/util/lib/object-utils.ts @@ -1,3 +1,5 @@ +import { dict } from './collections'; + export let assign = Object.assign; export function fillNulls(count: number): T[] { @@ -19,3 +21,19 @@ export type ObjectEntry = { [P in keyof D]: [P, D[P]] }[keyof export function entries(dict: D): ObjectEntry[] { return Object.entries(dict) as ObjectEntry[]; } + +export function mapObject( + object: T, + mapper: (value: T[keyof T], key: keyof T) => Out | undefined +): { [P in keyof T]: Out } { + let out = dict() as Record; + + Object.entries(object).forEach(([k, v]) => { + const value = mapper(v, k as keyof T); + if (value !== undefined) { + out[k] = value; + } + }); + + return out as { [P in keyof T]: Out }; +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3614cca237..07bc89ebb4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -45,12 +45,21 @@ importers: '@babel/preset-env': specifier: ^7.21.5 version: 7.21.5(@babel/core@7.21.8) + '@babel/traverse': + specifier: ^7.21.5 + version: 7.21.5 + '@babel/types': + specifier: ^7.21.5 + version: 7.21.5 '@glimmer/env': specifier: 0.1.7 version: 0.1.7 '@rollup/plugin-terser': specifier: ^0.4.1 version: 0.4.1(rollup@3.21.3) + '@types/babel__traverse': + specifier: ^7.18.5 + version: 7.18.5 '@types/node': specifier: ^18.16.3 version: 18.16.3 @@ -5874,7 +5883,6 @@ packages: /eslint-config-prettier@8.8.0(eslint@8.39.0): resolution: {integrity: sha512-wLbQiFre3tdGgpDv67NQKnJuTlcUVYHas3k+DZCc2U2BadthoEY4B7hLPvAxaqdyOGCzuLfii2fqGph10va7oA==} - hasBin: true peerDependencies: eslint: '>=7.0.0' dependencies: @@ -11039,7 +11047,6 @@ packages: /update-browserslist-db@1.0.11(browserslist@4.21.5): resolution: {integrity: sha512-dCwEFf0/oT85M1fHBg4F0jtLwJrutGoHSQXCh7u4o2t1drG+c0a9Flnqww6XUKSfQMPpJBRjU8d4RXB09qtvaA==} - hasBin: true peerDependencies: browserslist: '>= 4.21.0' dependencies: