Skip to content

Commit f2b0766

Browse files
authored
feat(es): Add options to disable all esnext transforms and lints (#9597)
1 parent b28047a commit f2b0766

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

.changeset/spicy-hairs-run.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
swc: patch
3+
swc_core: patch
4+
swc_ecma_lints: patch
5+
---
6+
7+
feat(es): Add options to disable all `esnext` transforms and lints

crates/swc/src/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,6 @@ impl VisitMut for MinifierPass<'_> {
506506
}
507507
}
508508

509-
fn should_enable(target: EsVersion, feature: EsVersion) -> bool {
509+
pub(crate) fn should_enable(target: EsVersion, feature: EsVersion) -> bool {
510510
target < feature
511511
}

crates/swc/src/config/mod.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ use swc_ecma_visit::{Fold, VisitMutWith};
7474

7575
pub use crate::plugin::PluginConfig;
7676
use crate::{
77-
builder::PassBuilder, dropped_comments_preserver::dropped_comments_preserver, SwcImportResolver,
77+
builder::{should_enable, PassBuilder},
78+
dropped_comments_preserver::dropped_comments_preserver,
79+
SwcImportResolver,
7880
};
7981

8082
#[cfg(test)]
@@ -579,6 +581,7 @@ impl Options {
579581
);
580582

581583
let keep_import_attributes = experimental.keep_import_attributes.into_bool();
584+
let disable_all_lints = experimental.disable_all_lints.into_bool();
582585

583586
#[cfg(feature = "plugin")]
584587
let plugin_transforms = {
@@ -697,23 +700,33 @@ impl Options {
697700
};
698701

699702
Box::new(chain!(
700-
lint_to_fold(swc_ecma_lints::rules::all(LintParams {
701-
program: &program,
702-
lint_config: &lints,
703-
top_level_ctxt,
704-
unresolved_ctxt,
705-
es_version,
706-
source_map: cm.clone(),
707-
})),
703+
Optional::new(
704+
lint_to_fold(swc_ecma_lints::rules::all(LintParams {
705+
program: &program,
706+
lint_config: &lints,
707+
top_level_ctxt,
708+
unresolved_ctxt,
709+
es_version,
710+
source_map: cm.clone(),
711+
})),
712+
!disable_all_lints
713+
),
708714
// Decorators may use type information
709-
Optional::new(decorator_pass, syntax.decorators()),
715+
Optional::new(
716+
decorator_pass,
717+
should_enable(es_version, EsVersion::EsNext) && syntax.decorators()
718+
),
710719
Optional::new(
711720
explicit_resource_management(),
712-
syntax.explicit_resource_management()
721+
should_enable(es_version, EsVersion::EsNext)
722+
&& syntax.explicit_resource_management()
713723
),
714724
// The transform strips import assertions, so it's only enabled if
715725
// keep_import_assertions is false.
716-
Optional::new(import_assertions(), !keep_import_attributes),
726+
Optional::new(
727+
import_assertions(),
728+
should_enable(es_version, EsVersion::EsNext) && !keep_import_attributes
729+
),
717730
Optional::new(
718731
typescript::tsx::<Option<&dyn Comments>>(
719732
cm.clone(),
@@ -1229,6 +1242,9 @@ pub struct JscExperimental {
12291242
/// This requires `isolatedDeclartion` feature of TypeScript 5.5.
12301243
#[serde(default)]
12311244
pub emit_isolated_dts: BoolConfig<false>,
1245+
1246+
#[serde(default)]
1247+
pub disable_all_lints: BoolConfig<false>,
12321248
}
12331249

12341250
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]

crates/swc_ecma_lints/src/rule.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ pub trait Rule: Debug + Send + Sync {
2020

2121
macro_rules! for_vec {
2222
($name:ident, $program:ident, $s:expr) => {{
23+
if $s.is_empty() {
24+
return;
25+
}
26+
2327
let program = $program;
2428
if cfg!(target_arch = "wasm32") {
2529
for rule in $s {

0 commit comments

Comments
 (0)