jslint
jslint copied to clipboard
Feature request: Spread syntax support in object literals
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?
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 }
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.
yep, hence the empty {} in Object.assign({}, ...) to avoid mutating the first object.
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?