Skip to content

Commit

Permalink
feat(es): Add options to disable all esnext transforms and lints (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Oct 1, 2024
1 parent b28047a commit f2b0766
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
7 changes: 7 additions & 0 deletions .changeset/spicy-hairs-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
swc: patch
swc_core: patch
swc_ecma_lints: patch
---

feat(es): Add options to disable all `esnext` transforms and lints
2 changes: 1 addition & 1 deletion crates/swc/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,6 @@ impl VisitMut for MinifierPass<'_> {
}
}

fn should_enable(target: EsVersion, feature: EsVersion) -> bool {
pub(crate) fn should_enable(target: EsVersion, feature: EsVersion) -> bool {
target < feature
}
40 changes: 28 additions & 12 deletions crates/swc/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ use swc_ecma_visit::{Fold, VisitMutWith};

pub use crate::plugin::PluginConfig;
use crate::{
builder::PassBuilder, dropped_comments_preserver::dropped_comments_preserver, SwcImportResolver,
builder::{should_enable, PassBuilder},
dropped_comments_preserver::dropped_comments_preserver,
SwcImportResolver,
};

#[cfg(test)]
Expand Down Expand Up @@ -579,6 +581,7 @@ impl Options {
);

let keep_import_attributes = experimental.keep_import_attributes.into_bool();
let disable_all_lints = experimental.disable_all_lints.into_bool();

#[cfg(feature = "plugin")]
let plugin_transforms = {
Expand Down Expand Up @@ -697,23 +700,33 @@ impl Options {
};

Box::new(chain!(
lint_to_fold(swc_ecma_lints::rules::all(LintParams {
program: &program,
lint_config: &lints,
top_level_ctxt,
unresolved_ctxt,
es_version,
source_map: cm.clone(),
})),
Optional::new(
lint_to_fold(swc_ecma_lints::rules::all(LintParams {
program: &program,
lint_config: &lints,
top_level_ctxt,
unresolved_ctxt,
es_version,
source_map: cm.clone(),
})),
!disable_all_lints
),
// Decorators may use type information
Optional::new(decorator_pass, syntax.decorators()),
Optional::new(
decorator_pass,
should_enable(es_version, EsVersion::EsNext) && syntax.decorators()
),
Optional::new(
explicit_resource_management(),
syntax.explicit_resource_management()
should_enable(es_version, EsVersion::EsNext)
&& syntax.explicit_resource_management()
),
// The transform strips import assertions, so it's only enabled if
// keep_import_assertions is false.
Optional::new(import_assertions(), !keep_import_attributes),
Optional::new(
import_assertions(),
should_enable(es_version, EsVersion::EsNext) && !keep_import_attributes
),
Optional::new(
typescript::tsx::<Option<&dyn Comments>>(
cm.clone(),
Expand Down Expand Up @@ -1229,6 +1242,9 @@ pub struct JscExperimental {
/// This requires `isolatedDeclartion` feature of TypeScript 5.5.
#[serde(default)]
pub emit_isolated_dts: BoolConfig<false>,

#[serde(default)]
pub disable_all_lints: BoolConfig<false>,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_lints/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ pub trait Rule: Debug + Send + Sync {

macro_rules! for_vec {
($name:ident, $program:ident, $s:expr) => {{
if $s.is_empty() {
return;
}

let program = $program;
if cfg!(target_arch = "wasm32") {
for rule in $s {
Expand Down

0 comments on commit f2b0766

Please sign in to comment.