Skip to content

Commit

Permalink
fix(fast_check): support expando properties (#428)
Browse files Browse the repository at this point in the history
Co-authored-by: David Sherret <dsherret@gmail.com>
  • Loading branch information
marvinhagemeister and dsherret authored May 13, 2024
1 parent 9ace007 commit 1f1b183
Show file tree
Hide file tree
Showing 93 changed files with 16,318 additions and 21,213 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ url = { version = "2.2.2", features = ["serde"] }

[dev-dependencies]
bincode = "1.3.3"
file_test_runner = "0.4.0"
file_test_runner = "0.7.0"
pretty_assertions = "1.0.0"
reqwest = { version = "0.12.4", default-features = false, features = ["http2", "charset", "rustls-tls-webpki-roots"] }
tempfile = "3.4.0"
Expand Down
68 changes: 39 additions & 29 deletions src/fast_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ pub enum FastCheckDiagnostic {
UnsupportedDefaultExportExpr { range: FastCheckDiagnosticRange },
#[error("found destructuring, which is not supported in the public API")]
UnsupportedDestructuring { range: FastCheckDiagnosticRange },
#[error(
"expando property referencing '{reference_name}' conflicts with '{object_name}.{reference_name}'"
)]
UnsupportedExpandoProperty {
object_name: String,
reference_name: String,
range: FastCheckDiagnosticRange,
},
#[error("found global augmentations, which are not supported")]
UnsupportedGlobalModule { range: FastCheckDiagnosticRange },
#[error("require statements are a CommonJS feature, which are not supported in ES modules")]
Expand Down Expand Up @@ -147,6 +155,7 @@ impl FastCheckDiagnostic {
UnsupportedComplexReference { .. } => Some("this is the reference"),
UnsupportedDefaultExportExpr { .. } => None,
UnsupportedDestructuring { .. } => None,
UnsupportedExpandoProperty { .. } => None,
UnsupportedGlobalModule { .. } => None,
UnsupportedRequire { .. } => None,
UnsupportedPrivateMemberReference { .. } => Some("this is the reference"),
Expand Down Expand Up @@ -176,6 +185,7 @@ impl FastCheckDiagnostic {
UnsupportedComplexReference { range, .. } => &range.specifier,
UnsupportedDefaultExportExpr { range } => &range.specifier,
UnsupportedDestructuring { range } => &range.specifier,
UnsupportedExpandoProperty { range, .. } => &range.specifier,
UnsupportedGlobalModule { range } => &range.specifier,
UnsupportedPrivateMemberReference { range, .. } => &range.specifier,
UnsupportedRequire { range } => &range.specifier,
Expand All @@ -201,6 +211,7 @@ impl FastCheckDiagnostic {
UnsupportedComplexReference { range, .. } => Some(range),
UnsupportedDefaultExportExpr { range } => Some(range),
UnsupportedDestructuring { range } => Some(range),
UnsupportedExpandoProperty { range, .. } => Some(range),
UnsupportedGlobalModule { range } => Some(range),
UnsupportedPrivateMemberReference { range, .. } => Some(range),
UnsupportedRequire { range } => Some(range),
Expand Down Expand Up @@ -228,6 +239,7 @@ impl deno_ast::diagnostics::Diagnostic for FastCheckDiagnostic {
| UnsupportedComplexReference { .. }
| UnsupportedDefaultExportExpr { .. }
| UnsupportedDestructuring { .. }
| UnsupportedExpandoProperty { .. }
| UnsupportedGlobalModule { .. }
| UnsupportedRequire { .. }
| UnsupportedPrivateMemberReference { .. }
Expand All @@ -254,6 +266,7 @@ impl deno_ast::diagnostics::Diagnostic for FastCheckDiagnostic {
UnsupportedComplexReference { .. } => "unsupported-complex-reference",
UnsupportedDefaultExportExpr { .. } => "unsupported-default-export-expr",
UnsupportedDestructuring { .. } => "unsupported-destructuring",
UnsupportedExpandoProperty { .. } => "unsupported-expando-property",
UnsupportedGlobalModule { .. } => "unsupported-global-module",
UnsupportedRequire { .. } => "unsupported-require",
UnsupportedPrivateMemberReference { .. } => {
Expand Down Expand Up @@ -308,38 +321,39 @@ impl deno_ast::diagnostics::Diagnostic for FastCheckDiagnostic {

fn hint(&self) -> Option<Cow<'_, str>> {
use FastCheckDiagnostic::*;
Some(Cow::Borrowed(match self {
Some(match self {
NotFoundReference { .. } => {
"fix the reference to point to a symbol that exists"
Cow::Borrowed("fix the reference to point to a symbol that exists")
}
MissingExplicitType { .. } => {
"add an explicit type annotation to the symbol"
Cow::Borrowed("add an explicit type annotation to the symbol")
}
MissingExplicitReturnType { .. } => {
"add an explicit return type to the function"
Cow::Borrowed("add an explicit return type to the function")
}
UnsupportedAmbientModule { .. } => {
"remove the ambient module declaration"
Cow::Borrowed("remove the ambient module declaration")
}
UnsupportedComplexReference { .. } => {
"extract the shared type to a type alias and reference the type alias instead"
Cow::Borrowed("extract the shared type to a type alias and reference the type alias instead")
}
UnsupportedDefaultExportExpr { .. } => "add an 'as' clause with an explicit type after the expression, or extract to a variable",
UnsupportedDestructuring { .. } => "separate each destructured symbol into its own export statement",
UnsupportedGlobalModule { .. } => "remove the 'global' augmentation",
UnsupportedRequire { .. } => "use an import statement instead",
UnsupportedPrivateMemberReference { .. } => "extract the type of the private member to a type alias and reference the type alias instead",
UnsupportedSuperClassExpr { .. } => "extract the superclass expression into a variable",
UnsupportedTsExportAssignment { .. } => "use an export statement instead",
UnsupportedTsNamespaceExport { .. } => "remove the namespace export",
UnsupportedUsing { .. } => "use 'const' instead of 'using'",
UnsupportedNestedJavaScript { .. } => "add a type declaration (d.ts) for the JavaScript module, or rewrite it to TypeScript",
UnsupportedJavaScriptEntrypoint { .. } => "add a type declaration (d.ts) for the JavaScript module, or rewrite it to TypeScript",
Emit { .. } => "this error may be the result of a bug in Deno - if you think this is the case, please open an issue",
UnsupportedDefaultExportExpr { .. } => Cow::Borrowed("add an 'as' clause with an explicit type after the expression, or extract to a variable"),
UnsupportedDestructuring { .. } => Cow::Borrowed("separate each destructured symbol into its own export statement"),
UnsupportedExpandoProperty { reference_name, .. } => Cow::Owned(format!("rename '{}' to something else to avoid conflicts or create a temporary variable with a different name to use in the expando property reference", reference_name)),
UnsupportedGlobalModule { .. } => Cow::Borrowed("remove the 'global' augmentation"),
UnsupportedRequire { .. } => Cow::Borrowed("use an import statement instead"),
UnsupportedPrivateMemberReference { .. } => Cow::Borrowed("extract the type of the private member to a type alias and reference the type alias instead"),
UnsupportedSuperClassExpr { .. } => Cow::Borrowed("extract the superclass expression into a variable"),
UnsupportedTsExportAssignment { .. } => Cow::Borrowed("use an export statement instead"),
UnsupportedTsNamespaceExport { .. } => Cow::Borrowed("remove the namespace export"),
UnsupportedUsing { .. } => Cow::Borrowed("use 'const' instead of 'using'"),
UnsupportedNestedJavaScript { .. } => Cow::Borrowed("add a type declaration (d.ts) for the JavaScript module, or rewrite it to TypeScript"),
UnsupportedJavaScriptEntrypoint { .. } => Cow::Borrowed("add a type declaration (d.ts) for the JavaScript module, or rewrite it to TypeScript"),
Emit { .. } => Cow::Borrowed("this error may be the result of a bug in Deno - if you think this is the case, please open an issue"),
// only a bug if the user sees these
ExportNotFound { .. } => "this error is the result of a bug in Deno and you don't be seeing it - please open an issue if one doesn't exist",
Cached { .. } => "this error is the result of a bug in Deno and you don't be seeing it - please open an issue if one doesn't exist",
}))
ExportNotFound { .. } => Cow::Borrowed("this error is the result of a bug in Deno and you don't be seeing it - please open an issue if one doesn't exist"),
Cached { .. } => Cow::Borrowed("this error is the result of a bug in Deno and you don't be seeing it - please open an issue if one doesn't exist"),
})
}

fn snippet_fixed(
Expand Down Expand Up @@ -372,6 +386,9 @@ impl deno_ast::diagnostics::Diagnostic for FastCheckDiagnostic {
UnsupportedDestructuring { .. } => Cow::Borrowed(&[
Cow::Borrowed("destructuring can not be inferred by fast check")
]),
UnsupportedExpandoProperty { .. } => Cow::Borrowed(&[
Cow::Borrowed("expando properties get converted to a namespace and the reference conflicts with a namespace export")
]),
UnsupportedGlobalModule { .. } => Cow::Borrowed(&[
Cow::Borrowed("global augmentations are not supported because they can modify global types, which can affect other modules type checking")
]),
Expand Down Expand Up @@ -571,14 +588,7 @@ fn transform_package(
.module_from_specifier(&specifier)
.unwrap_or_else(|| panic!("module not found: {}", specifier));
if let Some(module_info) = module_info.esm() {
transform::transform(
graph,
&specifier,
&ranges,
module_info.source(),
options,
)
.map(Some)
transform::transform(graph, module_info, &ranges, options).map(Some)
} else {
Ok(None) // nothing to transform
}
Expand Down
15 changes: 15 additions & 0 deletions src/fast_check/range_finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,21 @@ impl<'a> PublicRangeFinder<'a> {
}
}

// functions may contain expando property exports that are
// outside the range of the function, so include those as
// part of the found ranges
if node.is_function() {
for export_id in symbol.exports().values() {
let export_symbol =
module_info.symbol(*export_id).unwrap();
for export_decl in export_symbol.decls() {
if !decl.range.contains(&export_decl.range) {
found_ranges.insert(export_decl.range);
}
}
}
}

for dep in node.deps(ResolveDepsMode::TypesAndExpressions) {
match dep {
SymbolNodeDep::Id(id) => {
Expand Down
Loading

0 comments on commit 1f1b183

Please sign in to comment.