swc/crates/swc_ecma_lints/tests/fixture.rs
Donny/강동윤 6a97c7bef8
fix(es/utils): Fix wrong apis (#4604)
# API Changes

## `swc_ecma_utils`

### Renames

 - `find_ids` => `find_pat_ids`
 - `prepend` => `prepend_stmt`
 - `as_bool` => `cast_to_bool`
 - `as_number` => `as_pure_number`
 - `as_string` => `cast_to_string`
 - (`as_pure_bool`, `cast_to_number` is not renamed)
 - `UsageFinder` => `IdentUsageFinder`.
 - `IdentFinder` => `IdentRefFinder`.

## Changes

 - Re-export of `HANDLER` from `swc_common::errors` is removed. Use `swc_common::error::HANDLER` instead.
 - Re-export of `Id` from `swc_ecma_ast` is removed. Use `swc_ecma_ast::Id` instead.
 - `ExprCtx` is used for apis related to `Expr`.
 - `preserve_effects` is now a method of `ExprCtx`. It was a top-level function.
 - `extract_side_effects_to` is now a method of `ExprCtx`. It was a top-level function.
 - `IdentUsageFinder` (previously `UsageFinder`) now takes `Id` instead of `Ident`.
 - `IdentRefFinder` (previously `IdentFinder`) now takes `Id` instead of `Ident`.
 - `ExprExt::is_ident_ref_to` is removed.
 - `Expr::is_global_ref_to` is added.
 - `:🆔:id` is removed. Use `Ident::to_id()` instead.
2022-05-11 04:36:22 +00:00

82 lines
2.4 KiB
Rust

use std::path::PathBuf;
use swc_common::{errors::HANDLER, input::SourceFileInput, Mark, SyntaxContext};
use swc_ecma_ast::{EsVersion, Program};
use swc_ecma_lints::{
config::LintConfig,
rule::Rule,
rules::{all, LintParams},
};
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
use swc_ecma_transforms_base::resolver;
use swc_ecma_visit::VisitMutWith;
#[testing::fixture("tests/pass/**/input.js")]
#[testing::fixture("tests/pass/**/input.ts")]
fn pass(input: PathBuf) {
testing::run_test(false, |cm, handler| {
let fm = cm.load_file(&input).unwrap();
let es_version = EsVersion::latest();
let lexer = Lexer::new(
if input.extension().unwrap() == "ts" {
Syntax::Typescript(swc_ecma_parser::TsConfig {
..Default::default()
})
} else if input.extension().unwrap() == "tsx" {
Syntax::Typescript(swc_ecma_parser::TsConfig {
tsx: true,
..Default::default()
})
} else {
Syntax::Es(swc_ecma_parser::EsConfig {
..Default::default()
})
},
es_version,
SourceFileInput::from(&*fm),
None,
);
let mut parser = Parser::new_from(lexer);
let mut program = parser.parse_program().unwrap();
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
let need_ts = input.extension().unwrap() == "ts" || input.extension().unwrap() == "tsx";
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, need_ts));
let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark);
let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark);
let config = LintConfig::default();
let mut rules = all(LintParams {
program: &program,
lint_config: &config,
unresolved_ctxt,
top_level_ctxt,
es_version,
source_map: cm,
});
HANDLER.set(handler, || match &program {
Program::Module(m) => {
rules.lint_module(m);
}
Program::Script(s) => {
rules.lint_script(s);
}
});
if handler.has_errors() {
return Err(());
}
Ok(())
})
.unwrap();
}