mirror of
https://github.com/swc-project/swc.git
synced 2024-12-18 11:11:30 +03:00
987213797f
swc_ecma_lints: - Add types for general configuration. - Add `no-console`rule. swc: - Expose the lint config via `jsc.lints`.
64 lines
1.8 KiB
Rust
64 lines
1.8 KiB
Rust
use std::path::PathBuf;
|
|
|
|
use swc_common::{input::SourceFileInput, Mark, SyntaxContext};
|
|
use swc_ecma_ast::EsVersion;
|
|
use swc_ecma_lints::{config::LintConfig, rules::all};
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Syntax};
|
|
use swc_ecma_transforms_base::resolver::resolver_with_mark;
|
|
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();
|
|
|
|
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()
|
|
})
|
|
},
|
|
EsVersion::latest(),
|
|
SourceFileInput::from(&*fm),
|
|
None,
|
|
);
|
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let mut m = parser.parse_module().unwrap();
|
|
let top_level_mark = Mark::fresh(Mark::root());
|
|
|
|
m.visit_mut_with(&mut resolver_with_mark(top_level_mark));
|
|
|
|
let top_level_ctxt = SyntaxContext::empty().apply_mark(top_level_mark);
|
|
|
|
let mut config = LintConfig::default();
|
|
|
|
let rules = all(&config, top_level_ctxt);
|
|
|
|
HANDLER.set(handler, || {
|
|
for mut rule in rules {
|
|
rule.lint_module(&m);
|
|
}
|
|
});
|
|
|
|
if handler.has_errors() {
|
|
return Err(());
|
|
}
|
|
|
|
Ok(())
|
|
})
|
|
.unwrap();
|
|
}
|