Skip to content

Commit

Permalink
test: Support more directives in file analyzer testing system (#1069)
Browse files Browse the repository at this point in the history
**Description:**

When comparing TSC to error, we took the error results from ACTUAL.

In actual, the rules for each file were not applied properly,
As a result, tests like strictNullCheck didn't work correctly.

To fix this, we unified the env import code within the tests.
  • Loading branch information
sunrabbit123 authored Aug 17, 2023
1 parent a04f4b1 commit d5a01ed
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 31 deletions.
38 changes: 7 additions & 31 deletions crates/stc_ts_file_analyzer/tests/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rnode::{NodeIdGenerator, RNode, VisitWith};
use stc_testing::logger;
use stc_ts_ast_rnode::RModule;
use stc_ts_builtin_types::Lib;
use stc_ts_env::{Env, ModuleConfig, Rule};
use stc_ts_env::Env;
use stc_ts_errors::{debug::debugger::Debugger, ErrorKind};
use stc_ts_file_analyzer::{
analyzer::{Analyzer, NoopLoader},
Expand Down Expand Up @@ -41,8 +41,9 @@ struct StcError {
code: usize,
}

fn get_env() -> Env {
let mut libs = vec![];
fn get_env_from_path(path: &Path, mut libs: Vec<Lib>) -> Env {
let case = parse_conformance_test(path).unwrap().into_iter().next().unwrap();
libs.extend(&case.libs);
let ls = &[
"es2022.full",
"es2021.full",
Expand All @@ -59,15 +60,7 @@ fn get_env() -> Env {
libs.sort();
libs.dedup();

Env::simple(
Rule {
strict_function_types: true,
..Default::default()
},
EsVersion::latest(),
ModuleConfig::None,
&libs,
)
Env::simple(case.rule, case.target, case.module_config, &libs)
}

fn validate(input: &Path) -> Vec<StcError> {
Expand All @@ -78,7 +71,7 @@ fn validate(input: &Path) -> Vec<StcError> {

let fm = cm.load_file(input).unwrap();

let env = get_env();
let env = get_env_from_path(input, vec![]);

let generator = module_id::ModuleIdGenerator::default();
let path = Arc::new(FileName::Real(input.to_path_buf()));
Expand Down Expand Up @@ -240,8 +233,6 @@ fn run_test(file_name: PathBuf, want_error: bool, disable_logging: bool) -> Opti
let filename = file_name.display().to_string();
println!("{}", filename);

let case = parse_conformance_test(&file_name).unwrap().into_iter().next().unwrap();

let result = testing::Tester::new()
.print_errors(|cm, handler| -> Result<(), _> {
let _tracing = if disable_logging {
Expand All @@ -252,22 +243,7 @@ fn run_test(file_name: PathBuf, want_error: bool, disable_logging: bool) -> Opti

let handler = Arc::new(handler);
let fm = cm.load_file(&file_name).unwrap();
let mut libs = vec![];
let ls = &[
"es2020.full",
"es2019.full",
"es2018.full",
"es2017.full",
"es2016.full",
"es2015.full",
];
for s in ls {
libs.extend(Lib::load(s))
}
libs.sort();
libs.dedup();

let env = Env::simple(case.rule, case.target, case.module_config, &libs);
let env = get_env_from_path(&file_name, vec![]);
let stable_env = env.shared().clone();
let generator = module_id::ModuleIdGenerator::default();
let path = Arc::new(FileName::Real(file_name.clone()));
Expand Down
5 changes: 5 additions & 0 deletions crates/stc_ts_file_analyzer/tests/tsc/rule/strictNullCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// @strictNullChecks: true
// @declaration: true

declare let a : string;
a = "a" as string | undefined;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"file": "tests/tsc/rule/strictNullCheck.ts",
"line": 5,
"col": 1,
"code": 2322
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"file": "tests/tsc/types/intersection/withUnionConstraint/1.ts",
"line": 4,
"col": 9,
"code": 2322
}
]
1 change: 1 addition & 0 deletions crates/stc_ts_type_checker/tests/tsc-stats.rust-debug
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Stats {
required_error: 3502,
matched_error: 6533,
extra_error: 764,
panic: 73,
}

0 comments on commit d5a01ed

Please sign in to comment.