mirror of
https://github.com/swc-project/swc.git
synced 2024-12-24 06:05:02 +03:00
feat(es/lints): Remove source map accesses (#4147)
This commit is contained in:
parent
d4b6b53641
commit
7a06c5b3d8
@ -89,7 +89,6 @@ pub fn all(lint_params: LintParams) -> Vec<Box<dyn Rule>> {
|
||||
|
||||
rules.extend(prefer_regex_literals::prefer_regex_literals(
|
||||
program,
|
||||
&source_map,
|
||||
&lint_config.prefer_regex_literals,
|
||||
top_level_ctxt,
|
||||
es_version,
|
||||
@ -119,12 +118,7 @@ pub fn all(lint_params: LintParams) -> Vec<Box<dyn Rule>> {
|
||||
&lint_config.no_restricted_syntax,
|
||||
));
|
||||
|
||||
rules.extend(radix::radix(
|
||||
program,
|
||||
&source_map,
|
||||
top_level_ctxt,
|
||||
&lint_config.radix,
|
||||
));
|
||||
rules.extend(radix::radix(program, top_level_ctxt, &lint_config.radix));
|
||||
|
||||
rules.extend(no_bitwise::no_bitwise(&lint_config.no_bitwise));
|
||||
|
||||
|
@ -1,10 +1,5 @@
|
||||
use std::{
|
||||
fmt::{self, Debug},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swc_common::{collections::AHashSet, errors::HANDLER, SourceMap, Span, SyntaxContext};
|
||||
use swc_common::{collections::AHashSet, errors::HANDLER, Span, SyntaxContext};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_utils::{collect_decls_with_ctxt, ident::IdentLike};
|
||||
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
|
||||
@ -34,7 +29,6 @@ pub struct PreferRegexLiteralsConfig {
|
||||
|
||||
pub fn prefer_regex_literals(
|
||||
program: &Program,
|
||||
source_map: &Arc<SourceMap>,
|
||||
config: &RuleConfig<PreferRegexLiteralsConfig>,
|
||||
top_level_ctxt: SyntaxContext,
|
||||
es_version: EsVersion,
|
||||
@ -49,7 +43,6 @@ pub fn prefer_regex_literals(
|
||||
match rule_reaction {
|
||||
LintRuleReaction::Off => None,
|
||||
_ => Some(visitor_rule(PreferRegexLiterals::new(
|
||||
source_map.clone(),
|
||||
rule_reaction,
|
||||
disallow_redundant_wrapping,
|
||||
collect_decls_with_ctxt(program, top_level_ctxt),
|
||||
@ -59,10 +52,9 @@ pub fn prefer_regex_literals(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Debug)]
|
||||
struct PreferRegexLiterals {
|
||||
expected_reaction: LintRuleReaction,
|
||||
source_map: Arc<SourceMap>,
|
||||
disallow_redundant_wrapping: bool,
|
||||
top_level_ctxt: SyntaxContext,
|
||||
top_level_declared_vars: AHashSet<Id>,
|
||||
@ -72,27 +64,8 @@ struct PreferRegexLiterals {
|
||||
second_arg: Option<ArgValue>,
|
||||
}
|
||||
|
||||
impl Debug for PreferRegexLiterals {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("PreferRegexLiterals")
|
||||
.field("expected_reaction", &self.expected_reaction)
|
||||
.field(
|
||||
"disallow_redundant_wrapping",
|
||||
&self.disallow_redundant_wrapping,
|
||||
)
|
||||
.field("top_level_ctxt", &self.top_level_ctxt)
|
||||
.field("top_level_declared_vars", &self.top_level_declared_vars)
|
||||
.field("allow_global_this", &self.allow_global_this)
|
||||
.field("call_span", &self.call_span)
|
||||
.field("first_arg", &self.first_arg)
|
||||
.field("second_arg", &self.second_arg)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl PreferRegexLiterals {
|
||||
fn new(
|
||||
source_map: Arc<SourceMap>,
|
||||
expected_reaction: LintRuleReaction,
|
||||
disallow_redundant_wrapping: bool,
|
||||
top_level_declared_vars: AHashSet<Id>,
|
||||
@ -101,7 +74,6 @@ impl PreferRegexLiterals {
|
||||
) -> Self {
|
||||
Self {
|
||||
expected_reaction,
|
||||
source_map,
|
||||
disallow_redundant_wrapping,
|
||||
top_level_ctxt,
|
||||
top_level_declared_vars,
|
||||
@ -123,7 +95,6 @@ impl PreferRegexLiterals {
|
||||
|
||||
if let Some(ExprOrSpread { expr, .. }) = args.get(0) {
|
||||
self.first_arg = Some(extract_arg_val(
|
||||
&self.source_map,
|
||||
&self.top_level_ctxt,
|
||||
&self.top_level_declared_vars,
|
||||
expr.as_ref(),
|
||||
@ -133,7 +104,6 @@ impl PreferRegexLiterals {
|
||||
|
||||
if let Some(ExprOrSpread { expr, .. }) = args.get(1) {
|
||||
self.second_arg = Some(extract_arg_val(
|
||||
&self.source_map,
|
||||
&self.top_level_ctxt,
|
||||
&self.top_level_declared_vars,
|
||||
expr.as_ref(),
|
||||
|
@ -1,14 +1,9 @@
|
||||
use std::{
|
||||
fmt::{self, Debug},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swc_atoms::JsWord;
|
||||
use swc_common::{
|
||||
collections::AHashSet,
|
||||
errors::{DiagnosticBuilder, HANDLER},
|
||||
SourceMap, Span, SyntaxContext,
|
||||
Span, SyntaxContext,
|
||||
};
|
||||
use swc_ecma_ast::*;
|
||||
use swc_ecma_utils::{collect_decls_with_ctxt, ident::IdentLike};
|
||||
@ -51,14 +46,12 @@ pub struct RadixConfig {
|
||||
|
||||
pub fn radix(
|
||||
program: &Program,
|
||||
source_map: &Arc<SourceMap>,
|
||||
top_level_ctxt: SyntaxContext,
|
||||
config: &RuleConfig<RadixConfig>,
|
||||
) -> Option<Box<dyn Rule>> {
|
||||
match config.get_rule_reaction() {
|
||||
LintRuleReaction::Off => None,
|
||||
_ => Some(visitor_rule(Radix::new(
|
||||
source_map.clone(),
|
||||
collect_decls_with_ctxt(program, top_level_ctxt),
|
||||
top_level_ctxt,
|
||||
config,
|
||||
@ -66,12 +59,11 @@ pub fn radix(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
#[derive(Default, Debug)]
|
||||
struct Radix {
|
||||
expected_reaction: LintRuleReaction,
|
||||
top_level_ctxt: SyntaxContext,
|
||||
top_level_declared_vars: AHashSet<Id>,
|
||||
source_map: Arc<SourceMap>,
|
||||
|
||||
radix_mode: RadixMode,
|
||||
check_parens: bool,
|
||||
@ -79,31 +71,10 @@ struct Radix {
|
||||
classes_depth: usize,
|
||||
objects_depth: usize,
|
||||
arrow_fns_depth: usize,
|
||||
obj: Option<JsWord>,
|
||||
prop: Option<JsWord>,
|
||||
}
|
||||
|
||||
impl Debug for Radix {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("Radix")
|
||||
.field("expected_reaction", &self.expected_reaction)
|
||||
.field("top_level_ctxt", &self.top_level_ctxt)
|
||||
.field("top_level_declared_vars", &self.top_level_declared_vars)
|
||||
.field("radix_mode", &self.radix_mode)
|
||||
.field("check_parens", &self.check_parens)
|
||||
.field("classes_depth", &self.classes_depth)
|
||||
.field("classes_depth", &self.classes_depth)
|
||||
.field("objects_depth", &self.objects_depth)
|
||||
.field("arrow_fns_depth", &self.arrow_fns_depth)
|
||||
.field("obj", &self.obj)
|
||||
.field("prop", &self.prop)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Radix {
|
||||
fn new(
|
||||
source_map: Arc<SourceMap>,
|
||||
top_level_declared_vars: AHashSet<Id>,
|
||||
top_level_ctxt: SyntaxContext,
|
||||
config: &RuleConfig<RadixConfig>,
|
||||
@ -114,7 +85,6 @@ impl Radix {
|
||||
expected_reaction: config.get_rule_reaction(),
|
||||
top_level_ctxt,
|
||||
top_level_declared_vars,
|
||||
source_map,
|
||||
|
||||
radix_mode: rule_config.mode.unwrap_or_default(),
|
||||
check_parens: rule_config.check_parens.unwrap_or(true),
|
||||
@ -122,8 +92,6 @@ impl Radix {
|
||||
classes_depth: 0,
|
||||
objects_depth: 0,
|
||||
arrow_fns_depth: 0,
|
||||
obj: None,
|
||||
prop: None,
|
||||
}
|
||||
}
|
||||
|
||||
@ -185,7 +153,6 @@ impl Radix {
|
||||
match call_expr.args.get(1) {
|
||||
Some(ExprOrSpread { expr, .. }) => {
|
||||
match &extract_arg_val(
|
||||
&self.source_map,
|
||||
&self.top_level_ctxt,
|
||||
&self.top_level_declared_vars,
|
||||
expr.as_ref(),
|
||||
|
@ -1,8 +1,6 @@
|
||||
use std::{fmt::Debug, sync::Arc};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use swc_atoms::JsWord;
|
||||
use swc_common::{collections::AHashSet, SourceMap, Span, SyntaxContext};
|
||||
use swc_common::{collections::AHashSet, SyntaxContext};
|
||||
use swc_ecma_ast::{
|
||||
Expr, Id, Lit, MemberExpr, MemberProp, Number, ParenExpr, Regex, SeqExpr, Str, TaggedTpl, Tpl,
|
||||
};
|
||||
@ -55,26 +53,16 @@ pub enum ArgValue {
|
||||
}
|
||||
|
||||
pub fn extract_arg_val(
|
||||
source_map: &Arc<SourceMap>,
|
||||
top_level_ctxt: &SyntaxContext,
|
||||
top_level_declared_vars: &AHashSet<Id>,
|
||||
expr: &Expr,
|
||||
check_parens: bool,
|
||||
) -> ArgValue {
|
||||
fn extract_value_from_tpl(source_map: &Arc<SourceMap>, span: &Span) -> JsWord {
|
||||
let source = source_map.lookup_byte_offset(span.lo);
|
||||
let lo = (span.lo.0 as usize) + 1;
|
||||
let hi = (span.hi.0 as usize) - 1;
|
||||
|
||||
JsWord::from(&source.sf.src[lo..hi])
|
||||
}
|
||||
|
||||
match expr {
|
||||
Expr::Ident(_) => ArgValue::Ident,
|
||||
Expr::Paren(ParenExpr { expr, .. }) => {
|
||||
if check_parens {
|
||||
extract_arg_val(
|
||||
source_map,
|
||||
top_level_ctxt,
|
||||
top_level_declared_vars,
|
||||
expr.as_ref(),
|
||||
@ -90,16 +78,16 @@ pub fn extract_arg_val(
|
||||
exp: exp.clone(),
|
||||
flags: flags.clone(),
|
||||
},
|
||||
Expr::Tpl(Tpl { span, exprs, .. }) => {
|
||||
Expr::Tpl(Tpl { exprs, quasis, .. }) => {
|
||||
if exprs.is_empty() {
|
||||
ArgValue::Str(extract_value_from_tpl(source_map, span))
|
||||
ArgValue::Str(quasis.first().unwrap().raw.clone())
|
||||
} else {
|
||||
ArgValue::Other
|
||||
}
|
||||
}
|
||||
Expr::TaggedTpl(TaggedTpl {
|
||||
tag,
|
||||
tpl: Tpl { span, exprs, .. },
|
||||
tpl: Tpl { exprs, quasis, .. },
|
||||
..
|
||||
}) => {
|
||||
if exprs.is_empty() {
|
||||
@ -121,7 +109,7 @@ pub fn extract_arg_val(
|
||||
return ArgValue::Other;
|
||||
}
|
||||
|
||||
return ArgValue::Str(extract_value_from_tpl(source_map, span));
|
||||
return ArgValue::Str(quasis.first().unwrap().raw.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user