From d41036d8269a5f13108afd9b105d74eab4790269 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sat, 4 Jan 2020 23:34:08 +0300 Subject: [PATCH 1/3] Added support for optional-catch binding. Removed CLI scripts --- benchmark/asts.js | 3 +- bin/escodegen.js | 77 ----------------- bin/esgenerate.js | 64 --------------- escodegen.js | 20 +++-- package.json | 10 +-- test/harmony-espee.js | 186 ++++++++++++++++++++++++++++++++++++++++++ tools/release.js | 3 - 7 files changed, 202 insertions(+), 161 deletions(-) delete mode 100755 bin/escodegen.js delete mode 100755 bin/esgenerate.js create mode 100644 test/harmony-espee.js diff --git a/benchmark/asts.js b/benchmark/asts.js index 51dfbc7c..2c125994 100644 --- a/benchmark/asts.js +++ b/benchmark/asts.js @@ -1,6 +1,5 @@ var fs = require('fs'), - path = require('path'), - esprima = require('esprima'); + path = require('path'); var FILES_PATH = path.join(__dirname, './asts'); diff --git a/bin/escodegen.js b/bin/escodegen.js deleted file mode 100755 index a7c38aa1..00000000 --- a/bin/escodegen.js +++ /dev/null @@ -1,77 +0,0 @@ -#!/usr/bin/env node -/* - Copyright (C) 2012 Yusuke Suzuki - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -/*jslint sloppy:true node:true */ - -var fs = require('fs'), - path = require('path'), - root = path.join(path.dirname(fs.realpathSync(__filename)), '..'), - esprima = require('esprima'), - escodegen = require(root), - optionator = require('optionator')({ - prepend: 'Usage: escodegen [options] file...', - options: [ - { - option: 'config', - alias: 'c', - type: 'String', - description: 'configuration json for escodegen' - } - ] - }), - args = optionator.parse(process.argv), - files = args._, - options, - esprimaOptions = { - raw: true, - tokens: true, - range: true, - comment: true - }; - -if (files.length === 0) { - console.log(optionator.generateHelp()); - process.exit(1); -} - -if (args.config) { - try { - options = JSON.parse(fs.readFileSync(args.config, 'utf-8')); - } catch (err) { - console.error('Error parsing config: ', err); - } -} - -files.forEach(function (filename) { - var content = fs.readFileSync(filename, 'utf-8'), - syntax = esprima.parse(content, esprimaOptions); - - if (options.comment) { - escodegen.attachComments(syntax, syntax.comments, syntax.tokens); - } - - console.log(escodegen.generate(syntax, options)); -}); -/* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/bin/esgenerate.js b/bin/esgenerate.js deleted file mode 100755 index 449abcc8..00000000 --- a/bin/esgenerate.js +++ /dev/null @@ -1,64 +0,0 @@ -#!/usr/bin/env node -/* - Copyright (C) 2012 Yusuke Suzuki - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright - notice, this list of conditions and the following disclaimer in the - documentation and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY - DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -*/ - -/*jslint sloppy:true node:true */ - -var fs = require('fs'), - path = require('path'), - root = path.join(path.dirname(fs.realpathSync(__filename)), '..'), - escodegen = require(root), - optionator = require('optionator')({ - prepend: 'Usage: esgenerate [options] file.json ...', - options: [ - { - option: 'config', - alias: 'c', - type: 'String', - description: 'configuration json for escodegen' - } - ] - }), - args = optionator.parse(process.argv), - files = args._, - options; - -if (files.length === 0) { - console.log(optionator.generateHelp()); - process.exit(1); -} - -if (args.config) { - try { - options = JSON.parse(fs.readFileSync(args.config, 'utf-8')) - } catch (err) { - console.error('Error parsing config: ', err); - } -} - -files.forEach(function (filename) { - var content = fs.readFileSync(filename, 'utf-8'); - console.log(escodegen.generate(JSON.parse(content), options)); -}); -/* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/escodegen.js b/escodegen.js index 26e75385..28ada0c5 100644 --- a/escodegen.js +++ b/escodegen.js @@ -1176,15 +1176,19 @@ withIndent(function () { var guard; - result = [ - 'catch' + space + '(', - that.generateExpression(stmt.param, Precedence.Sequence, E_TTT), - ')' - ]; + if (!stmt.param) { + result = ['catch']; + } else { + result = [ + 'catch' + space + '(', + that.generateExpression(stmt.param, Precedence.Sequence, E_TTT), + ')' + ]; - if (stmt.guard) { - guard = that.generateExpression(stmt.guard, Precedence.Sequence, E_TTT); - result.splice(2, 0, ' if ', guard); + if (stmt.guard) { + guard = that.generateExpression(stmt.guard, Precedence.Sequence, E_TTT); + result.splice(2, 0, ' if ', guard); + } } }); result.push(this.maybeBlock(stmt.body, S_TFFF)); diff --git a/package.json b/package.json index eea60d44..86886b23 100644 --- a/package.json +++ b/package.json @@ -3,18 +3,13 @@ "description": "ECMAScript code generator", "homepage": "http://github.com/estools/escodegen", "main": "escodegen.js", - "bin": { - "esgenerate": "./bin/esgenerate.js", - "escodegen": "./bin/escodegen.js" - }, "files": [ "LICENSE.BSD", "README.md", - "bin", "escodegen.js", "package.json" ], - "version": "1.12.1", + "version": "2.0.0", "engines": { "node": ">=4.0" }, @@ -32,7 +27,6 @@ "dependencies": { "estraverse": "^4.2.0", "esutils": "^2.0.2", - "esprima": "^4.0.1", "optionator": "^0.8.1" }, "optionalDependencies": { @@ -44,6 +38,8 @@ "bower-registry-client": "^1.0.0", "chai": "^3.5.0", "commonjs-everywhere": "^0.9.7", + "espree": "^6.1.2", + "esprima": "^4.0.1", "gulp": "^3.8.10", "gulp-eslint": "^3.0.1", "gulp-mocha": "^3.0.1", diff --git a/test/harmony-espee.js b/test/harmony-espee.js new file mode 100644 index 00000000..e56d1f1e --- /dev/null +++ b/test/harmony-espee.js @@ -0,0 +1,186 @@ +/* + Copyright (C) 2011-2013 Yusuke Suzuki + Copyright (C) 2015 Ingvar Stepanyan + Copyright (C) 2012 Ariya Hidayat + Copyright (C) 2012 Joost-Wim Boekesteijn + Copyright (C) 2012 Yusuke Suzuki + Copyright (C) 2012 Arpad Borsos + Copyright (C) 2011 Ariya Hidayat + Copyright (C) 2011 Arpad Borsos + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +'use strict'; + +var espree = require('espree'), + escodegen = require('./loader'), + chai = require('chai'), + expect = chai.expect, + data; + +data = { + 'Optional catch binding': { + 'try {\n} catch {\n}': { + generateFrom: { + type: 'TryStatement', + block: { + type: 'BlockStatement', + body: [], + range: [4, 7], + loc: { + start: { line: 1, column: 4 }, + end: { line: 1, column: 7 } + } + }, + handlers: [{ + type: 'CatchClause', + param: null, + guard: null, + body: { + type: 'BlockStatement', + body: [], + range: [14, 17], + loc: { + start: { line: 1, column: 14 }, + end: { line: 1, column: 17 } + } + }, + range: [8, 17], + loc: { + start: { line: 1, column: 8 }, + end: { line: 1, column: 17 } + } + }], + finalizer: null, + range: [0, 17], + loc: { + start: { line: 1, column: 0 }, + end: { line: 1, column: 17 } + } + } + } + }, +}; + +function updateDeeply(target, override) { + var key, val; + + function isHashObject(target) { + return typeof target === 'object' && target instanceof Object && !(target instanceof RegExp); + } + + for (key in override) { + if (override.hasOwnProperty(key)) { + val = override[key]; + if (isHashObject(val)) { + if (isHashObject(target[key])) { + updateDeeply(target[key], val); + } else { + target[key] = updateDeeply({}, val); + } + } else { + target[key] = val; + } + } + } + return target; +} + +// Special handling for regular expression literal since we need to +// convert it to a string literal, otherwise it will be decoded +// as object "{}" and the regular expression would be lost. +function adjustRegexLiteral(key, value) { + 'use strict'; + if (key === 'value' && value instanceof RegExp) { + value = value.toString(); + } + return value; +} + +function testIdentity(code, syntax) { + 'use strict'; + var expected, tree, actual, actual2, options, StringObject; + + // alias, so that JSLint does not complain. + StringObject = String; + + options = { + comment: false, + range: false, + loc: false, + tokens: false, + raw: false + }; + + tree = espree.parse(code, options); + expected = JSON.stringify(tree, adjustRegexLiteral, 4); + tree = espree.parse(escodegen.generate(tree), options); + actual = JSON.stringify(tree, adjustRegexLiteral, 4); + tree = espree.parse(escodegen.generate(syntax), options); + actual2 = JSON.stringify(tree, adjustRegexLiteral, 4); + expect(actual).to.be.equal(expected); + expect(actual2).to.be.equal(expected); +} + +function testGenerate(expected, result) { + 'use strict'; + var actual, options; + + options = { + indent: ' ', + parse: espree.parse + }; + + if (result.options) { + options = updateDeeply(options, result.options); + } + + actual = escodegen.generate(result.generateFrom, options); + expect(actual).to.be.equal(expected); +} + +function isGeneratorIdentityFixture(result) { + 'use strict'; + return !result.hasOwnProperty('generateFrom') && + !result.hasOwnProperty('result'); +} + +function runTest(code, result) { + 'use strict'; + if (result.hasOwnProperty('generateFrom')) { + testGenerate(code, result); + } else { + testIdentity(code, result); + } +} + +describe('harmony espree test', function () { + Object.keys(data).forEach(function (category) { + Object.keys(data[category]).forEach(function (source) { + it(category, function () { + var expected = data[category][source]; + runTest(source, expected); + }); + }); + }); +}); +/* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/tools/release.js b/tools/release.js index fca6fd28..b8a804d3 100755 --- a/tools/release.js +++ b/tools/release.js @@ -28,10 +28,7 @@ var fs = require('fs'), path = require('path'), root = path.join(path.dirname(fs.realpathSync(__filename)), '..'), - escodegen = require(root), - esprima = require('esprima'), RegistryClient = require('bower-registry-client'), - semver = require('semver'), child_process = require('child_process'), Promise = require('bluebird'); From 22ff01d54a8ed144d8d961f93274cca3b349129f Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sat, 4 Jan 2020 23:38:27 +0300 Subject: [PATCH 2/3] Changed minimal required node.js version --- .travis.yml | 5 +++-- package.json | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 921e6c86..9f397b38 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,7 @@ sudo: false language: node_js node_js: - - "4" - - "6" - "8" + - "10" + - "12" + - "stable" diff --git a/package.json b/package.json index 86886b23..b791f9b7 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ ], "version": "2.0.0", "engines": { - "node": ">=4.0" + "node": ">=8.0" }, "maintainers": [ { From 656635c4d344284f68f742c05d9e82ef8fd5126e Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Sat, 4 Jan 2020 23:56:25 +0300 Subject: [PATCH 3/3] Updated gulp version to 4+, fixed gulpfile --- gulpfile.js | 18 +++++++++++------- package.json | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 590e1274..31b59ffe 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -64,20 +64,24 @@ var ESLINT_OPTION = { } }; -gulp.task('test', function () { +function test() { return gulp.src(TEST) .pipe(mocha({ reporter: 'spec', timeout: 100000 // 100s })); -}); +} -gulp.task('lint', function () { - return gulp.src(LINT) +function lint() { + return gulp.src(LINT) .pipe(eslint(ESLINT_OPTION)) .pipe(eslint.formatEach('stylish', process.stderr)) .pipe(eslint.failOnError()); -}); +} -gulp.task('travis', [ 'lint', 'test' ]); -gulp.task('default', [ 'travis' ]); +var travis = gulp.parallel(lint, test); + +exports.test = test; +exports.lint = lint; +exports.travis = travis; +exports.default = travis; diff --git a/package.json b/package.json index b791f9b7..9f5357d5 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "commonjs-everywhere": "^0.9.7", "espree": "^6.1.2", "esprima": "^4.0.1", - "gulp": "^3.8.10", + "gulp": "^4.0.2", "gulp-eslint": "^3.0.1", "gulp-mocha": "^3.0.1", "semver": "^5.1.0"