Commit bfc38380 authored by Tim van der Lippe's avatar Tim van der Lippe Committed by Commit Bot

Update Acorn ecmaVersion to 2018 to support Object spread

Also define a constant and use the constant everywhere we interact with
Acorn, as we were using 3 different ecmaVersions.

Bug: 999794
Change-Id: I56043ebccdb7767180af2f4a6c337f1f58ff187b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1778617
Commit-Queue: Tim van der Lippe <tvanderlippe@google.com>
Reviewed-by: default avatarYang Guo <yangguo@chromium.org>
Reviewed-by: default avatarErik Luo <luoe@chromium.org>
Cr-Commit-Position: refs/heads/master@{#693082}
parent d11dcf6e
...@@ -11,7 +11,8 @@ FormatterWorker.AcornTokenizer = class { ...@@ -11,7 +11,8 @@ FormatterWorker.AcornTokenizer = class {
constructor(content) { constructor(content) {
this._content = content; this._content = content;
this._comments = []; this._comments = [];
this._tokenizer = acorn.tokenizer(this._content, {ecmaVersion: 8, onComment: this._comments}); this._tokenizer =
acorn.tokenizer(this._content, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION, onComment: this._comments});
this._textCursor = new TextUtils.TextCursor(this._content.computeLineEndings()); this._textCursor = new TextUtils.TextCursor(this._content.computeLineEndings());
this._tokenLineStart = 0; this._tokenLineStart = 0;
this._tokenLineEnd = 0; this._tokenLineEnd = 0;
......
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
FormatterWorker.ACORN_ECMA_VERSION = 2018;
/** /**
* @param {string} mimeType * @param {string} mimeType
* @return {function(string, function(string, ?string, number, number):(!Object|undefined))} * @return {function(string, function(string, ?string, number, number):(!Object|undefined))}
...@@ -109,7 +112,7 @@ FormatterWorker.parseJSONRelaxed = function(content) { ...@@ -109,7 +112,7 @@ FormatterWorker.parseJSONRelaxed = function(content) {
* @param {string} content * @param {string} content
*/ */
FormatterWorker.evaluatableJavaScriptSubstring = function(content) { FormatterWorker.evaluatableJavaScriptSubstring = function(content) {
const tokenizer = acorn.tokenizer(content, {ecmaVersion: 9}); const tokenizer = acorn.tokenizer(content, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
let result = ''; let result = '';
try { try {
let token = tokenizer.getToken(); let token = tokenizer.getToken();
...@@ -155,7 +158,7 @@ FormatterWorker.preprocessTopLevelAwaitExpressions = function(content) { ...@@ -155,7 +158,7 @@ FormatterWorker.preprocessTopLevelAwaitExpressions = function(content) {
let root; let root;
let body; let body;
try { try {
root = acorn.parse(wrapped, {ecmaVersion: 10}); root = acorn.parse(wrapped, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
body = root.body[0].expression.callee.body; body = root.body[0].expression.callee.body;
} catch (e) { } catch (e) {
postMessage(''); postMessage('');
...@@ -251,7 +254,7 @@ FormatterWorker.preprocessTopLevelAwaitExpressions = function(content) { ...@@ -251,7 +254,7 @@ FormatterWorker.preprocessTopLevelAwaitExpressions = function(content) {
FormatterWorker.javaScriptIdentifiers = function(content) { FormatterWorker.javaScriptIdentifiers = function(content) {
let root = null; let root = null;
try { try {
root = acorn.parse(content, {ranges: false, ecmaVersion: 9}); root = acorn.parse(content, {ranges: false, ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
} catch (e) { } catch (e) {
} }
...@@ -351,7 +354,7 @@ FormatterWorker.findLastFunctionCall = function(content) { ...@@ -351,7 +354,7 @@ FormatterWorker.findLastFunctionCall = function(content) {
if (content.length > 10000) if (content.length > 10000)
return null; return null;
try { try {
const tokenizer = acorn.tokenizer(content, {ecmaVersion: 9}); const tokenizer = acorn.tokenizer(content, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
while (tokenizer.getToken().type !== acorn.tokTypes.eof) { while (tokenizer.getToken().type !== acorn.tokTypes.eof) {
} }
} catch (e) { } catch (e) {
...@@ -396,13 +399,13 @@ FormatterWorker.argumentsList = function(content) { ...@@ -396,13 +399,13 @@ FormatterWorker.argumentsList = function(content) {
let parsed = null; let parsed = null;
try { try {
// Try to parse as a function, anonymous function, or arrow function. // Try to parse as a function, anonymous function, or arrow function.
parsed = acorn.parse(`(${content})`, {ecmaVersion: 9}); parsed = acorn.parse(`(${content})`, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
} catch (e) { } catch (e) {
} }
if (!parsed) { if (!parsed) {
try { try {
// Try to parse as a method. // Try to parse as a method.
parsed = acorn.parse(`({${content}})`, {ecmaVersion: 9}); parsed = acorn.parse(`({${content}})`, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
} catch (e) { } catch (e) {
} }
} }
...@@ -457,7 +460,7 @@ FormatterWorker.findLastExpression = function(content) { ...@@ -457,7 +460,7 @@ FormatterWorker.findLastExpression = function(content) {
if (content.length > 10000) if (content.length > 10000)
return null; return null;
try { try {
const tokenizer = acorn.tokenizer(content, {ecmaVersion: 9}); const tokenizer = acorn.tokenizer(content, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
while (tokenizer.getToken().type !== acorn.tokTypes.eof) { while (tokenizer.getToken().type !== acorn.tokTypes.eof) {
} }
} catch (e) { } catch (e) {
...@@ -466,7 +469,7 @@ FormatterWorker.findLastExpression = function(content) { ...@@ -466,7 +469,7 @@ FormatterWorker.findLastExpression = function(content) {
const suffix = '.DEVTOOLS'; const suffix = '.DEVTOOLS';
try { try {
acorn.parse(content + suffix, {ecmaVersion: 9}); acorn.parse(content + suffix, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
} catch (parseError) { } catch (parseError) {
// If this is an invalid location for a '.', don't attempt to give autocomplete // If this is an invalid location for a '.', don't attempt to give autocomplete
if (parseError.message.startsWith('Unexpected token') && parseError.pos === content.length) if (parseError.message.startsWith('Unexpected token') && parseError.pos === content.length)
...@@ -494,7 +497,7 @@ FormatterWorker._lastCompleteExpression = function(content, suffix, types) { ...@@ -494,7 +497,7 @@ FormatterWorker._lastCompleteExpression = function(content, suffix, types) {
try { try {
// Wrap content in paren to successfully parse object literals // Wrap content in paren to successfully parse object literals
parsedContent = content[i] === '{' ? `(${content.substring(i)})${suffix}` : `${content.substring(i)}${suffix}`; parsedContent = content[i] === '{' ? `(${content.substring(i)})${suffix}` : `${content.substring(i)}${suffix}`;
ast = acorn.parse(parsedContent, {ecmaVersion: 9}); ast = acorn.parse(parsedContent, {ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
break; break;
} catch (e) { } catch (e) {
} }
......
...@@ -51,7 +51,8 @@ FormatterWorker.JavaScriptFormatter = class { ...@@ -51,7 +51,8 @@ FormatterWorker.JavaScriptFormatter = class {
this._content = text.substring(this._fromOffset, this._toOffset); this._content = text.substring(this._fromOffset, this._toOffset);
this._lastLineNumber = 0; this._lastLineNumber = 0;
this._tokenizer = new FormatterWorker.AcornTokenizer(this._content); this._tokenizer = new FormatterWorker.AcornTokenizer(this._content);
const ast = acorn.parse(this._content, {ranges: false, ecmaVersion: 8, preserveParens: true}); const ast = acorn.parse(
this._content, {ranges: false, ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION, preserveParens: true});
const walker = new FormatterWorker.ESTreeWalker(this._beforeVisit.bind(this), this._afterVisit.bind(this)); const walker = new FormatterWorker.ESTreeWalker(this._beforeVisit.bind(this), this._afterVisit.bind(this));
walker.walk(ast); walker.walk(ast);
} }
......
...@@ -11,9 +11,9 @@ FormatterWorker.javaScriptOutline = function(content) { ...@@ -11,9 +11,9 @@ FormatterWorker.javaScriptOutline = function(content) {
let ast; let ast;
try { try {
ast = acorn.parse(content, {ranges: false, ecmaVersion: 8}); ast = acorn.parse(content, {ranges: false, ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
} catch (e) { } catch (e) {
ast = acorn.loose.parse(content, {ranges: false, ecmaVersion: 8}); ast = acorn.loose.parse(content, {ranges: false, ecmaVersion: FormatterWorker.ACORN_ECMA_VERSION});
} }
const textCursor = new TextUtils.TextCursor(content.computeLineEndings()); const textCursor = new TextUtils.TextCursor(content.computeLineEndings());
......
Verifies JavaScript pretty-printing functionality.
Running: objectSpread
====== 8< ------
const a = {
a: 4,
...{
a: 5,
b: 42
}
};
------ >8 ======
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(async function() {
TestRunner.addResult(`Verifies JavaScript pretty-printing functionality.\n`);
await TestRunner.loadModule('sources_test_runner');
await TestRunner.showPanel('sources');
var testJSFormatter = SourcesTestRunner.testPrettyPrint.bind(SourcesTestRunner, 'text/javascript');
TestRunner.runTestSuite([
function objectSpread(next) {
var mappingQueries = [];
testJSFormatter('const a = {a:4,...{a: 5,b: 42}};', mappingQueries, next);
},
]);
})();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment