Skip to content
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

object spread: also drop non-nullish constants (e.g. { ...true }) #1141

Merged
merged 1 commit into from Apr 23, 2024

Conversation

WofWca
Copy link
Contributor

@WofWca WofWca commented Feb 6, 2022

This is a follow-up to 6f04338 (#1004)

Note, as I said in #1004, I have not yet found where in the spec it says how spreading constants other than undefined & null should be handled.

Copy link
Collaborator

@jridgewell jridgewell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spec says:

PropertyDefinition : ... AssignmentExpression

  1. Let exprValue be the result of evaluating AssignmentExpression.
  2. Let fromValue be ? GetValue(exprValue).
  3. Let excludedNames be a new empty List.
  4. Return ? CopyDataProperties(object, fromValue, excludedNames).

CopyDataProperties says

7.3.26 CopyDataProperties ( target, source, excludedItems )

The abstract operation CopyDataProperties takes arguments target (an Object), source (an ECMAScript language value), and excludedItems (a List of property keys). It performs the following steps when called:

  1. If source is undefined or null, return target.
  2. Let from be ! ToObject(source).
  3. Let keys be ? from.[[OwnPropertyKeys]]().
  4. For each element nextKey of keys, do
    a. Let excluded be false.
    b. For each element e of excludedItems, do
    i. If SameValue(e, nextKey) is true, then
    1. Set excluded to true.
      c. If excluded is false, then
      i. Let desc be ? from.[[GetOwnProperty]](nextKey).
      ii. If desc is not undefined and desc.[[Enumerable]] is true, then
    2. Let propValue be ? Get(from, nextKey).
    3. Perform ! CreateDataPropertyOrThrow(target, nextKey, propValue).
  5. Return target.

The important part is:

2. Let from be ! [ToObject](https://tc39.es/ecma262/multipage/abstract-operations.html#sec-toobject)(source).
3. Let keys be ? from.[[OwnPropertyKeys]]().

ToObject of a primitive returns a new object instance (eg, new Number(1), which is a Number instance and not a number primitive). This also applies when spreading a "string", and String instances are the only primitive class which has an own-property, which is what spread operator is iterating over.

lib/compress/index.js Outdated Show resolved Hide resolved
@WofWca
Copy link
Contributor Author

WofWca commented Feb 7, 2022

Just realized that it stopped droping AST_RegExp. I think it can still be merged as it's still an improvement overall.

lib/compress/index.js Outdated Show resolved Hide resolved
@WofWca WofWca force-pushed the object-spread-drop-constants branch from 51646fc to 07b8d32 Compare February 9, 2022 16:27
@WofWca
Copy link
Contributor Author

WofWca commented Feb 9, 2022

Alright, I've decided to update it so that it also drops AST_RegExps by explicitly checking if expr instanceof AST_Constant. Maybe is_constant() really could used a rename (#1144 (comment))

@WofWca WofWca force-pushed the object-spread-drop-constants branch from 07b8d32 to 2e226c1 Compare February 9, 2022 16:35
@fabiosantoscode
Copy link
Collaborator

This looks great, is it good to merge @jridgewell ?

@WofWca
Copy link
Contributor Author

WofWca commented Apr 23, 2024

So, what's the plan for this MR?

@fabiosantoscode
Copy link
Collaborator

This looks good. I kind of dropped the ball here, sorry!

@fabiosantoscode fabiosantoscode merged commit db932f0 into terser:master Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants