From 8823e4247e58c0cd63857017b1ed54f77e809821 Mon Sep 17 00:00:00 2001 From: Brian Ng Date: Sun, 25 Feb 2018 20:12:33 -0600 Subject: [PATCH] Fix up flow errors (#7227) * charcodes@0.1.0 * Add hasFlowComment to tokenizer/state * Fix babel-types flow errors * Add isIterator to tokenizer/state * Remove unnecessary argument from flow/readToken * Add annotation to tokenizer/isIterator * Fix reference to generated index.js.flow * Add workaround in babel-template expression formatter * Fix tsEatThenParseType return type * Fix inconsistency with ParseSubscript state * Add workaround for flow handling error with tagged template in optional chain * Add flow workaround in expectPlugin inside tokenizer --- .flowconfig | 2 +- package.json | 2 +- packages/babel-template/src/formatters.js | 7 ++++--- packages/babel-types/src/definitions/utils.js | 4 ++-- packages/babylon/package.json | 2 +- packages/babylon/src/parser/expression.js | 12 +++++++----- packages/babylon/src/plugins/flow.js | 2 +- packages/babylon/src/plugins/typescript.js | 4 ++-- packages/babylon/src/tokenizer/index.js | 3 ++- packages/babylon/src/tokenizer/state.js | 4 +++- packages/babylon/src/types.js | 9 +++++++++ yarn.lock | 6 +++--- 12 files changed, 36 insertions(+), 21 deletions(-) diff --git a/.flowconfig b/.flowconfig index ff9a56ee07..a60a4a9144 100644 --- a/.flowconfig +++ b/.flowconfig @@ -13,8 +13,8 @@ codemods/*/src [libs] lib/file.js lib/parser.js -lib/packages/babel-types/lib/index.js.flow lib/third-party-libs.js.flow +packages/babel-types/lib/index.js.flow [options] include_warnings=true diff --git a/package.json b/package.json index 6638f68c9b..335e4964aa 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,7 @@ "bundle-collapser": "^1.2.1", "chai": "^4.1.0", "chalk": "^2.0.0", - "charcodes": "0.0.10", + "charcodes": "0.1.0", "derequire": "^2.0.2", "eslint": "^4.5.0", "eslint-config-babel": "^7.0.2", diff --git a/packages/babel-template/src/formatters.js b/packages/babel-template/src/formatters.js index e665e343be..4feb1d5484 100644 --- a/packages/babel-template/src/formatters.js +++ b/packages/babel-template/src/formatters.js @@ -12,7 +12,7 @@ function makeStatementFormatter( return { // We need to prepend a ";" to force statement parsing so that // ExpressionStatement strings won't be parsed as directives. - // Alonside that, we also prepend a comment so that when a syntax error + // Alongside that, we also prepend a comment so that when a syntax error // is encountered, the user will be less likely to get confused about // where the random semicolon came from. code: str => `/* @babel/template */;\n${str}`, @@ -54,16 +54,17 @@ export const statement: Formatter = makeStatementFormatter( export const expression: Formatter = { code: str => `(\n${str}\n)`, - validate: (ast: BabelNodeFile) => { - const { program } = ast; + validate: ({ program }) => { if (program.body.length > 1) { throw new Error("Found multiple statements but wanted one"); } + // $FlowFixMe const expression = program.body[0].expression; if (expression.start === 0) { throw new Error("Parse result included parens."); } }, + // $FlowFixMe unwrap: ast => ast.program.body[0].expression, }; diff --git a/packages/babel-types/src/definitions/utils.js b/packages/babel-types/src/definitions/utils.js index 4fc52eef84..45cf72b297 100644 --- a/packages/babel-types/src/definitions/utils.js +++ b/packages/babel-types/src/definitions/utils.js @@ -21,10 +21,10 @@ function getType(val) { } // TODO: Import and use Node instead of any -opaque type Validator = (any, string, any) => void; +type Validator = (any, string, any) => void; type FieldOptions = { - default?: boolean, + default?: any, optional?: boolean, validate?: Validator, }; diff --git a/packages/babylon/package.json b/packages/babylon/package.json index 5d9a7029f6..b870ce8ef1 100644 --- a/packages/babylon/package.json +++ b/packages/babylon/package.json @@ -24,7 +24,7 @@ }, "devDependencies": { "@babel/helper-fixtures": "7.0.0-beta.40", - "charcodes": "0.0.10", + "charcodes": "0.1.0", "unicode-10.0.0": "^0.7.4" }, "bin": { diff --git a/packages/babylon/src/parser/expression.js b/packages/babylon/src/parser/expression.js index 83508c035a..5845cbf613 100644 --- a/packages/babylon/src/parser/expression.js +++ b/packages/babylon/src/parser/expression.js @@ -426,7 +426,10 @@ export default class ExpressionParser extends LValParser { startLoc: Position, noCalls?: ?boolean, ): N.Expression { - const state = { stop: false }; + const state = { + optionalChainMember: false, + stop: false, + }; do { base = this.parseSubscript(base, startPos, startLoc, noCalls, state); } while (!state.stop); @@ -439,7 +442,7 @@ export default class ExpressionParser extends LValParser { startPos: number, startLoc: Position, noCalls: ?boolean, - state: { stop: boolean, optionalChainMember?: boolean }, + state: N.ParseSubscriptState, ): N.Expression { if (!noCalls && this.eat(tt.doubleColon)) { const node = this.startNodeAt(startPos, startLoc); @@ -554,14 +557,13 @@ export default class ExpressionParser extends LValParser { const node = this.startNodeAt(startPos, startLoc); node.tag = base; node.quasi = this.parseTemplate(true); - if (!state.optionalChainMember) { - return this.finishNode(node, "TaggedTemplateExpression"); - } else { + if (state.optionalChainMember) { this.raise( startPos, "Tagged Template Literals are not allowed in optionalChain", ); } + return this.finishNode(node, "TaggedTemplateExpression"); } else { state.stop = true; return base; diff --git a/packages/babylon/src/plugins/flow.js b/packages/babylon/src/plugins/flow.js index b5c904ef19..cce373f357 100644 --- a/packages/babylon/src/plugins/flow.js +++ b/packages/babylon/src/plugins/flow.js @@ -1632,7 +1632,7 @@ export default (superClass: Class): Class => return this.finishOp(tt.relational, 1); } else if (isIteratorStart(code, next)) { this.state.isIterator = true; - return super.readWord(code); + return super.readWord(); } else { return super.readToken(code); } diff --git a/packages/babylon/src/plugins/typescript.js b/packages/babylon/src/plugins/typescript.js index 5af2b00f0b..58074479e0 100644 --- a/packages/babylon/src/plugins/typescript.js +++ b/packages/babylon/src/plugins/typescript.js @@ -886,7 +886,7 @@ export default (superClass: Class): Class => } } - tsEatThenParseType(token: TokenType): N.TsType | undefined { + tsEatThenParseType(token: TokenType): N.TsType | typeof undefined { return !this.match(token) ? undefined : this.tsNextThenParseType(); } @@ -1326,7 +1326,7 @@ export default (superClass: Class): Class => startPos: number, startLoc: Position, noCalls: ?boolean, - state: { stop: boolean }, + state: N.ParseSubscriptState, ): N.Expression { if (!this.hasPrecedingLineBreak() && this.eat(tt.bang)) { const nonNullExpression: N.TsNonNullExpression = this.startNodeAt( diff --git a/packages/babylon/src/tokenizer/index.js b/packages/babylon/src/tokenizer/index.js index 45d551b755..48c1efc25d 100644 --- a/packages/babylon/src/tokenizer/index.js +++ b/packages/babylon/src/tokenizer/index.js @@ -467,6 +467,7 @@ export default class Tokenizer extends LocationParser { const assign = this.input.charCodeAt(this.state.pos + 2) === charCodes.equalsTo; if (assign) { + // $FlowFixMe this.expectPlugin("logicalAssignment"); } this.finishOp( @@ -1280,7 +1281,7 @@ export default class Tokenizer extends LocationParser { return word + this.input.slice(chunkStart, this.state.pos); } - isIterator(word): boolean { + isIterator(word: string): boolean { return word === "@@iterator" || word === "@@asyncIterator"; } diff --git a/packages/babylon/src/tokenizer/state.js b/packages/babylon/src/tokenizer/state.js index 0041dcfbf8..99046451e8 100644 --- a/packages/babylon/src/tokenizer/state.js +++ b/packages/babylon/src/tokenizer/state.js @@ -21,7 +21,7 @@ export default class State { this.noArrowParamsConversionAt = []; // eslint-disable-next-line max-len - this.inMethod = this.inFunction = this.inParameters = this.maybeInArrowParameters = this.inGenerator = this.inAsync = this.inPropertyName = this.inType = this.inClassProperty = this.noAnonFunctionType = false; + this.inMethod = this.inFunction = this.inParameters = this.maybeInArrowParameters = this.inGenerator = this.inAsync = this.inPropertyName = this.inType = this.inClassProperty = this.noAnonFunctionType = this.hasFlowComment = this.isIterator = false; this.classLevel = 0; @@ -98,6 +98,8 @@ export default class State { noAnonFunctionType: boolean; inPropertyName: boolean; inClassProperty: boolean; + hasFlowComment: boolean; + isIterator: boolean; // Check whether we are in a (nested) class or not. classLevel: number; diff --git a/packages/babylon/src/types.js b/packages/babylon/src/types.js index e892b7d159..d97999fa15 100644 --- a/packages/babylon/src/types.js +++ b/packages/babylon/src/types.js @@ -1315,3 +1315,12 @@ export type TsNonNullExpression = NodeBase & { type: "TSNonNullExpression", expression: Expression, }; + +// ================ +// Other +// ================ + +export type ParseSubscriptState = { + optionalChainMember: boolean, + stop: boolean, +}; diff --git a/yarn.lock b/yarn.lock index 345a2f985b..559da23b48 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1671,9 +1671,9 @@ chalk@^2.0.0, chalk@^2.0.1, chalk@^2.1.0: escape-string-regexp "^1.0.5" supports-color "^4.0.0" -charcodes@0.0.10: - version "0.0.10" - resolved "https://registry.yarnpkg.com/charcodes/-/charcodes-0.0.10.tgz#98d67a7a1e17ce154d1faafd01e72e9a6cff54f5" +charcodes@0.1.0: + version "0.1.0" + resolved "https://registry.yarnpkg.com/charcodes/-/charcodes-0.1.0.tgz#61f8c244fc7f94f186fe74f31078901a3ed7928e" chardet@^0.4.0: version "0.4.2"