Skip to content

Commit

Permalink
[[FEAT]] Enable object rest/spread via esversion
Browse files Browse the repository at this point in the history
The "Object rest/spread properties" feature was standardized in ES2018.
Update enabling mechanism to use a new `esversion` value dedicated to
that edition.
  • Loading branch information
jugglinmike authored and rwaldron committed Jan 25, 2019
1 parent 280d36b commit 3fc9c19
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 45 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 tests/unit/unstable",
"test-unit": "nodeunit tests/unit",
"test": "npm run test-node && npm run test-browser"
},
"main": "./src/jshint.js",
Expand Down
8 changes: 4 additions & 4 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3877,8 +3877,8 @@ var JSHINT = (function() {
}

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

expression(10, context);
Expand Down Expand Up @@ -4024,8 +4024,8 @@ var JSHINT = (function() {
var isRest = spreadrest("rest");

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

// Due to visual symmetry with the array rest property (and the early
Expand Down
26 changes: 0 additions & 26 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,32 +1027,6 @@ exports.val = {
* right to remove or modify them between major version releases.
*/
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
Expand Down
9 changes: 9 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ var state = {
return this.option.moz;
},

/**
* Determine if constructs introduced in ECMAScript 8 should be accepted.
*
* @returns {boolean}
*/
inES9: function() {
return this.esVersion >= 9;
},

/**
* Determine if constructs introduced in ECMAScript 8 should be accepted.
*
Expand Down
2 changes: 1 addition & 1 deletion tests/test262/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = function(test) {
var isModule = !!test.attrs.flags.module;

try {
JSHint(test.contents, { esversion: 8, maxerr: Infinity, module: isModule });
JSHint(test.contents, { esversion: 9, maxerr: Infinity, module: isModule });
} catch (e) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

var TestRun = require("../../helpers/testhelper").setup.testRun;
var TestRun = require("../helpers/testhelper").setup.testRun;

exports.enabling = function (test) {
TestRun(test, "Not enabled")
.addError(1, 11, "'object spread property' is a non-standard language feature. Enable it using the 'objspreadrest' unstable option.")
.test("void { ...x };", { esversion: 6 });
.addError(1, 11, "'object spread property' is only available in ES9 (use 'esversion: 9').")
.test("void { ...x };", { esversion: 8 });

TestRun(test, "Not enabled")
.addError(1, 7, "'object rest property' is a non-standard language feature. Enable it using the 'objspreadrest' unstable option.")
.test("({ ...x } = {});", { esversion: 6 });
.addError(1, 7, "'object rest property' is only available in ES9 (use 'esversion: 9').")
.test("({ ...x } = {});", { esversion: 8 });

test.done();
};
Expand All @@ -24,7 +24,7 @@ exports.spread = function (test) {
];

TestRun(test, "identifier")
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
.test(code, { esversion: 9 });

code = [
"var o;",
Expand All @@ -37,7 +37,7 @@ exports.spread = function (test) {
];

TestRun(test, "expression")
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
.test(code, { esversion: 9 });

test.done();
};
Expand All @@ -52,7 +52,7 @@ exports.rest = function (test) {
];

TestRun(test, "identifier, final")
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
.test(code, { esversion: 9 });

code = [
"({ ...x, } = {});",
Expand All @@ -68,7 +68,7 @@ exports.rest = function (test) {
.addError(3, 15, "Invalid element after rest element.")
.addError(4, 14, "Invalid element after rest element.")
.addError(5, 18, "Invalid element after rest element.")
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
.test(code, { esversion: 9 });

code = [
"({ ...[a, b, c] } = {});",
Expand All @@ -84,7 +84,7 @@ exports.rest = function (test) {
.addError(3, 14, "Expected an identifier and instead saw '['.")
.addError(4, 13, "Expected an identifier and instead saw '['.")
.addError(5, 17, "Expected an identifier and instead saw '['.")
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
.test(code, { esversion: 9 });

code = [
"({ ...[a, b, c], } = {});",
Expand All @@ -105,15 +105,15 @@ exports.rest = function (test) {
.addError(4, 22, "Invalid element after rest element.")
.addError(5, 17, "Expected an identifier and instead saw '['.")
.addError(5, 26, "Invalid element after rest element.")
.test(code, { esversion: 6, unstable: { objspreadrest: true } });
.test(code, { esversion: 9 });

TestRun(test, "nested array pattern, empty")
.addError(1, 7, "Expected an identifier and instead saw '['.")
.test("({ ...[] } = {});", { esversion: 6, unstable: { objspreadrest: true } });
.test("({ ...[] } = {});", { esversion: 9 });

TestRun(test, "nested object pattern, empty")
.addError(1, 7, "Expected an identifier and instead saw '{'.")
.test("({ ...{} } = {});", { esversion: 6, unstable: { objspreadrest: true } });
.test("({ ...{} } = {});", { esversion: 9 });

test.done();
};

0 comments on commit 3fc9c19

Please sign in to comment.