summaryrefslogtreecommitdiff
path: root/cli/node/analyze.rs
diff options
context:
space:
mode:
authorDavid Sherret <dsherret@users.noreply.github.com>2023-04-21 21:02:46 -0400
committerGitHub <noreply@github.com>2023-04-21 21:02:46 -0400
commita615eb3b56545960ec9684991442dd34a8b2abfc (patch)
tree05e134487fd3e8aadfe513a70ee246c95633fa34 /cli/node/analyze.rs
parent779d379c68d1489cc01f6a2bfbcf677e08ca6d40 (diff)
refactor(node): move most of cli/node to ext/node (#18797)
This is just a straight refactor and I didn't do any cleanup in ext/node. After this PR we can start to clean it up and make things private that don't need to be public anymore.
Diffstat (limited to 'cli/node/analyze.rs')
-rw-r--r--cli/node/analyze.rs178
1 files changed, 0 insertions, 178 deletions
diff --git a/cli/node/analyze.rs b/cli/node/analyze.rs
deleted file mode 100644
index 27818639e..000000000
--- a/cli/node/analyze.rs
+++ /dev/null
@@ -1,178 +0,0 @@
-// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
-
-use std::collections::HashSet;
-
-use deno_ast::swc::common::SyntaxContext;
-use deno_ast::view::Node;
-use deno_ast::view::NodeTrait;
-use deno_ast::CjsAnalysis;
-use deno_ast::MediaType;
-use deno_ast::ModuleSpecifier;
-use deno_ast::ParsedSource;
-use deno_ast::SourceRanged;
-use deno_core::error::AnyError;
-use deno_runtime::deno_node::analyze::CjsAnalysis as ExtNodeCjsAnalysis;
-use deno_runtime::deno_node::analyze::CjsEsmCodeAnalyzer;
-
-use crate::cache::NodeAnalysisCache;
-
-pub struct CliCjsEsmCodeAnalyzer {
- cache: NodeAnalysisCache,
-}
-
-impl CliCjsEsmCodeAnalyzer {
- pub fn new(cache: NodeAnalysisCache) -> Self {
- Self { cache }
- }
-
- fn inner_cjs_analysis(
- &self,
- specifier: &ModuleSpecifier,
- source: &str,
- ) -> Result<CjsAnalysis, AnyError> {
- let source_hash = NodeAnalysisCache::compute_source_hash(source);
- if let Some(analysis) = self
- .cache
- .get_cjs_analysis(specifier.as_str(), &source_hash)
- {
- return Ok(analysis);
- }
-
- let media_type = MediaType::from_specifier(specifier);
- if media_type == MediaType::Json {
- return Ok(CjsAnalysis {
- exports: vec![],
- reexports: vec![],
- });
- }
-
- let parsed_source = deno_ast::parse_script(deno_ast::ParseParams {
- specifier: specifier.to_string(),
- text_info: deno_ast::SourceTextInfo::new(source.into()),
- media_type,
- capture_tokens: true,
- scope_analysis: false,
- maybe_syntax: None,
- })?;
- let analysis = parsed_source.analyze_cjs();
- self
- .cache
- .set_cjs_analysis(specifier.as_str(), &source_hash, &analysis);
-
- Ok(analysis)
- }
-}
-
-impl CjsEsmCodeAnalyzer for CliCjsEsmCodeAnalyzer {
- fn analyze_cjs(
- &self,
- specifier: &ModuleSpecifier,
- source: &str,
- ) -> Result<ExtNodeCjsAnalysis, AnyError> {
- let analysis = self.inner_cjs_analysis(specifier, source)?;
- Ok(ExtNodeCjsAnalysis {
- exports: analysis.exports,
- reexports: analysis.reexports,
- })
- }
-
- fn analyze_esm_top_level_decls(
- &self,
- specifier: &ModuleSpecifier,
- source: &str,
- ) -> Result<HashSet<String>, AnyError> {
- // TODO(dsherret): this code is way more inefficient than it needs to be.
- //
- // In the future, we should disable capturing tokens & scope analysis
- // and instead only use swc's APIs to go through the portions of the tree
- // that we know will affect the global scope while still ensuring that
- // `var` decls are taken into consideration.
- let source_hash = NodeAnalysisCache::compute_source_hash(source);
- if let Some(decls) = self
- .cache
- .get_esm_analysis(specifier.as_str(), &source_hash)
- {
- Ok(HashSet::from_iter(decls))
- } else {
- let parsed_source = deno_ast::parse_program(deno_ast::ParseParams {
- specifier: specifier.to_string(),
- text_info: deno_ast::SourceTextInfo::from_string(source.to_string()),
- media_type: deno_ast::MediaType::from_specifier(specifier),
- capture_tokens: true,
- scope_analysis: true,
- maybe_syntax: None,
- })?;
- let top_level_decls = analyze_top_level_decls(&parsed_source)?;
- self.cache.set_esm_analysis(
- specifier.as_str(),
- &source_hash,
- &top_level_decls.clone().into_iter().collect::<Vec<_>>(),
- );
- Ok(top_level_decls)
- }
- }
-}
-
-fn analyze_top_level_decls(
- parsed_source: &ParsedSource,
-) -> Result<HashSet<String>, AnyError> {
- fn visit_children(
- node: Node,
- top_level_context: SyntaxContext,
- results: &mut HashSet<String>,
- ) {
- if let Node::Ident(ident) = node {
- if ident.ctxt() == top_level_context && is_local_declaration_ident(node) {
- results.insert(ident.sym().to_string());
- }
- }
-
- for child in node.children() {
- visit_children(child, top_level_context, results);
- }
- }
-
- let top_level_context = parsed_source.top_level_context();
-
- parsed_source.with_view(|program| {
- let mut results = HashSet::new();
- visit_children(program.into(), top_level_context, &mut results);
- Ok(results)
- })
-}
-
-fn is_local_declaration_ident(node: Node) -> bool {
- if let Some(parent) = node.parent() {
- match parent {
- Node::BindingIdent(decl) => decl.id.range().contains(&node.range()),
- Node::ClassDecl(decl) => decl.ident.range().contains(&node.range()),
- Node::ClassExpr(decl) => decl
- .ident
- .as_ref()
- .map(|i| i.range().contains(&node.range()))
- .unwrap_or(false),
- Node::TsInterfaceDecl(decl) => decl.id.range().contains(&node.range()),
- Node::FnDecl(decl) => decl.ident.range().contains(&node.range()),
- Node::FnExpr(decl) => decl
- .ident
- .as_ref()
- .map(|i| i.range().contains(&node.range()))
- .unwrap_or(false),
- Node::TsModuleDecl(decl) => decl.id.range().contains(&node.range()),
- Node::TsNamespaceDecl(decl) => decl.id.range().contains(&node.range()),
- Node::VarDeclarator(decl) => decl.name.range().contains(&node.range()),
- Node::ImportNamedSpecifier(decl) => {
- decl.local.range().contains(&node.range())
- }
- Node::ImportDefaultSpecifier(decl) => {
- decl.local.range().contains(&node.range())
- }
- Node::ImportStarAsSpecifier(decl) => decl.range().contains(&node.range()),
- Node::KeyValuePatProp(decl) => decl.key.range().contains(&node.range()),
- Node::AssignPatProp(decl) => decl.key.range().contains(&node.range()),
- _ => false,
- }
- } else {
- false
- }
-}