Skip to content

Commit

Permalink
Warn on not-yet-supported import.meta usage (vercel#2397)
Browse files Browse the repository at this point in the history
This implements a warning when `import.meta` is referenced, both as a standalone expression (`import.meta`) and as a member expression (`import.meta.url`, `import.meta.foo`, etc.).

To do: 
* [x] This expression covers both `import.meta` as well as `new.target`. Investigate how this usage is impacted and handle it.

Test Plan: 
* Added a `import.meta` expression and `import.meta.url` member expression in a test app and verified both were flagged (see below)
* In the same test app, verified using `new.target` was not flagged.

```
path/to/testapp/src/index.jsx:1:12  error TP1106 import.meta is not yet supported
       1 > console.log(import.meta);
       2   console.log(import.meta.url);
       3   
       4   function Foo() {
       5     new.target;

path/to/testapp/src/index.jsx:2:12  error TP1106 import.meta is not yet supported
       1   console.log(import.meta);
       2 > console.log(import.meta.url);
       3   
       4   function Foo() {
       5     new.target;
       6   }
```
  • Loading branch information
wbinnssmith authored Oct 27, 2022
1 parent 90aed6c commit 75479b0
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions crates/turbopack-ecmascript/src/analyzer/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ pub enum Effect {
ast_path: Vec<AstParentKind>,
span: Span,
},
ImportMeta {
span: Span,
ast_path: Vec<AstParentKind>,
},
}

impl Effect {
Expand Down Expand Up @@ -86,6 +90,10 @@ impl Effect {
ast_path: _,
span: _,
} => {}
Effect::ImportMeta {
span: _,
ast_path: _,
} => {}
}
}
}
Expand Down Expand Up @@ -1093,6 +1101,21 @@ impl VisitAstPath for Analyzer<'_> {
})
}
}

fn visit_meta_prop_expr<'ast: 'r, 'r>(
&mut self,
expr: &'ast MetaPropExpr,
ast_path: &mut AstNodePath<AstParentNodeRef<'r>>,
) {
if expr.kind == MetaPropKind::ImportMeta {
// MetaPropExpr also covers `new.target`. Only consider `import.meta`
// an effect.
self.data.effects.push(Effect::ImportMeta {
span: expr.span,
ast_path: as_parent_path(ast_path),
})
}
}
}

impl<'a> Analyzer<'a> {
Expand Down
1 change: 1 addition & 0 deletions crates/turbopack-ecmascript/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ pub mod failed_to_analyse {
pub const NODE_RESOLVE_FROM: &str = "TP1104";
pub const NODE_PROTOBUF_LOADER: &str = "TP1105";
pub const AMD_DEFINE: &str = "TP1200";
pub const IMPORT_META: &str = "TP1106";
}
}
9 changes: 9 additions & 0 deletions crates/turbopack-ecmascript/src/references/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,15 @@ pub(crate) async fn analyze_ecmascript_module(
}
}
}
Effect::ImportMeta { span, ast_path: _ } => {
handler.span_warn_with_code(
span,
"import.meta is not yet supported",
DiagnosticId::Error(
errors::failed_to_analyse::ecmascript::IMPORT_META.to_string(),
),
);
}
}
}
}
Expand Down

0 comments on commit 75479b0

Please sign in to comment.