Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(coverage): add typescript to codegen tests #988

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
199 changes: 199 additions & 0 deletions tasks/coverage/codegen_typescript.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
codegen_typescript Summary:
AST Parsed : 5063/5063 (100.00%)
Positive Passed: 4867/5063 (96.13%)
Expect to Parse: "compiler/binopAssignmentShouldHaveType.ts"
Expect to Parse: "compiler/callOfConditionalTypeWithConcreteBranches.ts"
Expect to Parse: "compiler/callsOnComplexSignatures.tsx"
Expect to Parse: "compiler/castExpressionParentheses.ts"
Expect to Parse: "compiler/collisionExportsRequireAndInternalModuleAliasInGlobalFile.ts"
Expect to Parse: "compiler/collisionRestParameterFunctionExpressions.ts"
Expect to Parse: "compiler/collisionThisExpressionAndAliasInGlobal.ts"
Expect to Parse: "compiler/commentEmitAtEndOfFile1.ts"
Expect to Parse: "compiler/computedPropertyNameAndTypeParameterConflict.ts"
Expect to Parse: "compiler/conditionalEqualityTestingNullability.ts"
Expect to Parse: "compiler/conditionallyDuplicateOverloadsCausedByOverloadResolution.ts"
Expect to Parse: "compiler/constDeclarations-ambient.ts"
Expect to Parse: "compiler/contextualTypingOfOptionalMembers.tsx"
Expect to Parse: "compiler/contextualTypingOfTooShortOverloads.ts"
Expect to Parse: "compiler/contextuallyTypedJsxAttribute.ts"
Expect to Parse: "compiler/contextuallyTypedJsxChildren.tsx"
Expect to Parse: "compiler/declarationEmitDestructuringObjectLiteralPattern2.ts"
Expect to Parse: "compiler/declarationEmitNonExportedBindingPattern.ts"
Expect to Parse: "compiler/declarationEmitRetainsJsdocyComments.ts"
Expect to Parse: "compiler/declarationMaps.ts"
Expect to Parse: "compiler/declarationTypecheckNoUseBeforeReferenceCheck.ts"
Expect to Parse: "compiler/declareFileExportAssignment.ts"
Expect to Parse: "compiler/declareFileExportAssignmentWithVarFromVariableStatement.ts"
Expect to Parse: "compiler/discriminatedUnionJsxElement.tsx"
Expect to Parse: "compiler/dottedModuleName2.ts"
Expect to Parse: "compiler/downlevelLetConst13.ts"
Expect to Parse: "compiler/duplicateVarAndImport.ts"
Expect to Parse: "compiler/elidedEmbeddedStatementsReplacedWithSemicolon.ts"
Expect to Parse: "compiler/es6ModuleClassDeclaration.ts"
Expect to Parse: "compiler/es6ModuleConst.ts"
Expect to Parse: "compiler/es6ModuleConstEnumDeclaration.ts"
Expect to Parse: "compiler/es6ModuleConstEnumDeclaration2.ts"
Expect to Parse: "compiler/es6ModuleEnumDeclaration.ts"
Expect to Parse: "compiler/es6ModuleFunctionDeclaration.ts"
Expect to Parse: "compiler/es6ModuleInternalImport.ts"
Expect to Parse: "compiler/es6ModuleLet.ts"
Expect to Parse: "compiler/es6ModuleVariableStatement.ts"
Expect to Parse: "compiler/exportAsNamespace.d.ts"
Expect to Parse: "compiler/exportAssignValueAndType.ts"
Expect to Parse: "compiler/exportAssignmentWithPrivacyError.ts"
Expect to Parse: "compiler/exportAssignmentWithoutIdentifier1.ts"
Expect to Parse: "compiler/exportDefaultVariable.ts"
Expect to Parse: "compiler/exportEqualNamespaces.ts"
Expect to Parse: "compiler/functionReturnTypeQuery.ts"
Expect to Parse: "compiler/genericInferenceDefaultTypeParameterJsxReact.tsx"
Expect to Parse: "compiler/genericOverloadSignatures.ts"
Expect to Parse: "compiler/identityRelationNeverTypes.ts"
Expect to Parse: "compiler/indexingTypesWithNever.ts"
Expect to Parse: "compiler/inferenceErasedSignatures.ts"
Expect to Parse: "compiler/innerOverloads.ts"
Expect to Parse: "compiler/interfaceWithCommaSeparators.ts"
Expect to Parse: "compiler/jsxCallbackWithDestructuring.tsx"
Expect to Parse: "compiler/jsxComplexSignatureHasApplicabilityError.tsx"
Expect to Parse: "compiler/jsxElementClassTooManyParams.tsx"
Expect to Parse: "compiler/jsxEmitAttributeWithPreserve.tsx"
Expect to Parse: "compiler/jsxFragmentFactoryNoUnusedLocals.tsx"
Expect to Parse: "compiler/jsxGenericComponentWithSpreadingResultOfGenericFunction.tsx"
Expect to Parse: "compiler/jsxHasLiteralType.tsx"
Expect to Parse: "compiler/jsxInferenceProducesLiteralAsExpected.tsx"
Expect to Parse: "compiler/jsxIntrinsicElementsCompatability.tsx"
Expect to Parse: "compiler/jsxIntrinsicUnions.tsx"
Expect to Parse: "compiler/jsxLibraryManagedAttributesUnusedGeneric.tsx"
Expect to Parse: "compiler/jsxMultilineAttributeStringValues.tsx"
Expect to Parse: "compiler/jsxMultilineAttributeValuesReact.tsx"
Expect to Parse: "compiler/jsxNamespacedNameNotComparedToNonMatchingIndexSignature.tsx"
Expect to Parse: "compiler/jsxPartialSpread.tsx"
Expect to Parse: "compiler/jsxPropsAsIdentifierNames.tsx"
Expect to Parse: "compiler/jsxSpreadFirstUnionNoErrors.tsx"
Expect to Parse: "compiler/jsxSpreadTag.ts"
Expect to Parse: "compiler/keyofObjectWithGlobalSymbolIncluded.ts"
Expect to Parse: "compiler/keywordInJsxIdentifier.tsx"
Expect to Parse: "compiler/localClassesInLoop.ts"
Expect to Parse: "compiler/localClassesInLoop_ES6.ts"
Expect to Parse: "compiler/moduleVariables.ts"
Expect to Parse: "compiler/module_augmentUninstantiatedModule2.ts"
Expect to Parse: "compiler/numericIndexerConstraint3.ts"
Expect to Parse: "compiler/objectAssignLikeNonUnionResult.ts"
Expect to Parse: "compiler/overloadCallTest.ts"
Expect to Parse: "compiler/parseJsxExtends1.ts"
Expect to Parse: "compiler/privacyCheckAnonymousFunctionParameter.ts"
Expect to Parse: "compiler/privacyCheckAnonymousFunctionParameter2.ts"
Expect to Parse: "compiler/privacyCheckExportAssignmentOnExportedGenericInterface1.ts"
Expect to Parse: "compiler/privacyImport.ts"
Expect to Parse: "compiler/reactHOCSpreadprops.tsx"
Expect to Parse: "compiler/reactNamespaceJSXEmit.tsx"
Expect to Parse: "compiler/reactReadonlyHOCAssignabilityReal.tsx"
Expect to Parse: "compiler/reactTagNameComponentWithPropsNoOOM.tsx"
Expect to Parse: "compiler/reactTagNameComponentWithPropsNoOOM2.tsx"
Expect to Parse: "compiler/reducibleIndexedAccessTypes.ts"
Expect to Parse: "compiler/staticAnonymousTypeNotReferencingTypeParameter.ts"
Expect to Parse: "compiler/styledComponentsInstantiaionLimitNotReached.ts"
Expect to Parse: "compiler/templateLiteralIntersection.ts"
Expect to Parse: "compiler/tsxAttributeQuickinfoTypesSameAsObjectLiteral.tsx"
Expect to Parse: "compiler/tsxDiscriminantPropertyInference.tsx"
Expect to Parse: "compiler/tsxInferenceShouldNotYieldAnyOnUnions.tsx"
Expect to Parse: "compiler/tsxSpreadDoesNotReportExcessProps.tsx"
Expect to Parse: "compiler/tsxUnionMemberChecksFilterDataProps.tsx"
Expect to Parse: "compiler/typeAliasDeclarationEmit3.ts"
Expect to Parse: "compiler/typeInferenceWithExcessPropertiesJsx.tsx"
Expect to Parse: "conformance/ambient/ambientDeclarations.ts"
Expect to Parse: "conformance/ambient/ambientInsideNonAmbientExternalModule.ts"
Expect to Parse: "conformance/classes/classExpression.ts"
Expect to Parse: "conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts"
Expect to Parse: "conformance/constEnums/constEnum4.ts"
Expect to Parse: "conformance/decorators/legacyDecorators-contextualTypes.ts"
Expect to Parse: "conformance/es6/Symbols/symbolProperty55.ts"
Expect to Parse: "conformance/es6/Symbols/symbolProperty56.ts"
Expect to Parse: "conformance/es6/Symbols/symbolProperty60.ts"
Expect to Parse: "conformance/es6/yieldExpressions/generatorTypeCheck59.ts"
Expect to Parse: "conformance/es6/yieldExpressions/generatorTypeCheck61.ts"
Expect to Parse: "conformance/esDecorators/esDecorators-contextualTypes.ts"
Expect to Parse: "conformance/expressions/asOperator/asOpEmitParens.ts"
Expect to Parse: "conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts"
Expect to Parse: "conformance/expressions/typeGuards/typeGuardsInModule.ts"
Expect to Parse: "conformance/externalModules/asiPreventsParsingAsAmbientExternalModule02.ts"
Expect to Parse: "conformance/externalModules/globalAugmentationModuleResolution.ts"
Expect to Parse: "conformance/externalModules/topLevelAwait.3.ts"
Expect to Parse: "conformance/interfaces/declarationMerging/mergeThreeInterfaces.ts"
Expect to Parse: "conformance/interfaces/declarationMerging/mergeTwoInterfaces.ts"
Expect to Parse: "conformance/interfaces/declarationMerging/mergedInterfacesWithMultipleBases.ts"
Expect to Parse: "conformance/interfaces/declarationMerging/mergedInterfacesWithMultipleBases2.ts"
Expect to Parse: "conformance/interfaces/declarationMerging/twoMergedInterfacesWithDifferingOverloads2.ts"
Expect to Parse: "conformance/interfaces/interfaceDeclarations/asiPreventsParsingAsInterface03.ts"
Expect to Parse: "conformance/internalModules/moduleDeclarations/asiPreventsParsingAsNamespace03.ts"
Expect to Parse: "conformance/internalModules/moduleDeclarations/nonInstantiatedModule.ts"
Expect to Parse: "conformance/jsx/checkJsxChildrenProperty1.tsx"
Expect to Parse: "conformance/jsx/checkJsxChildrenProperty12.tsx"
Expect to Parse: "conformance/jsx/checkJsxChildrenProperty16.tsx"
Expect to Parse: "conformance/jsx/checkJsxChildrenProperty6.tsx"
Expect to Parse: "conformance/jsx/checkJsxChildrenProperty8.tsx"
Expect to Parse: "conformance/jsx/checkJsxIntersectionElementPropsType.tsx"
Expect to Parse: "conformance/jsx/checkJsxSubtleSkipContextSensitiveBug.tsx"
Expect to Parse: "conformance/jsx/checkJsxUnionSFXContextualTypeInferredCorrectly.tsx"
Expect to Parse: "conformance/jsx/jsxReactTestSuite.tsx"
Expect to Parse: "conformance/jsx/jsxs/jsxJsxsCjsTransformKeyProp.tsx"
Expect to Parse: "conformance/jsx/jsxs/jsxJsxsCjsTransformNestedSelfClosingChild.tsx"
Expect to Parse: "conformance/jsx/tsxAttributeResolution16.tsx"
Expect to Parse: "conformance/jsx/tsxDefaultAttributesResolution1.tsx"
Expect to Parse: "conformance/jsx/tsxDefaultAttributesResolution2.tsx"
Expect to Parse: "conformance/jsx/tsxElementResolution.tsx"
Expect to Parse: "conformance/jsx/tsxElementResolution13.tsx"
Expect to Parse: "conformance/jsx/tsxElementResolution14.tsx"
Expect to Parse: "conformance/jsx/tsxElementResolution5.tsx"
Expect to Parse: "conformance/jsx/tsxEmit1.tsx"
Expect to Parse: "conformance/jsx/tsxEmit2.tsx"
Expect to Parse: "conformance/jsx/tsxEmitSpreadAttribute.ts"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType1.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType2.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType3.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType4.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType5.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType6.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType7.tsx"
Expect to Parse: "conformance/jsx/tsxGenericAttributesType9.tsx"
Expect to Parse: "conformance/jsx/tsxInArrowFunction.tsx"
Expect to Parse: "conformance/jsx/tsxNamespacedAttributeName1.tsx"
Expect to Parse: "conformance/jsx/tsxNamespacedAttributeName2.tsx"
Expect to Parse: "conformance/jsx/tsxReactComponentWithDefaultTypeParameter1.tsx"
Expect to Parse: "conformance/jsx/tsxReactComponentWithDefaultTypeParameter2.tsx"
Expect to Parse: "conformance/jsx/tsxReactEmit1.tsx"
Expect to Parse: "conformance/jsx/tsxReactEmit2.tsx"
Expect to Parse: "conformance/jsx/tsxReactEmit8.tsx"
Expect to Parse: "conformance/jsx/tsxReactEmitEntities.tsx"
Expect to Parse: "conformance/jsx/tsxReactEmitNesting.tsx"
Expect to Parse: "conformance/jsx/tsxReactEmitSpreadAttribute.ts"
Expect to Parse: "conformance/jsx/tsxSpreadAttributesResolution11.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadAttributesResolution13.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadAttributesResolution15.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadAttributesResolution3.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadAttributesResolution8.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadAttributesResolution9.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadChildren.tsx"
Expect to Parse: "conformance/jsx/tsxSpreadChildrenInvalidType.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentOverload2.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentOverload3.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentOverload6.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentWithDefaultTypeParameter1.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentsWithTypeArguments1.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentsWithTypeArguments3.tsx"
Expect to Parse: "conformance/jsx/tsxStatelessFunctionComponentsWithTypeArguments5.tsx"
Expect to Parse: "conformance/jsx/tsxTypeErrors.tsx"
Expect to Parse: "conformance/jsx/tsxUnionElementType5.tsx"
Expect to Parse: "conformance/jsx/tsxUnionTypeComponent1.tsx"
Expect to Parse: "conformance/jsx/unicodeEscapesInJsxtags.tsx"
Expect to Parse: "conformance/types/namedTypes/optionalMethods.ts"
Expect to Parse: "conformance/types/rest/genericRestParameters2.ts"
Expect to Parse: "conformance/types/stringLiteral/stringLiteralTypesOverloads01.ts"
Expect to Parse: "conformance/types/stringLiteral/stringLiteralTypesOverloads02.ts"
Expect to Parse: "conformance/types/thisType/inferThisType.ts"
Expect to Parse: "conformance/types/thisType/thisTypeInFunctions4.ts"
Expect to Parse: "conformance/types/typeAliases/asiPreventsParsingAsTypeAlias02.ts"
Expect to Parse: "conformance/types/typeAliases/circularTypeAliasForUnionWithInterface.ts"
Expect to Parse: "conformance/types/typeRelationships/recursiveTypes/recursiveTypesUsedAsFunctionParameters.ts"
Expect to Parse: "conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembersOptionality.ts"
Expect to Parse: "conformance/types/typeRelationships/typeAndMemberIdentity/typeParametersAreIdenticalToThemselves.ts"
Expect to Parse: "conformance/types/typeRelationships/typeInference/intraExpressionInferencesJsx.tsx"
101 changes: 51 additions & 50 deletions tasks/coverage/src/babel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,47 @@ pub struct BabelOutput {
#[derive(Debug, Default, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BabelOptions {
pub source_type: Option<String>,
pub throws: Option<String>,
source_type: Option<String>,
throws: Option<String>,
#[serde(default)]
pub plugins: Vec<Value>, // Can be a string or an array
plugins: Vec<Value>, // Can be a string or an array
#[serde(default)]
pub allow_return_outside_function: bool,
allow_return_outside_function: bool,
#[serde(default)]
pub allow_await_outside_function: bool,
allow_await_outside_function: bool,
#[serde(default)]
pub allow_undeclared_exports: bool,
allow_undeclared_exports: bool,
}

impl BabelOptions {
fn is_jsx(&self) -> bool {
self.plugins.iter().any(|v| v.as_str().is_some_and(|v| v == "jsx"))
}

fn is_typescript(&self) -> bool {
self.plugins.iter().any(|v| {
let string_value = v.as_str().is_some_and(|v| v == "typescript");
let array_value = v.get(0).and_then(Value::as_str).is_some_and(|s| s == "typescript");
string_value || array_value
})
}

fn is_typescript_definition(&self) -> bool {
self.plugins.iter().filter_map(Value::as_array).any(|p| {
let typescript = p.get(0).and_then(Value::as_str).is_some_and(|s| s == "typescript");
let dts = p
.get(1)
.and_then(Value::as_object)
.and_then(|v| v.get("dts"))
.and_then(Value::as_bool)
.is_some_and(|v| v);
typescript && dts
})
}

fn is_module(&self) -> bool {
self.source_type.as_ref().map_or(false, |s| matches!(s.as_str(), "module" | "unambiguous"))
}
}

pub struct BabelSuite<T: Case> {
Expand Down Expand Up @@ -79,6 +110,7 @@ impl<T: Case> Suite<T> for BabelSuite<T> {
pub struct BabelCase {
path: PathBuf,
code: String,
source_type: SourceType,
options: BabelOptions,
should_fail: bool,
result: TestResult,
Expand All @@ -89,6 +121,10 @@ impl BabelCase {
self.result = result;
}

pub fn source_type(&self) -> SourceType {
self.source_type
}

fn read_file<T>(path: &Path, file_name: &'static str) -> Option<T>
where
T: DeserializeOwned,
Expand Down Expand Up @@ -150,56 +186,21 @@ impl BabelCase {
// both files doesn't exist
true
}

fn is_jsx(&self) -> bool {
self.options.plugins.iter().any(|v| v.as_str().is_some_and(|v| v == "jsx"))
}

fn is_typescript(&self) -> bool {
self.options.plugins.iter().any(|v| {
let string_value = v.as_str().is_some_and(|v| v == "typescript");
let array_value = v.get(0).and_then(Value::as_str).is_some_and(|s| s == "typescript");
string_value || array_value
})
}

fn is_typescript_definition(&self) -> bool {
self.options.plugins.iter().filter_map(Value::as_array).any(|p| {
let typescript = p.get(0).and_then(Value::as_str).is_some_and(|s| s == "typescript");
let dts = p
.get(1)
.and_then(Value::as_object)
.and_then(|v| v.get("dts"))
.and_then(Value::as_bool)
.is_some_and(|v| v);
typescript && dts
})
}

fn is_module(&self) -> bool {
self.options
.source_type
.as_ref()
.map_or(false, |s| matches!(s.as_str(), "module" | "unambiguous"))
}

/// # Panics
pub fn source_type(&self) -> SourceType {
SourceType::from_path(self.path())
.unwrap()
.with_script(true)
.with_jsx(self.is_jsx())
.with_typescript(self.is_typescript())
.with_typescript_definition(self.is_typescript_definition())
.with_module(self.is_module())
}
}

impl Case for BabelCase {
/// # Panics
fn new(path: PathBuf, code: String) -> Self {
let options = Self::read_options_json(&path);
let source_type = SourceType::from_path(&path)
.unwrap()
.with_script(true)
.with_jsx(options.is_jsx())
.with_typescript(options.is_typescript())
.with_typescript_definition(options.is_typescript_definition())
.with_module(options.is_module());
let should_fail = Self::determine_should_fail(&path, &options);
Self { path, code, options, should_fail, result: TestResult::ToBeRun }
Self { path, code, source_type, options, should_fail, result: TestResult::ToBeRun }
}

fn code(&self) -> &str {
Expand Down
34 changes: 34 additions & 0 deletions tasks/coverage/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
babel::BabelCase,
suite::{Case, TestResult},
test262::{Test262Case, TestFlag},
typescript::TypeScriptCase,
};

pub struct CodegenTest262Case {
Expand Down Expand Up @@ -78,6 +79,39 @@ impl Case for CodegenBabelCase {
}
}

pub struct CodegenTypeScriptCase {
base: TypeScriptCase,
}

impl Case for CodegenTypeScriptCase {
fn new(path: PathBuf, code: String) -> Self {
Self { base: TypeScriptCase::new(path, code) }
}

fn code(&self) -> &str {
self.base.code()
}

fn path(&self) -> &Path {
self.base.path()
}

fn test_result(&self) -> &TestResult {
self.base.test_result()
}

fn skip_test_case(&self) -> bool {
self.base.skip_test_case() || self.base.should_fail()
}

fn run(&mut self) {
let source_text = self.base.code();
let source_type = self.base.source_type();
let result = get_result(source_text, source_type, CodegenOptions);
self.base.set_result(result);
}
}

fn get_result(source_text: &str, source_type: SourceType, options: CodegenOptions) -> TestResult {
if !get_normal_result(source_text, source_type, options)
|| !get_minify_result(source_text, source_type, options)
Expand Down
Loading