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

Feature request: Spread syntax support in object literals #401

Open
bunglegrind opened this issue May 20, 2022 · 4 comments
Open

Feature request: Spread syntax support in object literals #401

bunglegrind opened this issue May 20, 2022 · 4 comments

Comments

@bunglegrind
Copy link

bunglegrind commented May 20, 2022

Is your feature request related to a problem? Please describe.
It looks like the spread syntax in object literals is not supported in JSLint, possibly because when "how javascript works" was written wasn't still part of the ECMAScript standard.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

example:

import parseq from "./parseq.js";

export default Object.freeze({
    ...parseq
});

Describe the solution you'd like
Are there any possibilities to support spread syntax? Or there are good reasons to avoid it?

@kaizhu256
Copy link
Member

kinda leaning against at moment ...

  • pro
    • do see some benefit in ergonomics
  • con
    • impacts program-correctness with subtle, order-related bugs when property-names collide
    • messes up current jslint-rule about keeping object-properties ascii-ordered
let foo = { aa: 1, bb: 2 };
let bar = {        bb: 3 };

// below feels like a bug-in-waiting
// where user needs to worry about property orderings:

console.log({ ...foo, ...bar, cc: 4 });
// { aa: 1, bb: 3, cc: 4 }
console.log({ ...bar, ...foo, cc: 4 });
// { aa: 1, bb: 2, cc: 4 }

Using similar Object.assign() would make the ordering clearer (but yea, it is less ergonomic):

let foo = { aa: 1, bb: 2 };
let bar = {        bb: 3 };

console.log(Object.assign({}, foo, bar, { cc: 4 }));
// { aa: 1, bb: 3, cc: 4 }
console.log(Object.assign({}, bar, foo, { cc: 4 }));
// { aa: 1, bb: 2, cc: 4 }

@bunglegrind
Copy link
Author

Just to add another point to your considerations: between Object.assign and the spread syntax, the former mutates the first argument, whereas the latter not.

@kaizhu256
Copy link
Member

yep, hence the empty {} in Object.assign({}, ...) to avoid mutating the first object.

@Hrxn
Copy link

Hrxn commented Jan 9, 2023

kinda leaning against at moment ...

  • pro

    • do see some benefit in ergonomics
  • con

    • impacts program-correctness with subtle, order-related bugs when property-names collide
    • messes up current jslint-rule about keeping object-properties ascii-ordered
let foo = { aa: 1, bb: 2 };
let bar = {        bb: 3 };

// below feels like a bug-in-waiting
// where user needs to worry about property orderings:

console.log({ ...foo, ...bar, cc: 4 });
// { aa: 1, bb: 3, cc: 4 }
console.log({ ...bar, ...foo, cc: 4 });
// { aa: 1, bb: 2, cc: 4 }

Using similar Object.assign() would make the ordering clearer (but yea, it is less ergonomic):

let foo = { aa: 1, bb: 2 };
let bar = {        bb: 3 };

console.log(Object.assign({}, foo, bar, { cc: 4 }));
// { aa: 1, bb: 3, cc: 4 }
console.log(Object.assign({}, bar, foo, { cc: 4 }));
// { aa: 1, bb: 2, cc: 4 }

What am I missing here, what's the point of these examples here?

console.log({ ...foo, ...bar, cc: 4 });
// { aa: 1, bb: 3, cc: 4 }
console.log({ ...bar, ...foo, cc: 4 });
// { aa: 1, bb: 2, cc: 4 }
console.log(Object.assign({}, foo, bar, { cc: 4 }));
// { aa: 1, bb: 3, cc: 4 }
console.log(Object.assign({}, bar, foo, { cc: 4 }));
// { aa: 1, bb: 2, cc: 4 }

Isn't this supposed to show some difference? Or am I totally on the wrong path here?

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

No branches or pull requests

3 participants