2022-01-05 05:59:10 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-01-26 14:39:19 +03:00
|
|
|
use swc_common::{input::SourceFileInput, Mark, SyntaxContext};
|
2022-02-02 17:43:46 +03:00
|
|
|
use swc_ecma_ast::{EsVersion, Program};
|
|
|
|
use swc_ecma_lints::{
|
|
|
|
config::LintConfig,
|
|
|
|
rules::{all, LintParams},
|
|
|
|
};
|
2022-01-05 05:59:10 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
2022-03-05 20:29:21 +03:00
|
|
|
use swc_ecma_transforms_base::resolver::{resolver_with_mark, ts_resolver};
|
2022-01-05 05:59:10 +03:00
|
|
|
use swc_ecma_utils::HANDLER;
|
|
|
|
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();
|
2022-02-02 17:43:46 +03:00
|
|
|
let es_version = EsVersion::latest();
|
2022-01-05 05:59:10 +03:00
|
|
|
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
},
|
2022-02-02 17:43:46 +03:00
|
|
|
es_version,
|
2022-01-05 05:59:10 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
|
|
|
None,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
|
2022-03-07 08:49:39 +03:00
|
|
|
let mut program = parser.parse_program().unwrap();
|
2022-01-26 14:39:19 +03:00
|
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
2022-01-05 05:59:10 +03:00
|
|
|
|
2022-03-05 20:29:21 +03:00
|
|
|
if input.extension().unwrap() == "ts" || input.extension().unwrap() == "tsx" {
|
2022-03-07 08:49:39 +03:00
|
|
|
program.visit_mut_with(&mut ts_resolver(top_level_mark));
|
2022-03-05 20:29:21 +03:00
|
|
|
} else {
|
2022-03-07 08:49:39 +03:00
|
|
|
program.visit_mut_with(&mut resolver_with_mark(top_level_mark));
|
2022-03-05 20:29:21 +03:00
|
|
|
}
|
2022-01-26 14:39:19 +03:00
|
|
|
|
|
|
|
let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark);
|
|
|
|
|
2022-02-02 17:43:46 +03:00
|
|
|
let config = LintConfig::default();
|
2022-01-26 14:39:19 +03:00
|
|
|
|
2022-02-02 17:43:46 +03:00
|
|
|
let rules = all(LintParams {
|
|
|
|
program: &program,
|
|
|
|
lint_config: &config,
|
|
|
|
top_level_ctxt,
|
|
|
|
es_version,
|
2022-02-07 11:12:14 +03:00
|
|
|
source_map: cm,
|
2022-02-02 17:43:46 +03:00
|
|
|
});
|
2022-01-05 05:59:10 +03:00
|
|
|
|
2022-01-13 17:06:11 +03:00
|
|
|
HANDLER.set(handler, || {
|
2022-02-02 17:43:46 +03:00
|
|
|
if let Program::Module(m) = &program {
|
|
|
|
for mut rule in rules {
|
2022-02-07 11:12:14 +03:00
|
|
|
rule.lint_module(m);
|
2022-02-02 17:43:46 +03:00
|
|
|
}
|
2022-01-05 05:59:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if handler.has_errors() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|