Skip to content

Commit

Permalink
Typecheck formatter_worker/AcornTokenizer.js with TypeScript
Browse files Browse the repository at this point in the history
The `.mjs.d.ts` file has been upstreamed in
acornjs/acorn#954 as per the instructions in
microsoft/TypeScript#27957 (comment)

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 <tvanderlippe@chromium.org>
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Reviewed-by: Benedikt Meurer <bmeurer@chromium.org>
  • Loading branch information
TimvdLippe authored and Commit Bot committed May 29, 2020
1 parent 4f51138 commit 9358906
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
34 changes: 21 additions & 13 deletions front_end/formatter_worker/AcornTokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -18,17 +21,19 @@ export class AcornTokenizer {
*/
constructor(content) {
this._content = content;
/** @type {!Array<!Acorn.Comment>} */
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}
*/
Expand All @@ -39,7 +44,7 @@ export class AcornTokenizer {
}

/**
* @param {!Acorn.Acorn.TokenOrComment} token
* @param {!Acorn.Token} token
* @param {string=} keyword
* @return {boolean}
*/
Expand All @@ -49,7 +54,7 @@ export class AcornTokenizer {
}

/**
* @param {!Acorn.Acorn.TokenOrComment} token
* @param {!TokenOrComment} token
* @param {string=} identifier
* @return {boolean}
*/
Expand All @@ -58,36 +63,36 @@ export class AcornTokenizer {
}

/**
* @param {!Acorn.Acorn.TokenOrComment} token
* @param {!TokenOrComment} token
* @return {boolean}
*/
static lineComment(token) {
return token.type === 'Line';
}

/**
* @param {!Acorn.Acorn.TokenOrComment} token
* @param {!TokenOrComment} token
* @return {boolean}
*/
static blockComment(token) {
return token.type === 'Block';
}

/**
* @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();
Expand All @@ -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;
}

Expand Down
17 changes: 9 additions & 8 deletions front_end/formatter_worker/JavaScriptFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -67,7 +67,7 @@ export class JavaScriptFormatter {
}

/**
* @param {?Acorn.Acorn.TokenOrComment} token
* @param {?TokenOrComment} token
* @param {string} format
*/
_push(token, format) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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';
}
Expand Down
1 change: 1 addition & 0 deletions front_end/third_party/acorn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]

Expand Down
2 changes: 2 additions & 0 deletions front_end/third_party/acorn/package/dist/acorn.mjs.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import * as acorn from "./acorn";
export = acorn;
11 changes: 3 additions & 8 deletions front_end/third_party/acorn/package/dist/acorn_types.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const Acorn = {};
* @constructor
*/
Acorn.Tokenizer = function() {
/** @type {function():!Acorn.Token} */
/** @type {function():!Token} */
this.getToken;
};

Expand All @@ -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
Expand Down

0 comments on commit 9358906

Please sign in to comment.