Skip to content

Commit

Permalink
Remove Object.fromEntries
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wycats committed May 5, 2023
1 parent ad48cc9 commit f6bea85
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
31 changes: 30 additions & 1 deletion build/broccoli/strip-glimmer-utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function (jsTree) {
[
stripGlimmerUtils,
{
bindings: ['expect', 'unwrap', 'castToSimple', 'castToBrowser'],
bindings: ['expect', 'unwrap', 'castToSimple', 'castToBrowser', 'asPresentArray'],
source: '@glimmer/util',
},
],
Expand Down Expand Up @@ -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());
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
10 changes: 3 additions & 7 deletions packages/@glimmer/syntax/test/support.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { entries } from '@glimmer/util';
import { isObject, mapObject } from '@glimmer/util';

import { AST, preprocess as parse, PreprocessOptions } from '..';

Expand All @@ -7,15 +7,11 @@ function normalizeNode(obj: AST.Node | Array<AST.Node>): AST.Node | Array<AST.No
}

function normalizeValue<T extends AST.Node | AST.Node[] | unknown>(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;
Expand Down
2 changes: 1 addition & 1 deletion packages/@glimmer/util/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions packages/@glimmer/util/lib/object-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dict } from './collections';

export let assign = Object.assign;

export function fillNulls<T>(count: number): T[] {
Expand All @@ -19,3 +21,19 @@ export type ObjectEntry<D extends object> = { [P in keyof D]: [P, D[P]] }[keyof
export function entries<D extends object>(dict: D): ObjectEntry<D>[] {
return Object.entries(dict) as ObjectEntry<D>[];
}

export function mapObject<T extends object, Out>(
object: T,
mapper: (value: T[keyof T], key: keyof T) => Out | undefined
): { [P in keyof T]: Out } {
let out = dict() as Record<string, Out>;

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 };
}
11 changes: 9 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f6bea85

Please sign in to comment.