swc/crates/swc_ecma_lints/tests/fixture.rs
Donny/강동윤 5520b236dd
refactor(es/parser): Rename EsConfig and TsConfig (#9094)
**Description:**

`EsConfig` and `TsConfig` sound like a general configuration for the whole language, while actually it's only about parsing.

To avoid a breaking change, I created type aliases that will work without changing the code, while warning the users.

**Related issue:**

 - Closes #9089.
2024-06-22 01:56:37 +09:00

80 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(Default::default())
} else if input.extension().unwrap() == "tsx" {
Syntax::Typescript(swc_ecma_parser::TsSyntax {
tsx: true,
..Default::default()
})
} else {
Syntax::Es(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);
}
// TODO: reenable once experimental_metadata breaking change is merged
//_ => unreachable!(),
});
if handler.has_errors() {
return Err(());
}
Ok(())
})
.unwrap();
}