2022-01-05 05:59:10 +03:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2022-05-11 07:36:22 +03:00
|
|
|
use swc_common::{errors::HANDLER, input::SourceFileInput, Mark, SyntaxContext};
|
2022-02-02 17:43:46 +03:00
|
|
|
use swc_ecma_ast::{EsVersion, Program};
|
|
|
|
use swc_ecma_lints::{
|
|
|
|
config::LintConfig,
|
2022-03-12 06:02:22 +03:00
|
|
|
rule::Rule,
|
2022-02-02 17:43:46 +03:00
|
|
|
rules::{all, LintParams},
|
|
|
|
};
|
2022-01-05 05:59:10 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
2022-04-26 10:38:50 +03:00
|
|
|
use swc_ecma_transforms_base::resolver;
|
2022-01-05 05:59:10 +03:00
|
|
|
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" {
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Typescript(Default::default())
|
2022-01-05 05:59:10 +03:00
|
|
|
} else if input.extension().unwrap() == "tsx" {
|
2024-06-21 19:56:37 +03:00
|
|
|
Syntax::Typescript(swc_ecma_parser::TsSyntax {
|
2022-01-05 05:59:10 +03:00
|
|
|
tsx: true,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
} else {
|
2023-03-30 11:06:02 +03:00
|
|
|
Syntax::Es(Default::default())
|
2022-01-05 05:59:10 +03:00
|
|
|
},
|
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-04-26 10:38:50 +03:00
|
|
|
let unresolved_mark = Mark::new();
|
|
|
|
let top_level_mark = Mark::new();
|
2022-01-05 05:59:10 +03:00
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let need_ts = input.extension().unwrap() == "ts" || input.extension().unwrap() == "tsx";
|
|
|
|
|
|
|
|
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, need_ts));
|
2022-01-26 14:39:19 +03:00
|
|
|
|
2022-04-26 10:38:50 +03:00
|
|
|
let unresolved_ctxt = SyntaxContext::empty().apply_mark(unresolved_mark);
|
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-03-12 06:02:22 +03:00
|
|
|
let mut rules = all(LintParams {
|
2022-02-02 17:43:46 +03:00
|
|
|
program: &program,
|
|
|
|
lint_config: &config,
|
2022-04-26 10:38:50 +03:00
|
|
|
unresolved_ctxt,
|
2022-02-02 17:43:46 +03:00
|
|
|
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-03-12 06:02:22 +03:00
|
|
|
HANDLER.set(handler, || match &program {
|
|
|
|
Program::Module(m) => {
|
|
|
|
rules.lint_module(m);
|
|
|
|
}
|
|
|
|
Program::Script(s) => {
|
|
|
|
rules.lint_script(s);
|
2022-01-05 05:59:10 +03:00
|
|
|
}
|
2022-07-21 08:34:48 +03:00
|
|
|
// TODO: reenable once experimental_metadata breaking change is merged
|
|
|
|
//_ => unreachable!(),
|
2022-01-05 05:59:10 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if handler.has_errors() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|