From 9358906f2c5b33b0fb7e7eaaf118c457d6ce80f5 Mon Sep 17 00:00:00 2001 From: Tim van der Lippe Date: Fri, 29 May 2020 16:21:02 +0100 Subject: [PATCH] Typecheck formatter_worker/AcornTokenizer.js with TypeScript The `.mjs.d.ts` file has been upstreamed in https://github.com/acornjs/acorn/pull/954 as per the instructions in https://github.com/microsoft/TypeScript/issues/27957#issuecomment-623631119 R=jacktfranklin@chromium.org,bmeurer@chromium.org Bug: 1011811 Change-Id: Ia2e0c3145318ac93b5e49d2b40780ffe7c469bab Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2218015 Commit-Queue: Tim van der Lippe Reviewed-by: Jack Franklin Reviewed-by: Benedikt Meurer --- front_end/formatter_worker/AcornTokenizer.js | 34 ++++++++++++------- .../formatter_worker/JavaScriptFormatter.js | 17 +++++----- front_end/third_party/acorn/BUILD.gn | 1 + .../acorn/package/dist/acorn.mjs.d.ts | 2 ++ .../acorn/package/dist/acorn_types.mjs | 11 ++---- 5 files changed, 36 insertions(+), 29 deletions(-) create mode 100644 front_end/third_party/acorn/package/dist/acorn.mjs.d.ts diff --git a/front_end/formatter_worker/AcornTokenizer.js b/front_end/formatter_worker/AcornTokenizer.js index ccc256f94a..1f4ae9d788 100644 --- a/front_end/formatter_worker/AcornTokenizer.js +++ b/front_end/formatter_worker/AcornTokenizer.js @@ -2,13 +2,16 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// @ts-nocheck -// TODO(crbug.com/1011811): Enable TypeScript compiler checks - import * as Platform from '../platform/platform.js'; import * as TextUtils from '../text_utils/text_utils.js'; import * as Acorn from '../third_party/acorn/package/dist/acorn.mjs'; +/** + * @typedef {(!Acorn.Token|!Acorn.Comment)} + */ +// @ts-ignore typedef +export let TokenOrComment; + /** * @unrestricted */ @@ -18,17 +21,19 @@ export class AcornTokenizer { */ constructor(content) { this._content = content; + /** @type {!Array} */ this._comments = []; this._tokenizer = Acorn.tokenizer(this._content, {onComment: this._comments, ecmaVersion: ECMA_VERSION}); const contentLineEndings = Platform.StringUtilities.findLineEndingIndexes(this._content); this._textCursor = new TextUtils.TextCursor.TextCursor(contentLineEndings); this._tokenLineStart = 0; this._tokenLineEnd = 0; + this._tokenColumnStart = 0; this._nextTokenInternal(); } /** - * @param {!Acorn.Acorn.TokenOrComment} token + * @param {!Acorn.Token} token * @param {string=} values * @return {boolean} */ @@ -39,7 +44,7 @@ export class AcornTokenizer { } /** - * @param {!Acorn.Acorn.TokenOrComment} token + * @param {!Acorn.Token} token * @param {string=} keyword * @return {boolean} */ @@ -49,7 +54,7 @@ export class AcornTokenizer { } /** - * @param {!Acorn.Acorn.TokenOrComment} token + * @param {!TokenOrComment} token * @param {string=} identifier * @return {boolean} */ @@ -58,7 +63,7 @@ export class AcornTokenizer { } /** - * @param {!Acorn.Acorn.TokenOrComment} token + * @param {!TokenOrComment} token * @return {boolean} */ static lineComment(token) { @@ -66,7 +71,7 @@ export class AcornTokenizer { } /** - * @param {!Acorn.Acorn.TokenOrComment} token + * @param {!TokenOrComment} token * @return {boolean} */ static blockComment(token) { @@ -74,20 +79,20 @@ export class AcornTokenizer { } /** - * @return {!Acorn.Acorn.TokenOrComment} + * @return {!TokenOrComment} */ _nextTokenInternal() { if (this._comments.length) { - return this._comments.shift(); + return /** @type {!TokenOrComment} */ (this._comments.shift()); } const token = this._bufferedToken; this._bufferedToken = this._tokenizer.getToken(); - return token; + return /** @type {!TokenOrComment} */ (token); } /** - * @return {?Acorn.Acorn.TokenOrComment} + * @return {?TokenOrComment} */ nextToken() { const token = this._nextTokenInternal(); @@ -105,12 +110,15 @@ export class AcornTokenizer { } /** - * @return {?Acorn.Acorn.TokenOrComment} + * @return {?TokenOrComment} */ peekToken() { if (this._comments.length) { return this._comments[0]; } + if (!this._bufferedToken) { + return null; + } return this._bufferedToken.type !== Acorn.tokTypes.eof ? this._bufferedToken : null; } diff --git a/front_end/formatter_worker/JavaScriptFormatter.js b/front_end/formatter_worker/JavaScriptFormatter.js index 6d2404b2f2..817d154ad4 100644 --- a/front_end/formatter_worker/JavaScriptFormatter.js +++ b/front_end/formatter_worker/JavaScriptFormatter.js @@ -33,7 +33,7 @@ import * as Acorn from '../third_party/acorn/package/dist/acorn.mjs'; -import {AcornTokenizer, ECMA_VERSION} from './AcornTokenizer.js'; +import {AcornTokenizer, ECMA_VERSION, TokenOrComment} from './AcornTokenizer.js'; // eslint-disable-line no-unused-vars import {ESTreeWalker} from './ESTreeWalker.js'; import {FormattedContentBuilder} from './FormattedContentBuilder.js'; // eslint-disable-line no-unused-vars @@ -67,7 +67,7 @@ export class JavaScriptFormatter { } /** - * @param {?Acorn.Acorn.TokenOrComment} token + * @param {?TokenOrComment} token * @param {string} format */ _push(token, format) { @@ -100,7 +100,7 @@ export class JavaScriptFormatter { return; } while (this._tokenizer.peekToken() && this._tokenizer.peekToken().start < node.start) { - const token = /** @type {!Acorn.Acorn.TokenOrComment} */ (this._tokenizer.nextToken()); + const token = /** @type {!TokenOrComment} */ (this._tokenizer.nextToken()); const format = this._formatToken(node.parent, token); this._push(token, format); } @@ -111,7 +111,7 @@ export class JavaScriptFormatter { */ _afterVisit(node) { while (this._tokenizer.peekToken() && this._tokenizer.peekToken().start < node.end) { - const token = /** @type {!Acorn.Acorn.TokenOrComment} */ (this._tokenizer.nextToken()); + const token = /** @type {!TokenOrComment} */ (this._tokenizer.nextToken()); const format = this._formatToken(node, token); this._push(token, format); } @@ -138,17 +138,18 @@ export class JavaScriptFormatter { /** * @param {!ESTree.Node} node - * @param {!Acorn.Acorn.TokenOrComment} token + * @param {!TokenOrComment} tokenOrComment * @return {string} */ - _formatToken(node, token) { + _formatToken(node, tokenOrComment) { const AT = AcornTokenizer; - if (AT.lineComment(token)) { + if (AT.lineComment(tokenOrComment)) { return 'tn'; } - if (AT.blockComment(token)) { + if (AT.blockComment(tokenOrComment)) { return 'tn'; } + const token = /** @type {!Acorn.Token} */ (tokenOrComment); if (node.type === 'ContinueStatement' || node.type === 'BreakStatement') { return node.label && AT.keyword(token) ? 'ts' : 't'; } diff --git a/front_end/third_party/acorn/BUILD.gn b/front_end/third_party/acorn/BUILD.gn index 7144163de0..f79906a5ef 100644 --- a/front_end/third_party/acorn/BUILD.gn +++ b/front_end/third_party/acorn/BUILD.gn @@ -8,6 +8,7 @@ copy("acorn") { "package/dist/acorn.d.ts", "package/dist/acorn.js", "package/dist/acorn.mjs", + "package/dist/acorn.mjs.d.ts", "package/dist/acorn.mjs.map", ] diff --git a/front_end/third_party/acorn/package/dist/acorn.mjs.d.ts b/front_end/third_party/acorn/package/dist/acorn.mjs.d.ts new file mode 100644 index 0000000000..5f633595e5 --- /dev/null +++ b/front_end/third_party/acorn/package/dist/acorn.mjs.d.ts @@ -0,0 +1,2 @@ +import * as acorn from "./acorn"; +export = acorn; \ No newline at end of file diff --git a/front_end/third_party/acorn/package/dist/acorn_types.mjs b/front_end/third_party/acorn/package/dist/acorn_types.mjs index 7b1c58e1a5..4c3323fb00 100644 --- a/front_end/third_party/acorn/package/dist/acorn_types.mjs +++ b/front_end/third_party/acorn/package/dist/acorn_types.mjs @@ -3,7 +3,7 @@ export const Acorn = {}; * @constructor */ Acorn.Tokenizer = function() { - /** @type {function():!Acorn.Token} */ + /** @type {function():!Token} */ this.getToken; }; @@ -20,17 +20,12 @@ Acorn.TokenType = function() { /** * @typedef {{type: !Acorn.TokenType, value: string, start: number, end: number}} */ -Acorn.Token; +export let Token; /** * @typedef {{type: string, value: string, start: number, end: number}} */ -Acorn.Comment; - -/** - * @typedef {(!Acorn.Token|!Acorn.Comment)} - */ -Acorn.TokenOrComment; +export let Comment; /** * @param {string} text