Skip to content

Commit

Permalink
[[FEAT]] Implement support for object spread/rest
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike committed Dec 23, 2018
1 parent fff0f61 commit 35e1b17
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 781 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"test-cli": "nodeunit tests/cli.js",
"test-node": "npm run test-unit && npm run test-cli && npm run test-regression",
"test-regression": "nodeunit tests/regression",
"test-unit": "nodeunit tests/unit",
"test-unit": "nodeunit tests/unit tests/unit/unstable",
"test": "npm run test-node && npm run test-browser"
},
"main": "./src/jshint.js",
Expand Down
26 changes: 24 additions & 2 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3344,6 +3344,13 @@ var JSHINT = (function() {
} else if (nextVal === "set" && i && f["(metrics)"].arity !== 1) {
warning("W077", t, i);
}

} else if (spreadrest("spread")) {
if (!state.option.unstable.objspreadrest) {
warning("W144", state.tokens.next, "object spread property", "objspreadrest");
}

expression(10);
} else {
if (state.tokens.next.value === "*" && state.tokens.next.type === "(punctuator)") {
if (!state.inES6()) {
Expand Down Expand Up @@ -3478,8 +3485,19 @@ var JSHINT = (function() {
nextInnerDE();
} else {
// this id will either be the property name or the property name and the assigning identifier
id = identifier();
if (checkPunctuator(state.tokens.next, ":")) {
var isRest = spreadrest("rest");

if (isRest) {
if (!state.option.unstable.objspreadrest) {
warning("W144", state.tokens.next, "object rest property", "objspreadrest");
}

nextInnerDE();
} else {
id = identifier();
}

if (!isRest && checkPunctuator(state.tokens.next, ":")) {
advance(":");
nextInnerDE();
} else if (id) {
Expand All @@ -3489,6 +3507,10 @@ var JSHINT = (function() {
}
identifiers.push({ id: id, token: state.tokens.curr });
}

if (isRest && checkPunctuator(state.tokens.next, ",")) {
warning("W130", state.tokens.next);
}
}
};

Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ var warnings = {
W141: "Empty {a}: this is unnecessary and can be removed.",
W142: "Empty {a}: consider replacing with `import '{b}';`.",
W143: "Assignment to properties of a mapped arguments object may cause " +
"unexpected changes to formal parameters."
"unexpected changes to formal parameters.",
W144: "'{a}' is a non-standard language feature. Enable it using the '{b}' unstable option."
};

var info = {
Expand Down
29 changes: 28 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,34 @@ exports.val = {
* the presence and behavior of these options is volatile; JSHint reserves the
* right to remove or modify them between major version releases.
*/
exports.unstable = {};
exports.unstable = {
/**
* Enable parsing support for rest properties for object destructuring
* assignment and spread properties for object literals. From the proposal:
*
* > Rest Properties
* >
* > Rest properties collect the remaining own enumerable property keys that
* > are not already picked off by the destructuring pattern. Those keys and
* > their values are copied onto a new object.
* >
* > let { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
* > x; // 1
* > y; // 2
* > z; // { a: 3, b: 4 }
* >
* > Spread Properties
* >
* > Spread properties in object initializers copies own enumerable
* > properties from a provided object onto the newly created object.
* >
* > let n = { x, y, ...z };
* > n; // { x: 1, y: 2, a: 3, b: 4 }
*
* https://github.com/tc39/proposal-object-rest-spread
*/
objspreadrest: true
};

// These are JSHint boolean options which are shared with JSLint
// where the definition in JSHint is opposite JSLint
Expand Down

0 comments on commit 35e1b17

Please sign in to comment.