Skip to content

Commit

Permalink
refactor: use deno_ast's setup of swc scope analysis (#909)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Oct 11, 2021
1 parent dc09c9e commit ebe630d
Show file tree
Hide file tree
Showing 21 changed files with 125 additions and 156 deletions.
107 changes: 64 additions & 43 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 @@ -21,7 +21,7 @@ default = []
docs = []

[dependencies]
deno_ast = { version = "0.2.0", features = ["transforms", "utils", "visit", "view"] }
deno_ast = { version = "0.3.0", features = ["transforms", "utils", "visit", "view"] }
log = "0.4.14"
serde = { version = "1.0.125", features = ["derive"] }
serde_json = "1.0.64"
Expand Down
73 changes: 14 additions & 59 deletions src/ast_parser.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// Copyright 2020-2021 the Deno authors. All rights reserved. MIT license.
use deno_ast::swc::common::Globals;
use deno_ast::swc::common::Mark;
use deno_ast::swc::parser::Syntax;
use deno_ast::swc::transforms::resolver::ts_resolver;
use deno_ast::swc::visit::FoldWith;
use deno_ast::MediaType;
use deno_ast::ParsedSource;
use std::error::Error;
Expand Down Expand Up @@ -39,59 +35,18 @@ impl SwcDiagnostic {
}
}

/// Low-level utility structure with common AST parsing functions.
///
/// Allows to build more complicated parser by providing a callback
/// to `parse_module`.
pub(crate) struct AstParser {
pub(crate) globals: Globals,
/// The marker passed to the resolver (from swc).
///
/// This mark is applied to top level bindings and unresolved references.
pub(crate) top_level_mark: Mark,
}

impl AstParser {
pub(crate) fn new() -> Self {
let globals = Globals::new();
let top_level_mark = deno_ast::swc::common::GLOBALS
.set(&globals, || Mark::fresh(Mark::root()));

AstParser {
globals,
top_level_mark,
}
}

pub(crate) fn parse_program(
&self,
file_name: &str,
syntax: Syntax,
source_code: String,
) -> Result<ParsedSource, SwcDiagnostic> {
deno_ast::parse_program_with_post_process(
deno_ast::ParseParams {
specifier: file_name.to_string(),
media_type: MediaType::Unknown,
source: deno_ast::SourceTextInfo::from_string(source_code),
capture_tokens: true,
maybe_syntax: Some(syntax),
},
|program| {
// This is used to apply proper "syntax context" to all AST elements. When SWC performs
// transforms/folding it might change some of those context and "ts_resolver" ensures
// that all elements end up in proper lexical scope.
deno_ast::swc::common::GLOBALS.set(&self.globals, || {
program.fold_with(&mut ts_resolver(self.top_level_mark))
})
},
)
.map_err(|diagnostic| SwcDiagnostic::from_diagnostic(&diagnostic))
}
}

impl Default for AstParser {
fn default() -> Self {
Self::new()
}
pub(crate) fn parse_program(
file_name: &str,
syntax: Syntax,
source_code: String,
) -> Result<ParsedSource, SwcDiagnostic> {
deno_ast::parse_program(deno_ast::ParseParams {
specifier: file_name.to_string(),
media_type: MediaType::Unknown,
source: deno_ast::SourceTextInfo::from_string(source_code),
capture_tokens: true,
maybe_syntax: Some(syntax),
scope_analysis: true,
})
.map_err(|diagnostic| SwcDiagnostic::from_diagnostic(&diagnostic))
}
17 changes: 7 additions & 10 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use crate::ignore_directives::{
use crate::rules::{get_all_rules, LintRule};
use crate::scopes::Scope;
use deno_ast::swc::common::comments::Comment;
use deno_ast::swc::common::BytePos;
use deno_ast::swc::common::{Span, SyntaxContext};
use deno_ast::view as ast_view;
use deno_ast::view::{BytePos, RootNode, SourceFile};
use deno_ast::view::{RootNode, SourceFile};
use deno_ast::MediaType;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
Expand Down Expand Up @@ -146,11 +147,7 @@ impl<'view> Context<'view> {
}

pub fn all_comments(&self) -> impl Iterator<Item = &'view Comment> {
self
.program
.comments()
.expect("Program should have information about comments, but doesn't")
.all_comments()
self.program.comment_container().unwrap().all_comments()
}

pub fn leading_comments_at(
Expand All @@ -159,8 +156,8 @@ impl<'view> Context<'view> {
) -> impl Iterator<Item = &'view Comment> {
self
.program
.comments()
.expect("Program should have information about comments, but doesn't")
.comment_container()
.unwrap()
.leading_comments(lo)
}

Expand All @@ -170,8 +167,8 @@ impl<'view> Context<'view> {
) -> impl Iterator<Item = &'view Comment> {
self
.program
.comments()
.expect("Program should have information about comments, but doesn't")
.comment_container()
.unwrap()
.trailing_comments(hi)
}

Expand Down
Loading

0 comments on commit ebe630d

Please sign in to comment.