From fede5d2142921dde6f38f6a50e2ab547fde96b85 Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 02:19:33 +0300 Subject: [PATCH 1/7] Upgrade gulp, gulp3 isnt compatible with new nodes --- gulpfile.js | 4 ++-- package.json | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/gulpfile.js b/gulpfile.js index 590e1274..822807f6 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -79,5 +79,5 @@ gulp.task('lint', function () { .pipe(eslint.failOnError()); }); -gulp.task('travis', [ 'lint', 'test' ]); -gulp.task('default', [ 'travis' ]); +gulp.task('travis', gulp.parallel('lint', 'test')); +gulp.task('default', gulp.parallel('travis')); diff --git a/package.json b/package.json index 5e649dde..b6288a22 100644 --- a/package.json +++ b/package.json @@ -44,9 +44,9 @@ "bower-registry-client": "^1.0.0", "chai": "^3.5.0", "commonjs-everywhere": "^0.9.7", - "gulp": "^3.8.10", - "gulp-eslint": "^3.0.1", - "gulp-mocha": "^3.0.1", + "gulp": "^4.0.1", + "gulp-eslint": "^6.0.0", + "gulp-mocha": "^7.0.2", "semver": "^5.1.0" }, "license": "BSD-2-Clause", From a33fb373b980068ed656e456f650f4d17a7a6cac Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 02:19:47 +0300 Subject: [PATCH 2/7] Support noRange flag --- escodegen.js | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/escodegen.js b/escodegen.js index 41b3850f..fcb12afb 100644 --- a/escodegen.js +++ b/escodegen.js @@ -61,6 +61,7 @@ sourceMap, sourceCode, preserveBlankLines, + noRange, FORMAT_MINIFY, FORMAT_DEFAULTS; @@ -79,6 +80,15 @@ return CodeGenerator.Statement.hasOwnProperty(node.type); } + // Get range either by directly accessing .start + .end or accessing .range + function getRange(node) { + if (!noRange) { + return node.range; + } else { + return [node.start, node.end]; + } + } + Precedence = { Sequence: 0, Yield: 1, @@ -201,7 +211,8 @@ directive: false, raw: true, verbatim: null, - sourceCode: null + sourceCode: null, + noRange: false }; } @@ -668,7 +679,7 @@ result = []; extRange = comment.extendedRange; - range = comment.range; + range = getRange(comment); prefix = sourceCode.substring(extRange[0], range[0]); count = (prefix.match(/\n/g) || []).length; @@ -684,7 +695,7 @@ for (i = 1, len = stmt.leadingComments.length; i < len; i++) { comment = stmt.leadingComments[i]; - range = comment.range; + range = getRange(comment); infix = sourceCode.substring(prevRange[1], range[0]); count = (infix.match(/\n/g) || []).length; @@ -726,7 +737,7 @@ if (preserveBlankLines) { comment = stmt.trailingComments[0]; extRange = comment.extendedRange; - range = comment.range; + range = getRange(comment); prefix = sourceCode.substring(extRange[0], range[0]); count = (prefix.match(/\n/g) || []).length; @@ -1023,7 +1034,7 @@ withIndent(function () { // handle functions without any code if (stmt.body.length === 0 && preserveBlankLines) { - range = stmt.range; + range = getRange(stmt); if (range[1] - range[0] > 2) { content = sourceCode.substring(range[0] + 1, range[1] - 1); if (content[0] === '\n') { @@ -1051,14 +1062,14 @@ } } if (!stmt.body[0].leadingComments) { - generateBlankLines(stmt.range[0], stmt.body[0].range[0], result); + generateBlankLines(getRange(stmt)[0], getRange(stmt.body[0])[0], result); } } // handle spaces between lines if (i > 0) { if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) { - generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result); + generateBlankLines(getRange(stmt.body[i - 1])[1], getRange(stmt.body[i])[0], result); } } } @@ -1090,7 +1101,7 @@ // handle spaces after the last line if (i === iz - 1) { if (!stmt.body[i].trailingComments) { - generateBlankLines(stmt.body[i].range[1], stmt.range[1], result); + generateBlankLines(getRange(stmt.body[i])[1], getRange(stmt)[1], result); } } } @@ -1692,14 +1703,14 @@ // handle spaces before the first line if (i === 0) { if (!stmt.body[0].leadingComments) { - generateBlankLines(stmt.range[0], stmt.body[i].range[0], result); + generateBlankLines(getRange(stmt)[0], getRange(stmt.body[i])[0], result); } } // handle spaces between lines if (i > 0) { if (!stmt.body[i - 1].trailingComments && !stmt.body[i].leadingComments) { - generateBlankLines(stmt.body[i - 1].range[1], stmt.body[i].range[0], result); + generateBlankLines(getRange(stmt.body[i - 1])[1], getRange(stmt.body[i])[0], result); } } } @@ -1720,7 +1731,7 @@ // handle spaces after the last line if (i === iz - 1) { if (!stmt.body[i].trailingComments) { - generateBlankLines(stmt.body[i].range[1], stmt.range[1], result); + generateBlankLines(getRange(stmt.body[i])[1], getRange(stmt)[1], result); } } } @@ -2547,6 +2558,7 @@ sourceMap = options.sourceMap; sourceCode = options.sourceCode; preserveBlankLines = options.format.preserveBlankLines && sourceCode !== null; + noRange = options.noRange; extra = options; if (sourceMap) { From 79b0983b637fe28be21422431e44bceaf027897a Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 02:56:27 +0300 Subject: [PATCH 3/7] Remove the noRange option - just do it automatically --- escodegen.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/escodegen.js b/escodegen.js index fcb12afb..bbae4935 100644 --- a/escodegen.js +++ b/escodegen.js @@ -61,7 +61,6 @@ sourceMap, sourceCode, preserveBlankLines, - noRange, FORMAT_MINIFY, FORMAT_DEFAULTS; @@ -82,11 +81,10 @@ // Get range either by directly accessing .start + .end or accessing .range function getRange(node) { - if (!noRange) { - return node.range; - } else { + if (!("range" in node)) { return [node.start, node.end]; } + return node.range; } Precedence = { @@ -211,8 +209,7 @@ directive: false, raw: true, verbatim: null, - sourceCode: null, - noRange: false + sourceCode: null }; } @@ -2558,7 +2555,6 @@ sourceMap = options.sourceMap; sourceCode = options.sourceCode; preserveBlankLines = options.format.preserveBlankLines && sourceCode !== null; - noRange = options.noRange; extra = options; if (sourceMap) { From 0793cc63cbdc41d91ef161e9927dfa879d20c7f1 Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 02:56:48 +0300 Subject: [PATCH 4/7] Add tests for ranges --- test/range.js | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 test/range.js diff --git a/test/range.js b/test/range.js new file mode 100644 index 00000000..96dc916c --- /dev/null +++ b/test/range.js @@ -0,0 +1,86 @@ +/* + Copyright (C) 2019 Guy Lewin + + 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 fs = require('fs'), + esprima = require('esprima'), + escodegen = require('./loader'), + chai = require('chai'), + expect = chai.expect; + +function test(tree) { + var tree, actual; + var code = "{\n }"; + var options = { + preserveBlankLines: true, + sourceCode: code, + format: { + preserveBlankLines: true + } + }; + + actual = escodegen.generate(tree, options); + expect(actual).to.be.equal(code); +} + +describe('test building trees with and without range', function () { + it("with range - Esprima built", function () { + // Parsed by Esprima + test({ + "type": "Program", + "body": [{ + "type": "BlockStatement", + "body": [], + "range": [ + 0, + 7 + ] + }], + "sourceType": "module", + "range": [ + 0, + 7 + ] + }); + }); + + it("without range - Acorn built", function () { + // Parsed by Acorn + test({ + "type": "Program", + "start": 0, + "end": 7, + "body": [{ + "type": "BlockStatement", + "start": 0, + "end": 7, + "body": [] + }], + "sourceType": "module" + }); + }); +}); + +/* vim: set sw=4 ts=4 et tw=80 : */ From 58131fd02b4fe766d128bf9f1c13e4aae24d4712 Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 03:08:03 +0300 Subject: [PATCH 5/7] node 6 is in EOL, use newer nodes for testing --- .travis.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 921e6c86..5cccead6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ sudo: false language: node_js node_js: - - "4" - - "6" - "8" + - "10" + - "12" From 6fd9c4a3d61743bbcf95c9b9d7bc98ba61341279 Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 15:37:14 +0300 Subject: [PATCH 6/7] Remove wrong preserveBlankLines --- test/range.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/range.js b/test/range.js index 96dc916c..0059ee54 100644 --- a/test/range.js +++ b/test/range.js @@ -34,7 +34,6 @@ function test(tree) { var tree, actual; var code = "{\n }"; var options = { - preserveBlankLines: true, sourceCode: code, format: { preserveBlankLines: true From 6c2173a9e0b9827c1fc951ff602a70bd86249e3c Mon Sep 17 00:00:00 2001 From: Guy Lewin Date: Sat, 19 Oct 2019 15:37:39 +0300 Subject: [PATCH 7/7] Add Acorn integration test broken without this fix --- test/compare-acorn-preserve-blank-lines.js | 66 +++++++++++++++++++ .../block-statement.expected.js | 2 + .../block-statement.js | 2 + 3 files changed, 70 insertions(+) create mode 100644 test/compare-acorn-preserve-blank-lines.js create mode 100644 test/compare-acorn-preserve-blank-lines/block-statement.expected.js create mode 100644 test/compare-acorn-preserve-blank-lines/block-statement.js diff --git a/test/compare-acorn-preserve-blank-lines.js b/test/compare-acorn-preserve-blank-lines.js new file mode 100644 index 00000000..0e267478 --- /dev/null +++ b/test/compare-acorn-preserve-blank-lines.js @@ -0,0 +1,66 @@ +/* + Copyright (C) 2019 Guy Lewin + + 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 fs = require('fs'), + acorn = require('acorn'), + escodegen = require('./loader'), + chai = require('chai'), + expect = chai.expect; + +function test(code, expected) { + var tree, actual, options, StringObject; + + // alias, so that JSLint does not complain. + StringObject = String; + + tree = acorn.parse(code); + + options = { + sourceCode: code, + format: { + preserveBlankLines: true + } + } + + // for UNIX text comment + actual = escodegen.generate(tree, options).replace(/[\n\r]$/, '') + '\n'; + expect(actual).to.be.equal(expected); +} + +describe('compare acorn preserve blank lines test', function () { + fs.readdirSync(__dirname + '/compare-acorn-preserve-blank-lines').sort().forEach(function(file) { + var code, expected, exp; + if (/\.js$/.test(file) && !/expected\.js$/.test(file)) { + it(file, function () { + exp = file.replace(/\.js$/, '.expected.js'); + code = fs.readFileSync(__dirname + '/compare-acorn-preserve-blank-lines/' + file, 'utf-8'); + expected = fs.readFileSync(__dirname + '/compare-acorn-preserve-blank-lines/' + exp, 'utf-8'); + test(code, expected); + }); + } + }); +}); +/* vim: set sw=4 ts=4 et tw=80 : */ diff --git a/test/compare-acorn-preserve-blank-lines/block-statement.expected.js b/test/compare-acorn-preserve-blank-lines/block-statement.expected.js new file mode 100644 index 00000000..b90c392a --- /dev/null +++ b/test/compare-acorn-preserve-blank-lines/block-statement.expected.js @@ -0,0 +1,2 @@ +{ + } diff --git a/test/compare-acorn-preserve-blank-lines/block-statement.js b/test/compare-acorn-preserve-blank-lines/block-statement.js new file mode 100644 index 00000000..b90c392a --- /dev/null +++ b/test/compare-acorn-preserve-blank-lines/block-statement.js @@ -0,0 +1,2 @@ +{ + }