From 9fec528016e1a9b8773bde5fd08f2174139e8ff5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 10 Jan 2020 16:11:10 -0500 Subject: [PATCH] Reduce false negative cases of typescript parser tests (#10979) * tests: read baesline errors for typescript parsing error * chore: add error codes * chore: tune the regex --- .../parser-tests/typescript/error-codes.js | 71 +++++++++++ scripts/parser-tests/typescript/index.js | 35 +++++- scripts/parser-tests/typescript/whitelist.txt | 110 ------------------ 3 files changed, 100 insertions(+), 116 deletions(-) create mode 100644 scripts/parser-tests/typescript/error-codes.js diff --git a/scripts/parser-tests/typescript/error-codes.js b/scripts/parser-tests/typescript/error-codes.js new file mode 100644 index 000000000000..2a2580dfb8a2 --- /dev/null +++ b/scripts/parser-tests/typescript/error-codes.js @@ -0,0 +1,71 @@ +/* +This file includes the code of TS Diagnostics error that should also be thrown from babel-parser. +The TypeScript parser is highly tolerant on errors so these error would not produce parseDiagnostics which can be checked +in the parser test runner. We check these error codes against the stderr log in the build/typescript/tests/baselines/reference + +Note that babel-parser should not throw for the TypeChecking Diagnostics + +The commented out diagnostic codes will introduce false positive cases that should be addressed in separate PRs. +*/ + +module.exports = [ + // "TS1005", // '{0}' expected. + "TS1009", // Trailing comma not allowed. + "TS1014", // A rest parameter must be last in a parameter list. + "TS1019", // An index signature parameter cannot have a question mark. + "TS1028", // Accessibility modifier already seen. + "TS1029", // '{0}' modifier must precede '{1}' modifier. + "TS1030", // '{0}' modifier already seen. + "TS1031", // '{0}' modifier cannot appear on a class element. + "TS1042", // '{0}' modifier cannot be used here. + "TS1048", // A rest parameter cannot have an initializer. + "TS1053", // A 'set' accessor cannot have rest parameter. + "TS1054", // A 'get' accessor cannot have parameters. + // "TS1071", // '{0}' modifier cannot appear on an index signature. + "TS1089", // '{0}' modifier cannot appear on a constructor declaration. + "TS1090", // '{0}' modifier cannot appear on a parameter. + "TS1100", // Invalid use of 'arguments' in strict mode. + "TS1101", // 'with' statements are not allowed in strict mode. + "TS1104", // A 'continue' statement can only be used within an enclosing iteration statement. + "TS1105", // A 'break' statement can only be used within an enclosing iteration or switch statement. + "TS1107", // Jump target cannot cross function boundary. + "TS1108", // A 'return' statement can only be used within a function body. + "TS1113", // A 'default' clause cannot appear more than once in a 'switch' statement. + "TS1115", // A 'continue' statement can only jump to a label of an enclosing iteration statement. + "TS1116", // A 'break' statement can only jump to a label of an enclosing statement. + "TS1123", // Variable declaration list cannot be empty. + "TS1141", // String literal expected. + "TS1142", // Line break not permitted here. + "TS1162", // An object member cannot be declared optional. + "TS1163", // A 'yield' expression is only allowed in a generator body. + "TS1184", // Modifiers cannot appear here. + "TS1191", // An import declaration cannot have modifiers. + "TS1196", // Catch clause variable cannot have a type annotation. + "TS1197", // Catch clause variable cannot have an initializer. + "TS1200", // Line terminator not permitted before arrow. + "TS1312", // '=' can only be used in an object literal property inside a destructuring assignment. + // "TS1212", // Identifier expected. '{0}' is a reserved word in strict mode." + // "TS1213", // Identifier expected. '{0}' is a reserved word in strict mode. Class definitions are automatically in strict mode. + // "TS1214", // Identifier expected. '{0}' is a reserved word in strict mode. Modules are automatically in strict mode. + "TS1246", // An interface property cannot have an initializer. + "TS1247", // A type literal property cannot have an initializer. + "TS1248", // A class member cannot have the 'const' keyword. + "TS1308", // 'await' expression is only allowed within an async function. + "TS2337", // Super calls are not permitted outside constructors or in nested functions inside constructors. + // "TS2300", // Duplicate identifier '{0}'. + "TS2364", // The left-hand side of an assignment expression must be a variable or a property access. + // "TS2371", // A parameter initializer is only allowed in a function or constructor implementation. + // "TS2393", // Duplicate function implementation. + "TS2396", // Duplicate identifier 'arguments'. Compiler uses 'arguments' to initialize rest parameters. + // "TS2440", // Import declaration conflicts with local declaration of '{0}'. + // "TS2451", // Cannot redeclare block-scoped variable '{0}'. + "TS2452", // An enum member cannot have a numeric name. + "TS2566", // A rest element cannot have a property name. + "TS2481", // Cannot initialize outer scoped variable '{0}' in the same scope as block scoped declaration '{0}'. + // "TS2567", // Enum declarations can only merge with namespace or other enum declarations. + "TS2659", // 'super' is only allowed in members of object literal expressions when option 'target' is 'ES2015' or higher. + "TS2660", // 'super' can only be referenced in members of derived classes or object literal expressions. + "TS2699", // Static property '{0}' conflicts with built-in property 'Function.{0}' of constructor function '{1}'. + "TS8018", // Octal literals are not allowed in enums members initializer. + // "TS17012", // '{0}' is not a valid meta-property for keyword '{1}'. Did you mean '{2}'? +]; diff --git a/scripts/parser-tests/typescript/index.js b/scripts/parser-tests/typescript/index.js index 0de0384529c7..5e3e4b2a9d63 100644 --- a/scripts/parser-tests/typescript/index.js +++ b/scripts/parser-tests/typescript/index.js @@ -1,7 +1,8 @@ const path = require("path"); const fs = require("fs").promises; -const ts = require("typescript"); +const ts = require("../../../build/typescript"); const TestRunner = require("../utils/parser-test-runner"); +const parsingErrorCodes = require("./error-codes"); async function* loadTests(dir) { const names = await fs.readdir(dir); @@ -20,11 +21,30 @@ const plugins = [ "dynamicImport", ]; +const TSTestsPath = path.join(__dirname, "../../../build/typescript/tests"); + +// Check if the baseline errors contain the codes that should also be thrown from babel-parser +async function baselineContainsParserErrorCodes(testName) { + try { + const baselineErrors = await fs.readFile( + path.join( + TSTestsPath, + "baselines/reference", + testName.replace(/\.tsx?$/, ".errors.txt") + ), + "utf8" + ); + return parsingErrorCodes.some(code => baselineErrors.includes(code)); + } catch (e) { + if (e.code !== "ENOENT") { + throw e; + } + return false; + } +} + const runner = new TestRunner({ - testDir: path.join( - __dirname, - "../../../build/typescript/tests/cases/compiler" - ), + testDir: path.join(TSTestsPath, "./cases/compiler"), whitelist: path.join(__dirname, "whitelist.txt"), logInterval: 50, shouldUpdate: process.argv.includes("--update-whitelist"), @@ -32,6 +52,7 @@ const runner = new TestRunner({ async *getTests() { for await (const test of loadTests(this.testDir)) { const isTSX = test.name.slice(-4) === ".tsx"; + const ast = ts.createSourceFile( test.name, test.contents, @@ -44,7 +65,9 @@ const runner = new TestRunner({ contents: test.contents, fileName: test.name, id: test.name, - expectedError: ast.parseDiagnostics.length > 0, + expectedError: + ast.parseDiagnostics.length > 0 || + (await baselineContainsParserErrorCodes(test.name)), sourceType: "module", plugins: isTSX ? plugins.concat("jsx") : plugins, }; diff --git a/scripts/parser-tests/typescript/whitelist.txt b/scripts/parser-tests/typescript/whitelist.txt index a69318cb0174..36d525c171eb 100644 --- a/scripts/parser-tests/typescript/whitelist.txt +++ b/scripts/parser-tests/typescript/whitelist.txt @@ -1,12 +1,10 @@ ArrowFunctionExpression1.ts -ClassDeclarationWithInvalidConstOnPropertyDeclaration.ts MemberAccessorDeclaration15.ts ParameterList13.ts ParameterList4.ts ParameterList5.ts ParameterList6.ts accessorParameterAccessibilityModifier.ts -accessorWithRestParam.ts accessorWithoutBody1.ts accessorWithoutBody2.ts aliasUsageInAccessorsOfClass.ts @@ -21,12 +19,6 @@ aliasUsageInVarAssignment.ts aliasUsedAsNameValue.ts allowImportClausesToMergeWithTypes.ts allowJscheckJsTypeParameterNoCrash.ts -alwaysStrict.ts -alwaysStrictES6.ts -alwaysStrictModule.ts -alwaysStrictModule2.ts -alwaysStrictNoImplicitUseStrict.ts -ambientWithStatements.ts amdDeclarationEmitNoExtraDeclare.ts amdModuleConstEnumUsage.ts amdModuleName2.ts @@ -34,10 +26,6 @@ anonClassDeclarationEmitIsAnon.ts anyDeclare.ts argumentsBindsToFunctionScopeArgumentList.ts arrayOfExportedClass.ts -arrowFunctionErrorSpan.ts -asiReturn.ts -assignToInvalidLHS.ts -assignmentToParenthesizedExpression1.ts asyncFunctionsAcrossFiles.ts augmentExportEquals1.ts augmentExportEquals1_1.ts @@ -59,16 +47,9 @@ augmentedTypesEnum2.ts augmentedTypesFunction.ts augmentedTypesInterface.ts augmentedTypesVar.ts -awaitInClassInAsyncFunction.ts -awaitInNonAsyncFunction.ts -awaitLiteralValues.ts bigIntWithTargetES3.ts bigintIndex.ts bigintWithLib.ts -breakNotInIterationOrSwitchStatement1.ts -breakNotInIterationOrSwitchStatement2.ts -breakTarget5.ts -breakTarget6.ts cacheResolutions.ts cachedModuleResolution1.ts cachedModuleResolution2.ts @@ -80,47 +61,23 @@ cachedModuleResolution7.ts cachedModuleResolution8.ts cachedModuleResolution9.ts callOverloads2.ts -catchClauseWithInitializer1.ts -catchClauseWithTypeAnnotation.ts checkSuperCallBeforeThisAccessing9.ts classCannotExtendVar.ts -classExpressionPropertyModifiers.ts classExpressionWithDecorator1.ts classExtendsAcrossFiles.ts classExtendsMultipleBaseClasses.ts -classHeritageWithTrailingSeparator.ts classOverloadForFunction.ts -collisionArgumentsArrowFunctions.ts -collisionArgumentsClassConstructor.ts -collisionArgumentsClassMethod.ts -collisionArgumentsFunction.ts -collisionArgumentsFunctionExpressions.ts collisionExportsRequireAndClass.ts commonSourceDir5.ts commonSourceDir6.ts commonjsSafeImport.ts -complicatedPrivacy.ts conflictingTypeAnnotatedVar.ts -constDeclarationShadowedByVarDeclaration.ts constDeclarations-invalidContexts.ts constDeclarations-scopes.ts constDeclarations-validContexts.ts constEnumNoEmitReexport.ts constEnumNoPreserveDeclarationReexport.ts constEnumPreserveEmitReexport.ts -constInClassExpression.ts -constructorArgsErrors1.ts -constructorArgsErrors2.ts -constructorArgsErrors3.ts -constructorArgsErrors4.ts -constructorArgsErrors5.ts -continueNotInIterationStatement1.ts -continueNotInIterationStatement2.ts -continueNotInIterationStatement3.ts -continueNotInIterationStatement4.ts -continueTarget1.ts -continueTarget5.ts -continueTarget6.ts convertKeywordsYes.ts declarationEmitAmdModuleNameDirective.ts declarationEmitComputedNameCausesImportToBePainted.ts @@ -143,7 +100,6 @@ declarationImportTypeAliasInferredAndEmittable.ts declarationMapsMultifile.ts declarationMapsOutFile.ts declarationsForInferredTypeFromOtherFile.ts -declareAlreadySeen.ts declareModifierOnImport1.ts decoratorMetadataRestParameterWithImportedType.ts decoratorMetadataWithImportDeclarationNameCollision.ts @@ -165,7 +121,6 @@ dependencyViaImportAlias.ts destructureOptionalParameter.ts destructuredDeclarationEmit.ts doubleUnderscoreExportStarConflict.ts -downlevelLetConst1.ts duplicateIdentifierBindingElementInParameterDeclaration1.ts duplicateIdentifierBindingElementInParameterDeclaration2.ts duplicateIdentifierEnum.ts @@ -178,7 +133,6 @@ duplicateLabel2.ts duplicateVarAndImport.ts duplicateVarAndImport2.ts duplicateVarsAcrossFileBoundaries.ts -dynamicImportTrailingComma.ts dynamicNames.ts elidedEmbeddedStatementsReplacedWithSemicolon.ts emitClassMergedWithConstNamespaceNotElided.ts @@ -186,12 +140,7 @@ emitSuperCallBeforeEmitParameterPropertyDeclaration1.ts emitSuperCallBeforeEmitParameterPropertyDeclaration1ES6.ts emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1.ts emitSuperCallBeforeEmitPropertyDeclarationAndParameterPropertyDeclaration1ES6.ts -emitThisInSuperMethodCall.ts enumGenericTypeClash.ts -enumIdentifierLiterals.ts -errorOnInitializerInInterfaceProperty.ts -errorOnInitializerInObjectTypeLiteralProperty.ts -es3-oldStyleOctalLiteralInEnums.ts es3-oldStyleOctalLiteralTypes.ts es3defaultAliasIsQuoted.ts es5-asyncFunctionWithStatements.ts @@ -203,20 +152,14 @@ es6ExportAssignment3.ts es6ImportDefaultBindingFollowedWithNamedImport.ts es6ImportDefaultBindingFollowedWithNamedImport1.ts es6ImportDefaultBindingFollowedWithNamedImport1InEs5.ts -es6ImportDefaultBindingFollowedWithNamedImport1WithExport.ts es6ImportDefaultBindingFollowedWithNamedImportDts.ts es6ImportDefaultBindingFollowedWithNamedImportDts1.ts es6ImportDefaultBindingFollowedWithNamedImportInEs5.ts -es6ImportDefaultBindingFollowedWithNamedImportWithExport.ts -es6ImportDefaultBindingFollowedWithNamespaceBinding1WithExport.ts -es6ImportDefaultBindingFollowedWithNamespaceBindingWithExport.ts es6ImportDefaultBindingMergeErrors.ts -es6ImportDefaultBindingWithExport.ts es6ImportEqualsDeclaration.ts es6ImportEqualsExportModuleCommonJsError.ts es6ImportEqualsExportModuleEs2015Error.ts es6ImportNameSpaceImportMergeErrors.ts -es6ImportNameSpaceImportWithExport.ts es6ImportNamedImport.ts es6ImportNamedImportAmd.ts es6ImportNamedImportDts.ts @@ -225,9 +168,7 @@ es6ImportNamedImportInExportAssignment.ts es6ImportNamedImportMergeErrors.ts es6ImportNamedImportNoExportMember.ts es6ImportNamedImportNoNamedExports.ts -es6ImportNamedImportWithExport.ts es6ImportNamedImportWithTypesAndValues.ts -es6ImportWithoutFromClauseWithExport.ts es6ModuleInternalNamedImports.ts es6ModuleInternalNamedImports2.ts es6UseOfTopLevelRequire.ts @@ -235,7 +176,6 @@ esModuleInterop.ts esModuleInteropImportTSLibHasImport.ts esModuleInteropNamedDefaultImports.ts esModuleInteropTslibHelpers.ts -exportAlreadySeen.ts exportAssignClassAndModule.ts exportAssignmentImportMergeNoCrash.ts exportAssignmentMembersVisibleInAugmentation.ts @@ -245,7 +185,6 @@ exportAssignmentWithExportModifier.ts exportAssignmentWithoutAllowSyntheticDefaultImportsError.ts exportClassExtendingIntersection.ts exportClassWithoutName.ts -exportDeclarationInInternalModule.ts exportDeclarationsInAmbientNamespaces.ts exportDefaultAbstractClass.ts exportDefaultAsyncFunction2.ts @@ -263,20 +202,15 @@ extendingClassFromAliasAndUsageInIndexer.ts extendsClauseAlreadySeen.ts extendsClauseAlreadySeen2.ts externalModuleAssignToVar.ts -fatarrowfunctionsOptionalArgsErrors1.ts fileWithNextLine2.ts -fileWithNextLine3.ts funClodule.ts functionAndImportNameConflict.ts functionCall15.ts functionDeclarationWithResolutionOfTypeNamedArguments01.ts functionExpressionInWithBlock.ts functionExpressionWithResolutionOfTypeNamedArguments01.ts -functionsWithModifiersInBlocks1.ts gettersAndSettersErrors.ts giant.ts -illegalModifiersOnClassElements.ts -illegalSuperCallsInConstructor.ts implementClausePrecedingExtends.ts implementsClauseAlreadySeen.ts importAndVariableDeclarationConflict1.ts @@ -285,7 +219,6 @@ importAndVariableDeclarationConflict4.ts importAsBaseClass.ts importDecl.ts importDeclWithClassModifiers.ts -importDeclWithDeclareModifier.ts importDeclWithDeclareModifierInAmbientContext.ts importHelpers.ts importHelpersAmd.ts @@ -298,25 +231,20 @@ importHelpersOutFile.ts importHelpersSystem.ts importWithTrailingSlash.ts importedModuleClassNameClash.ts -indexSignatureTypeCheck.ts -indexSignatureTypeCheck2.ts indexSignatureWithAccessibilityModifier.ts indexSignatureWithInitializer1.ts indexTypeCheck.ts indexWithoutParamType.ts -indexerAsOptional.ts indexerSignatureWithRestParam.ts initializedParameterBeforeNonoptionalNotOptional.ts interfaceMayNotBeExtendedWitACall.ts interfaceWithImplements1.ts -invalidContinueInDownlevelAsync.ts invalidReferenceSyntax1.ts isLiteral1.ts isLiteral2.ts isolatedModulesReExportType.ts jsEnumTagOnObjectFrozen.ts jsFileCompilationBindDuplicateIdentifier.ts -jsFileCompilationBindErrors.ts jsFileCompilationDuplicateFunctionImplementation.ts jsFileCompilationDuplicateFunctionImplementationFileOrderReversed.ts jsFileCompilationExternalPackageError.ts @@ -337,7 +265,6 @@ letDeclarations-scopes-duplicates6.ts letDeclarations-scopes-duplicates7.ts letDeclarations-scopes.ts letDeclarations-validContexts.ts -literalsInComputedProperties1.ts mergeWithImportedType.ts mergedDeclarations6.ts metadataOfClassFromAlias.ts @@ -346,8 +273,6 @@ metadataReferencedWithinFilteredUnion.ts mismatchedClassConstructorVariable.ts missingSemicolonInModuleSpecifier.ts misspelledNewMetaProperty.ts -modifierOnParameter1.ts -modifiersInObjectLiterals.ts modifiersOnInterfaceIndexSignature1.ts moduleAugmentationCollidingNamesInAugmentation1.ts moduleAugmentationDeclarationEmit1.ts @@ -374,9 +299,6 @@ moduleAugmentationsImports2.ts moduleAugmentationsImports3.ts moduleAugmentationsImports4.ts moduleDuplicateIdentifiers.ts -moduleElementsInWrongContext.ts -moduleElementsInWrongContext2.ts -moduleElementsInWrongContext3.ts moduleResolutionNoTs.ts moduleResolutionWithSymlinks.ts moduleResolutionWithSymlinks_withOutDir.ts @@ -385,9 +307,6 @@ moduleSharesNameWithImportDeclarationInsideIt3.ts moduleSharesNameWithImportDeclarationInsideIt5.ts module_augmentUninstantiatedModule2.ts multiImportExport.ts -multiLinePropertyAccessAndArrowFunctionIndent1.ts -multipleClassPropertyModifiers.ts -multipleClassPropertyModifiersErrors.ts multipleInheritance.ts nameCollisions.ts namespacesWithTypeAliasOnlyExportsMerge.ts @@ -402,12 +321,7 @@ nodeResolution6.ts nodeResolution8.ts nonMergedOverloads.ts numberVsBigIntOperations.ts -objectBindingPattern_restElementWithPropertyName.ts -objectLiteralMemberWithModifiers1.ts -objectLiteralMemberWithModifiers2.ts -objectLiteralMemberWithQuestionMark1.ts objectLiteralMemberWithoutBlock1.ts -objectTypeWithOptionalProperty1.ts outModuleConcatAmd.ts outModuleConcatCommonjs.ts outModuleConcatCommonjsDeclarationOnly.ts @@ -422,7 +336,6 @@ pathMappingBasedModuleResolution3_classic.ts pathMappingBasedModuleResolution3_node.ts preserveUnusedImports.ts privacyCheckExternalModuleExportAssignmentOfGenericClass.ts -privacyImportParseErrors.ts privacyTopLevelAmbientExternalModuleImportWithExport.ts privacyTopLevelAmbientExternalModuleImportWithoutExport.ts reExportGlobalDeclaration1.ts @@ -440,35 +353,16 @@ redeclareParameterInCatchBlock.ts reexportedMissingAlias.ts relativeNamesInClassicResolution.ts requireAsFunctionInExternalModule.ts -restParamAsOptional.ts restParamModifier2.ts -restParameterNotLast.ts -restParameterWithBindingPattern3.ts shadowedReservedCompilerDeclarationsWithNoEmit.ts -shadowingViaLocalValue.ts -shadowingViaLocalValueOrBindingElement.ts shorthandPropertyAssignmentInES6Module.ts -shorthandPropertyAssignmentsInDestructuring.ts -shorthandPropertyAssignmentsInDestructuring_ES6.ts sourceMap-LineBreaks.ts sourceMapValidationDecorators.ts sourceMapValidationStatements.ts -spaceBeforeQuestionMarkInPropertyAssignment.ts -standaloneBreak.ts staticIndexer.ts -staticModifierAlreadySeen.ts -staticMustPrecedePublic.ts -staticPrototypeProperty.ts strictModeReservedWord.ts strictModeReservedWordInClassDeclaration.ts superCallFromClassThatHasNoBaseType1.ts -superCallFromFunction1.ts -superCallOutsideConstructor.ts -superCallsInConstructor.ts -superInObjectLiterals_ES5.ts -superInObjectLiterals_ES6.ts -super_inside-object-literal-getters-and-setters.ts -switchStatementsWithMultipleDefaults1.ts symbolLinkDeclarationEmitModuleNames.ts symbolMergeValueAndImportedType.ts systemExportAssignment.ts @@ -480,7 +374,6 @@ systemModuleWithSuperClass.ts systemObjectShorthandRename.ts targetEs6DecoratorMetadataImportNotElided.ts targetTypeCastTest.ts -throwWithoutNewLine2.ts tsxDeepAttributeAssignabilityError.tsx typeReferenceDirectives10.ts typeReferenceDirectives11.ts @@ -490,7 +383,6 @@ typeReferenceDirectives5.ts typeReferenceDirectives7.ts typeReferenceDirectives8.ts typeReferenceDirectives9.ts -unaryOperatorsInStrictMode.ts unusedImports1.ts unusedImports11.ts unusedImports12.ts @@ -501,9 +393,7 @@ unusedImports5.ts unusedInvalidTypeArguments.ts varAndFunctionShareName.ts varArgConstructorMemberParameter.ts -variableDeclarationInStrictMode1.ts withStatement.ts withStatementErrors.ts withStatementInternalComments.ts withStatementNestedScope.ts -yieldStringLiteral.ts