Skip to content

Commit

Permalink
feat(linter): support read env from eslintrc
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jan 22, 2024
1 parent 27aaff2 commit 403d328
Show file tree
Hide file tree
Showing 18 changed files with 2,295 additions and 94 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"browser": true
},
"rules": {
"no-undef": "error"
}
}
5 changes: 5 additions & 0 deletions crates/oxc_cli/fixtures/eslintrc_env/eslintrc_no_env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-undef": "error"
}
}
1 change: 1 addition & 0 deletions crates/oxc_cli/fixtures/eslintrc_env/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('')
23 changes: 23 additions & 0 deletions crates/oxc_cli/src/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,29 @@ mod test {
assert_eq!(result.number_of_errors, 0);
}

#[test]
fn eslintrc_no_env() {
let args =
&["-c", "fixtures/eslintrc_env/eslintrc_no_env.json", "fixtures/eslintrc_env/test.js"];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 1);
assert_eq!(result.number_of_errors, 0);
}

#[test]
fn eslintrc_with_env() {
let args = &[
"-c",
"fixtures/eslintrc_env/eslintrc_env_browser.json",
"fixtures/eslintrc_env/test.js",
];
let result = test(args);
assert_eq!(result.number_of_files, 1);
assert_eq!(result.number_of_warnings, 0);
assert_eq!(result.number_of_errors, 0);
}

#[test]
fn no_empty_allow_empty_catch() {
let args = &[
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@ json-strip-comments = "1.0.1"
miette = { workspace = true }
insta = { workspace = true }

[build-dependencies]
phf = { workspace = true, features = ["macros"] }
phf_codegen = "0.11"
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }

[package.metadata.cargo-machete]
ignored = ["once_cell"]
150 changes: 150 additions & 0 deletions crates/oxc_linter/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//! [Defines environment settings and globals.](https://github.com/eslint/eslintrc/blob/main/conf/environments.js)

use std::collections::HashMap;
use std::env;
use std::fs::File;
use std::io::{BufWriter, Write};
use std::path::Path;

fn main() {
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("environments.rs");

let new_globals: HashMap<&str, HashMap<String, bool>> = HashMap::from([
(
"new_globals_2015",
HashMap::from([
(String::from("ArrayBuffer"), false),
(String::from("DataView"), false),
(String::from("Float32Array"), false),
(String::from("Float64Array"), false),
(String::from("Int16Array"), false),
(String::from("Int32Array"), false),
(String::from("Int8Array"), false),
(String::from("Map"), false),
(String::from("Promise"), false),
(String::from("Proxy"), false),
(String::from("Reflect"), false),
(String::from("Set"), false),
(String::from("Symbol"), false),
(String::from("Uint16Array"), false),
(String::from("Uint32Array"), false),
(String::from("Uint8Array"), false),
(String::from("Uint8ClampedArray"), false),
(String::from("WeakMap"), false),
(String::from("WeakSet"), false),
]),
),
(
"new_globals_2017",
HashMap::from([
(String::from("Atomics"), false),
(String::from("SharedArrayBuffer"), false),
]),
),
(
"new_globals_2020",
HashMap::from([
(String::from("BigInt"), false),
(String::from("BigInt64Array"), false),
(String::from("BigUint64Array"), false),
(String::from("globalThis"), false),
]),
),
(
"new_globals_2021",
HashMap::from([
(String::from("AggregateError"), false),
(String::from("FinalizationRegistry"), false),
(String::from("WeakRef"), false),
]),
),
]);

let pre_define_var = HashMap::from([
(String::from("undefined"), false),
(String::from("Infinity"), false),
(String::from("NaN"), false),
(String::from("eval"), false),
(String::from("arguments"), false),
]);

// globals.json is copied from [sindresorhus/globals](https://github.com/sindresorhus/globals/blob/870383888c52f48d11d02975f107468f9401024e/globals.json)
// Each global is given a value of true or false.
// A value of true indicates that the variable may be overwritten.
// A value of false indicates that the variable should be considered read-only.
let globals_file = File::open("./globals.json").unwrap();
let globals: HashMap<String, HashMap<String, bool>> =
serde_json::from_reader(globals_file).unwrap();

let mut es2015_2017 = HashMap::new();
es2015_2017.extend(new_globals["new_globals_2015"].clone());
es2015_2017.extend(new_globals["new_globals_2017"].clone());

let mut es2015_2017_2020 = es2015_2017.clone();
es2015_2017_2020.extend(new_globals["new_globals_2020"].clone());

let mut es2015_2017_2020_2021 = es2015_2017_2020.clone();
es2015_2017_2020_2021.extend(new_globals["new_globals_2021"].clone());

let envs_preset: HashMap<&str, &HashMap<String, bool>> = HashMap::from([
// Language
("builtin", &globals["builtin"]),
("es6", &new_globals["new_globals_2015"]),
("es2015", &new_globals["new_globals_2015"]),
("es2016", &new_globals["new_globals_2015"]),
("es2017", &es2015_2017),
("es2018", &es2015_2017),
("es2019", &es2015_2017),
("es2020", &es2015_2017_2020),
("es2021", &es2015_2017_2020_2021),
("es2022", &es2015_2017_2020_2021),
("es2023", &es2015_2017_2020_2021),
("es2024", &es2015_2017_2020_2021),
// Platforms
("browser", &globals["browser"]),
("node", &globals["node"]),
("shared-node-browser", &globals["shared-node-browser"]),
("worker", &globals["worker"]),
("serviceworker", &globals["serviceworker"]),
// Frameworks
("commonjs", &globals["commonjs"]),
("amd", &globals["amd"]),
("mocha", &globals["mocha"]),
("jasmine", &globals["jasmine"]),
("jest", &globals["jest"]),
("phantomjs", &globals["phantomjs"]),
("jquery", &globals["jquery"]),
("qunit", &globals["qunit"]),
("prototypejs", &globals["prototypejs"]),
("shelljs", &globals["shelljs"]),
("meteor", &globals["meteor"]),
("mongo", &globals["mongo"]),
("protractor", &globals["protractor"]),
("applescript", &globals["applescript"]),
("nashorn", &globals["nashorn"]),
("atomtest", &globals["atomtest"]),
("embertest", &globals["embertest"]),
("webextensions", &globals["webextensions"]),
("greasemonkey", &globals["greasemonkey"]),
// oxc
("pre_define_var", &pre_define_var),
]);

let mut envs = phf_codegen::Map::new();

for (env_name, preset) in envs_preset {
let mut env = phf_codegen::Map::new();
for (feature, value) in preset {
env.entry(feature, &value.to_string());
}

envs.entry(env_name, &env.build().to_string());
}

writeln!(
BufWriter::new(File::create(path).unwrap()),
"#[allow(unused)]\npub static ENVIRONMENTS: phf::Map<&'static str, phf::Map<&'static str, bool>> = {};",
envs.build()
)
.unwrap();
}
Loading

0 comments on commit 403d328

Please sign in to comment.