Skip to content

Commit

Permalink
[[FEAT]] Add support for ES2019 opt. catch param
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Aug 23, 2019
1 parent 1295f3e commit ec71bc0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
26 changes: 18 additions & 8 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,13 +707,15 @@ var JSHINT = (function() {
case "7":
case "8":
case "9":
case "10":
state.option.moz = false;
state.option.esversion = +val;
break;
case "2015":
case "2016":
case "2017":
case "2018":
case "2019":
state.option.moz = false;
// Translate specification publication year to version number.
state.option.esversion = +val - 2009;
Expand Down Expand Up @@ -4734,13 +4736,11 @@ var JSHINT = (function() {

blockstmt("try", function(context) {
var b;
var hasParameter = false;

function doCatch() {
advance("catch");
function catchParameter() {
advance("(");

state.funct["(scope)"].stack("catchparams");

if (checkPunctuators(state.tokens.next, ["[", "{"])) {
var tokens = destructuringPattern(context);
_.each(tokens, function(token) {
Expand All @@ -4765,9 +4765,6 @@ var JSHINT = (function() {
}

advance(")");

block(context, false);
state.funct["(scope)"].unstack();
}

block(context | prodParams.tryClause, true);
Expand All @@ -4777,7 +4774,20 @@ var JSHINT = (function() {
if (b && (!state.inMoz())) {
warning("W118", state.tokens.next, "multiple catch blocks");
}
doCatch();
advance("catch");
if (state.tokens.next.id !== "{") {
state.funct["(scope)"].stack("catchparams");
hasParameter = true;
catchParameter();
} else if (!state.inES10()) {
warning("W119", state.tokens.curr, "optional catch binding", "10");
}
block(context, false);

if (hasParameter) {
state.funct["(scope)"].unstack();
hasParameter = false;
}
b = true;
}

Expand Down
3 changes: 3 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,9 @@ exports.val = {
* 9](https://www.ecma-international.org/ecma-262/9.0/index.html). Notable
* additions: asynchronous iteration, rest/spread properties, and various
* RegExp extensions
* - `10` - To enable language features introduced by ECMAScript
* 10](https://www.ecma-international.org/ecma-262/10.0/index.html).
* Notable additions: optional catch bindings.
*/
esversion: 5
};
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 10 should be accepted.
*
* @returns {boolean}
*/
inES10: function() {
return this.esVersion >= 10;
},

/**
* Determine if constructs introduced in ECMAScript 9 should be accepted.
*
Expand Down
8 changes: 0 additions & 8 deletions tests/test262/expectations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2068,14 +2068,6 @@ test/language/statements/for-of/let-identifier-with-newline.js(default)
test/language/statements/if/let-block-with-newline.js(default)
test/language/statements/if/let-identifier-with-newline.js(default)
test/language/statements/labeled/value-yield-non-strict-escaped.js(default)
test/language/statements/try/optional-catch-binding-finally.js(default)
test/language/statements/try/optional-catch-binding-finally.js(strict mode)
test/language/statements/try/optional-catch-binding-lexical.js(default)
test/language/statements/try/optional-catch-binding-lexical.js(strict mode)
test/language/statements/try/optional-catch-binding-throws.js(default)
test/language/statements/try/optional-catch-binding-throws.js(strict mode)
test/language/statements/try/optional-catch-binding.js(default)
test/language/statements/try/optional-catch-binding.js(strict mode)
test/language/statements/while/let-block-with-newline.js(default)
test/language/statements/while/let-identifier-with-newline.js(default)
test/language/statements/with/let-block-with-newline.js(default)
Expand Down
2 changes: 1 addition & 1 deletion tests/test262/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ module.exports = function(test) {

try {
JSHint(test.contents, {
esversion: 9,
esversion: 10,
maxerr: Infinity,
module: isModule,
unstable: {
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -4158,13 +4158,15 @@ exports.esversion = function(test) {
"// jshint esversion: 9",
"// jshint esversion: 2018",
"// jshint esversion: 10",
"// jshint esversion: 2019"
"// jshint esversion: 2019",
"// jshint esversion: 11",
"// jshint esversion: 2020"
];

TestRun(test, "Value")
.addError(2, 1, "Bad option value.")
.addError(12, 1, "Bad option value.")
.addError(13, 1, "Bad option value.")
.addError(14, 1, "Bad option value.")
.addError(15, 1, "Bad option value.")
.test(code);

var es5code = [
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4068,6 +4068,19 @@ exports["catch block no curlies"] = function (test) {
test.done();
};

exports.optionalCatch = function (test) {
var code = "try {} catch {}";

TestRun(test)
.addError(1, 8, "'optional catch binding' is only available in ES10 (use 'esversion: 10').")
.test(code);

TestRun(test)
.test(code, {esversion: 10});

test.done();
};

exports["strict violation - use of arguments and eval"] = function (test) {
var code = [
"'use strict';",
Expand Down

0 comments on commit ec71bc0

Please sign in to comment.