From f2b51c2e6f6b7aa3a7356d16049b6abc7087e0e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Mon, 2 Mar 2020 21:55:49 -0500 Subject: [PATCH] address review comments --- packages/babel-parser/src/plugins/flow.js | 78 +++++++++---------- .../babel-parser/src/plugins/jsx/index.js | 16 ++-- .../src/plugins/typescript/index.js | 42 +++++----- 3 files changed, 68 insertions(+), 68 deletions(-) diff --git a/packages/babel-parser/src/plugins/flow.js b/packages/babel-parser/src/plugins/flow.js index 9eaf3c9851a7..0f9958c4a66b 100644 --- a/packages/babel-parser/src/plugins/flow.js +++ b/packages/babel-parser/src/plugins/flow.js @@ -45,7 +45,7 @@ const reservedTypes = new Set([ /* eslint sort-keys: "error" */ // The Errors key follows https://github.com/facebook/flow/blob/master/src/parser/parse_error.ml unless it does not exist -const flowErrors = Object.freeze({ +const FlowErrors = Object.freeze({ AmbiguousConditionalArrow: "Ambiguous expression: wrap the arrow functions in parentheses to disambiguate.", AmbiguousDeclareModuleKind: @@ -245,7 +245,7 @@ export default (superClass: Class): Class => moduloLoc.line !== checksLoc.line || moduloLoc.column !== checksLoc.column - 1 ) { - this.raise(moduloPos, flowErrors.UnexpectedSpaceBetweenModuloChecks); + this.raise(moduloPos, FlowErrors.UnexpectedSpaceBetweenModuloChecks); } if (this.eat(tt.parenL)) { node.value = this.parseExpression(); @@ -338,7 +338,7 @@ export default (superClass: Class): Class => return this.flowParseDeclareModuleExports(node); } else { if (insideModule) { - this.raise(this.state.lastTokStart, flowErrors.NestedDeclareModule); + this.raise(this.state.lastTokStart, FlowErrors.NestedDeclareModule); } return this.flowParseDeclareModule(node); } @@ -387,14 +387,14 @@ export default (superClass: Class): Class => if (!this.isContextual("type") && !this.match(tt._typeof)) { this.raise( this.state.lastTokStart, - flowErrors.InvalidNonTypeImportInDeclareModule, + FlowErrors.InvalidNonTypeImportInDeclareModule, ); } this.parseImport(bodyNode); } else { this.expectContextual( "declare", - flowErrors.UnsupportedStatementInDeclareModule, + FlowErrors.UnsupportedStatementInDeclareModule, ); bodyNode = this.flowParseDeclare(bodyNode, true); @@ -416,7 +416,7 @@ export default (superClass: Class): Class => if (kind === "CommonJS") { this.raise( bodyElement.start, - flowErrors.AmbiguousDeclareModuleKind, + FlowErrors.AmbiguousDeclareModuleKind, ); } kind = "ES"; @@ -424,13 +424,13 @@ export default (superClass: Class): Class => if (hasModuleExport) { this.raise( bodyElement.start, - flowErrors.DuplicateDeclareModuleExports, + FlowErrors.DuplicateDeclareModuleExports, ); } if (kind === "ES") { this.raise( bodyElement.start, - flowErrors.AmbiguousDeclareModuleKind, + FlowErrors.AmbiguousDeclareModuleKind, ); } kind = "CommonJS"; @@ -472,7 +472,7 @@ export default (superClass: Class): Class => const suggestion = exportSuggestions[label]; throw this.raise( this.state.start, - flowErrors.UnsupportedDeclareExportKind, + FlowErrors.UnsupportedDeclareExportKind, label, suggestion, ); @@ -630,7 +630,7 @@ export default (superClass: Class): Class => checkNotUnderscore(word: string) { if (word === "_") { - this.raise(this.state.start, flowErrors.UnexpectedReservedUnderscore); + this.raise(this.state.start, FlowErrors.UnexpectedReservedUnderscore); } } @@ -640,8 +640,8 @@ export default (superClass: Class): Class => this.raise( startLoc, declaration - ? flowErrors.AssignReservedType - : flowErrors.UnexpectedReservedType, + ? FlowErrors.AssignReservedType + : FlowErrors.UnexpectedReservedType, word, ); } @@ -726,7 +726,7 @@ export default (superClass: Class): Class => node.default = this.flowParseType(); } else { if (requireDefault) { - this.raise(nodeStart, flowErrors.MissingTypeParamDefault); + this.raise(nodeStart, FlowErrors.MissingTypeParamDefault); } } @@ -1061,7 +1061,7 @@ export default (superClass: Class): Class => ) { this.raise( inexactStart, - flowErrors.UnexpectedExplicitInexactInObject, + FlowErrors.UnexpectedExplicitInexactInObject, ); } } @@ -1104,26 +1104,26 @@ export default (superClass: Class): Class => if (!allowSpread) { this.raise( this.state.lastTokStart, - flowErrors.InexactInsideNonObject, + FlowErrors.InexactInsideNonObject, ); } else if (!allowInexact) { - this.raise(this.state.lastTokStart, flowErrors.InexactInsideExact); + this.raise(this.state.lastTokStart, FlowErrors.InexactInsideExact); } if (variance) { - this.raise(variance.start, flowErrors.InexactVariance); + this.raise(variance.start, FlowErrors.InexactVariance); } return null; } if (!allowSpread) { - this.raise(this.state.lastTokStart, flowErrors.UnexpectedSpreadType); + this.raise(this.state.lastTokStart, FlowErrors.UnexpectedSpreadType); } if (protoStart != null) { this.unexpected(protoStart); } if (variance) { - this.raise(variance.start, flowErrors.SpreadVariance); + this.raise(variance.start, FlowErrors.SpreadVariance); } node.argument = this.flowParseType(); @@ -1495,7 +1495,7 @@ export default (superClass: Class): Class => throw this.raise( this.state.start, - flowErrors.UnexpectedSubtractionOperand, + FlowErrors.UnexpectedSubtractionOperand, ); } @@ -1858,7 +1858,7 @@ export default (superClass: Class): Class => // e.g. Source: a ? (b): c => (d): e => f // Result 1: a ? b : (c => ((d): e => f)) // Result 2: a ? ((b): c => d) : (e => f) - this.raise(state.start, flowErrors.AmbiguousConditionalArrow); + this.raise(state.start, FlowErrors.AmbiguousConditionalArrow); } if (failed && valid.length === 1) { @@ -2180,7 +2180,7 @@ export default (superClass: Class): Class => (!expr.extra || !expr.extra.parenthesized) && (exprList.length > 1 || !isParenthesizedExpr) ) { - this.raise(expr.typeAnnotation.start, flowErrors.TypeCastInPattern); + this.raise(expr.typeAnnotation.start, FlowErrors.TypeCastInPattern); } } @@ -2355,7 +2355,7 @@ export default (superClass: Class): Class => parseAssignableListItemTypes(param: N.Pattern): N.Pattern { if (this.eat(tt.question)) { if (param.type !== "Identifier") { - this.raise(param.start, flowErrors.OptionalBindingPattern); + this.raise(param.start, FlowErrors.OptionalBindingPattern); } ((param: any): N.Identifier).optional = true; @@ -2379,7 +2379,7 @@ export default (superClass: Class): Class => node.typeAnnotation && node.right.start < node.typeAnnotation.start ) { - this.raise(node.typeAnnotation.start, flowErrors.TypeBeforeInitializer); + this.raise(node.typeAnnotation.start, FlowErrors.TypeBeforeInitializer); } return node; @@ -2503,7 +2503,7 @@ export default (superClass: Class): Class => if (nodeIsTypeImport && specifierIsTypeImport) { this.raise( firstIdentLoc, - flowErrors.ImportTypeShorthandOnlyInPureImport, + FlowErrors.ImportTypeShorthandOnlyInPureImport, ); } @@ -2681,7 +2681,7 @@ export default (superClass: Class): Class => /*:: invariant(typeParameters) */ throw this.raise( typeParameters.start, - flowErrors.UnexpectedTokenAfterTypeParameter, + FlowErrors.UnexpectedTokenAfterTypeParameter, ); } @@ -2940,7 +2940,7 @@ export default (superClass: Class): Class => parseTopLevel(file: N.File, program: N.Program): N.File { const fileNode = super.parseTopLevel(file, program); if (this.state.hasFlowComment) { - this.raise(this.state.pos, flowErrors.UnterminatedFlowComment); + this.raise(this.state.pos, FlowErrors.UnterminatedFlowComment); } return fileNode; } @@ -2948,7 +2948,7 @@ export default (superClass: Class): Class => skipBlockComment(): void { if (this.hasPlugin("flowComments") && this.skipFlowComment()) { if (this.state.hasFlowComment) { - this.unexpected(null, flowErrors.NestedFlowComment); + this.unexpected(null, FlowErrors.NestedFlowComment); } this.hasFlowCommentCompletion(); this.state.pos += this.skipFlowComment(); @@ -3014,7 +3014,7 @@ export default (superClass: Class): Class => ): void { this.raise( pos, - flowErrors.EnumBooleanMemberNotInitialized, + FlowErrors.EnumBooleanMemberNotInitialized, memberName, enumName, ); @@ -3027,7 +3027,7 @@ export default (superClass: Class): Class => const suggestion = memberName[0].toUpperCase() + memberName.slice(1); this.raise( pos, - flowErrors.EnumInvalidMemberName, + FlowErrors.EnumInvalidMemberName, memberName, suggestion, enumName, @@ -3038,14 +3038,14 @@ export default (superClass: Class): Class => pos: number, { enumName, memberName }: { enumName: string, memberName: string }, ): void { - this.raise(pos, flowErrors.EnumDuplicateMemberName, memberName, enumName); + this.raise(pos, FlowErrors.EnumDuplicateMemberName, memberName, enumName); } flowEnumErrorInconsistentMemberValues( pos: number, { enumName }: { enumName: string }, ): void { - this.raise(pos, flowErrors.EnumInconsistentMemberValues, enumName); + this.raise(pos, FlowErrors.EnumInconsistentMemberValues, enumName); } flowEnumErrorInvalidExplicitType( @@ -3058,8 +3058,8 @@ export default (superClass: Class): Class => return this.raise( pos, suppliedType === null - ? flowErrors.EnumInvalidExplicitTypeUnknownSupplied - : flowErrors.EnumInvalidExplicitType, + ? FlowErrors.EnumInvalidExplicitTypeUnknownSupplied + : FlowErrors.EnumInvalidExplicitType, enumName, suppliedType, ); @@ -3074,14 +3074,14 @@ export default (superClass: Class): Class => case "boolean": case "number": case "string": - message = flowErrors.EnumInvalidMemberInitializerPrimaryType; + message = FlowErrors.EnumInvalidMemberInitializerPrimaryType; break; case "symbol": - message = flowErrors.EnumInvalidMemberInitializerSymbolType; + message = FlowErrors.EnumInvalidMemberInitializerSymbolType; break; default: // null - message = flowErrors.EnumInvalidMemberInitializerUnknownType; + message = FlowErrors.EnumInvalidMemberInitializerUnknownType; } return this.raise(pos, message, enumName, memberName, explicitType); } @@ -3092,7 +3092,7 @@ export default (superClass: Class): Class => ): void { this.raise( pos, - flowErrors.EnumNumberMemberNotInitialized, + FlowErrors.EnumNumberMemberNotInitialized, enumName, memberName, ); @@ -3104,7 +3104,7 @@ export default (superClass: Class): Class => ): void { this.raise( pos, - flowErrors.EnumStringMemberInconsistentlyInitailized, + FlowErrors.EnumStringMemberInconsistentlyInitailized, enumName, ); } diff --git a/packages/babel-parser/src/plugins/jsx/index.js b/packages/babel-parser/src/plugins/jsx/index.js index 8e123f6288ba..6158cae7579a 100644 --- a/packages/babel-parser/src/plugins/jsx/index.js +++ b/packages/babel-parser/src/plugins/jsx/index.js @@ -16,7 +16,7 @@ import { Errors } from "../../parser/location"; const HEX_NUMBER = /^[\da-fA-F]+$/; const DECIMAL_NUMBER = /^\d+$/; -const jsxErrors = Object.freeze({ +const JsxErrors = Object.freeze({ AttributeIsEmpty: "JSX attributes must only be assigned a non-empty expression", MissingClosingTagFragment: "Expected corresponding JSX closing tag for <>", @@ -96,7 +96,7 @@ export default (superClass: Class): Class => let chunkStart = this.state.pos; for (;;) { if (this.state.pos >= this.length) { - throw this.raise(this.state.start, jsxErrors.UnterminatedJsxContent); + throw this.raise(this.state.start, JsxErrors.UnterminatedJsxContent); } const ch = this.input.charCodeAt(this.state.pos); @@ -293,7 +293,7 @@ export default (superClass: Class): Class => this.next(); node = this.jsxParseExpressionContainer(node); if (node.expression.type === "JSXEmptyExpression") { - this.raise(node.start, jsxErrors.AttributeIsEmpty); + this.raise(node.start, JsxErrors.AttributeIsEmpty); } return node; @@ -302,7 +302,7 @@ export default (superClass: Class): Class => return this.parseExprAtom(); default: - throw this.raise(this.state.start, jsxErrors.UnsupportedJsxValue); + throw this.raise(this.state.start, JsxErrors.UnsupportedJsxValue); } } @@ -457,13 +457,13 @@ export default (superClass: Class): Class => this.raise( // $FlowIgnore closingElement.start, - jsxErrors.MissingClosingTagFragment, + JsxErrors.MissingClosingTagFragment, ); } else if (!isFragment(openingElement) && isFragment(closingElement)) { this.raise( // $FlowIgnore closingElement.start, - jsxErrors.MissingClosingTagElement, + JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name), ); } else if (!isFragment(openingElement) && !isFragment(closingElement)) { @@ -475,7 +475,7 @@ export default (superClass: Class): Class => this.raise( // $FlowIgnore closingElement.start, - jsxErrors.MissingClosingTagElement, + JsxErrors.MissingClosingTagElement, getQualifiedJSXName(openingElement.name), ); } @@ -493,7 +493,7 @@ export default (superClass: Class): Class => if (this.isRelational("<")) { throw this.raise( this.state.start, - jsxErrors.UnwrappedAdjacentJSXElements, + JsxErrors.UnwrappedAdjacentJSXElements, ); } diff --git a/packages/babel-parser/src/plugins/typescript/index.js b/packages/babel-parser/src/plugins/typescript/index.js index d981058a079d..9b6273c649e8 100644 --- a/packages/babel-parser/src/plugins/typescript/index.js +++ b/packages/babel-parser/src/plugins/typescript/index.js @@ -59,7 +59,7 @@ type ParsingContext = | "TypeMembers" | "TypeParametersOrArguments"; -const tsErrors = Object.freeze({ +const TSErrors = Object.freeze({ ClassMethodHasDeclare: "Class methods cannot have the 'declare' modifier", ClassMethodHasReadonly: "Class methods cannot have the 'readonly' modifier", DeclareClassFieldHasInitializer: @@ -186,7 +186,7 @@ export default (superClass: Class): Class => if (!modifier) break; if (Object.hasOwnProperty.call(modified, modifier)) { - this.raise(startPos, tsErrors.DuplicateModifier, modifier); + this.raise(startPos, TSErrors.DuplicateModifier, modifier); } modified[modifier] = true; } @@ -300,7 +300,7 @@ export default (superClass: Class): Class => this.expect(tt._import); this.expect(tt.parenL); if (!this.match(tt.string)) { - this.raise(this.state.start, tsErrors.UnsupportedImportTypeArgument); + this.raise(this.state.start, TSErrors.UnsupportedImportTypeArgument); } // For compatibility to estree we cannot call parseLiteral directly here @@ -436,7 +436,7 @@ export default (superClass: Class): Class => ) { this.raise( pattern.start, - tsErrors.UnsupportedSignatureParameterKind, + TSErrors.UnsupportedSignatureParameterKind, pattern.type, ); } @@ -635,7 +635,7 @@ export default (superClass: Class): Class => if (elementNode.type === "TSOptionalType") { seenOptionalElement = true; } else if (seenOptionalElement && elementNode.type !== "TSRestType") { - this.raise(elementNode.start, tsErrors.OptionalTypeBeforeRequired); + this.raise(elementNode.start, TSErrors.OptionalTypeBeforeRequired); } }); @@ -709,7 +709,7 @@ export default (superClass: Class): Class => if (templateNode.expressions.length > 0) { this.raise( templateNode.expressions[0].start, - tsErrors.TemplateTypeHasSubstitution, + TSErrors.TemplateTypeHasSubstitution, ); } node.literal = templateNode; @@ -821,7 +821,7 @@ export default (superClass: Class): Class => case "TSArrayType": return; default: - this.raise(node.start, tsErrors.UnexpectedReadonly); + this.raise(node.start, TSErrors.UnexpectedReadonly); } } @@ -1127,7 +1127,7 @@ export default (superClass: Class): Class => ); if (!delimitedList.length) { - this.raise(originalStart, tsErrors.EmptyHeritageClauseType, descriptor); + this.raise(originalStart, TSErrors.EmptyHeritageClauseType, descriptor); } return delimitedList; @@ -1668,7 +1668,7 @@ export default (superClass: Class): Class => if (accessibility) pp.accessibility = accessibility; if (readonly) pp.readonly = readonly; if (elt.type !== "Identifier" && elt.type !== "AssignmentPattern") { - this.raise(pp.start, tsErrors.UnsupportedParameterPropertyKind); + this.raise(pp.start, TSErrors.UnsupportedParameterPropertyKind); } pp.parameter = ((elt: any): N.Identifier | N.AssignmentPattern); return this.finishNode(pp, "TSParameterProperty"); @@ -1967,15 +1967,15 @@ export default (superClass: Class): Class => classBody.body.push(idx); if ((member: any).abstract) { - this.raise(member.start, tsErrors.IndexSignatureHasAbstract); + this.raise(member.start, TSErrors.IndexSignatureHasAbstract); } if (isStatic) { - this.raise(member.start, tsErrors.IndexSignatureHasStatic); + this.raise(member.start, TSErrors.IndexSignatureHasStatic); } if ((member: any).accessibility) { this.raise( member.start, - tsErrors.IndexSignatureHasAccessibility, + TSErrors.IndexSignatureHasAccessibility, (member: any).accessibility, ); } @@ -2001,11 +2001,11 @@ export default (superClass: Class): Class => if (optional) methodOrProp.optional = true; if ((methodOrProp: any).readonly && this.match(tt.parenL)) { - this.raise(methodOrProp.start, tsErrors.ClassMethodHasReadonly); + this.raise(methodOrProp.start, TSErrors.ClassMethodHasReadonly); } if ((methodOrProp: any).declare && this.match(tt.parenL)) { - this.raise(methodOrProp.start, tsErrors.ClassMethodHasDeclare); + this.raise(methodOrProp.start, TSErrors.ClassMethodHasDeclare); } } @@ -2155,7 +2155,7 @@ export default (superClass: Class): Class => this.parseClassPropertyAnnotation(node); if (node.declare && this.match(tt.equal)) { - this.raise(this.state.start, tsErrors.DeclareClassFieldHasInitializer); + this.raise(this.state.start, TSErrors.DeclareClassFieldHasInitializer); } return super.parseClassProperty(node); @@ -2166,14 +2166,14 @@ export default (superClass: Class): Class => ): N.ClassPrivateProperty { // $FlowIgnore if (node.abstract) { - this.raise(node.start, tsErrors.PrivateElementHasAbstract); + this.raise(node.start, TSErrors.PrivateElementHasAbstract); } // $FlowIgnore if (node.accessibility) { this.raise( node.start, - tsErrors.PrivateElementHasAccessibility, + TSErrors.PrivateElementHasAccessibility, node.accessibility, ); } @@ -2397,7 +2397,7 @@ export default (superClass: Class): Class => parseAssignableListItemTypes(param: N.Pattern) { if (this.eat(tt.question)) { if (param.type !== "Identifier") { - this.raise(param.start, tsErrors.PatternIsOptional); + this.raise(param.start, TSErrors.PatternIsOptional); } ((param: any): N.Identifier).optional = true; @@ -2512,7 +2512,7 @@ export default (superClass: Class): Class => ) { this.raise( node.typeAnnotation.start, - tsErrors.TypeAnnotationAfterAssign, + TSErrors.TypeAnnotationAfterAssign, ); } @@ -2541,7 +2541,7 @@ export default (superClass: Class): Class => if (!this.state.maybeInArrowParameters) { exprList[i] = this.typeCastToParameter(expr); } else { - this.raise(expr.start, tsErrors.UnexpectedTypeCastInParameter); + this.raise(expr.start, TSErrors.UnexpectedTypeCastInParameter); } break; } @@ -2568,7 +2568,7 @@ export default (superClass: Class): Class => for (let i = 0; i < exprList.length; i++) { const expr = exprList[i]; if (expr && expr.type === "TSTypeCastExpression") { - this.raise(expr.start, tsErrors.UnexpectedTypeAnnotation); + this.raise(expr.start, TSErrors.UnexpectedTypeAnnotation); } }