forked from oxc-project/oxc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast_util.rs
559 lines (514 loc) · 19.8 KB
/
ast_util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use oxc_ast::{
ast::{BindingIdentifier, *},
AstKind,
};
use oxc_ecmascript::ToBoolean;
use oxc_semantic::{AstNode, IsGlobalReference, NodeId, ReferenceId, Semantic, SymbolId};
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator};
use crate::LintContext;
/// Test if an AST node is a boolean value that never changes. Specifically we
/// test for:
/// 1. Literal booleans (`true` or `false`)
/// 2. Unary `!` expressions with a constant value
/// 3. Constant booleans created via the `Boolean` global function
pub fn is_static_boolean<'a>(expr: &Expression<'a>, semantic: &Semantic<'a>) -> bool {
match expr {
Expression::BooleanLiteral(_) => true,
Expression::CallExpression(call_expr) => call_expr.is_constant(true, semantic),
Expression::UnaryExpression(unary_expr) => {
unary_expr.operator == UnaryOperator::LogicalNot
&& unary_expr.argument.is_constant(true, semantic)
}
_ => false,
}
}
/// Checks if a branch node of `LogicalExpression` short circuits the whole condition
fn is_logical_identity(op: LogicalOperator, expr: &Expression) -> bool {
match expr {
expr if expr.is_literal() => {
let boolean_value = expr.to_boolean();
(op == LogicalOperator::Or && boolean_value == Some(true))
|| (op == LogicalOperator::And && boolean_value == Some(false))
}
Expression::UnaryExpression(unary_expr) => {
op == LogicalOperator::And && unary_expr.operator == UnaryOperator::Void
}
Expression::LogicalExpression(logical_expr) => {
op == logical_expr.operator
&& (is_logical_identity(logical_expr.operator, &logical_expr.left)
|| is_logical_identity(logical_expr.operator, &logical_expr.right))
}
Expression::AssignmentExpression(assign_expr) => {
matches!(
assign_expr.operator,
AssignmentOperator::LogicalAnd | AssignmentOperator::LogicalOr
) && ((op == LogicalOperator::And
&& assign_expr.operator == AssignmentOperator::LogicalAnd)
|| (op == LogicalOperator::Or
&& assign_expr.operator == AssignmentOperator::LogicalOr))
&& is_logical_identity(op, &assign_expr.right)
}
Expression::ParenthesizedExpression(expr) => is_logical_identity(op, &expr.expression),
_ => false,
}
}
/// Checks if a node has a constant truthiness value.
/// `inBooleanPosition`:
/// `true` if checking the test of a condition.
/// `false` in all other cases.
/// When `false`, checks if -- for both string and number --
/// if coerced to that type, the value will be constant.
pub trait IsConstant<'a, 'b> {
fn is_constant(&self, in_boolean_position: bool, semantic: &Semantic<'a>) -> bool;
}
impl<'a> IsConstant<'a, '_> for Expression<'a> {
fn is_constant(&self, in_boolean_position: bool, semantic: &Semantic<'a>) -> bool {
match self {
Self::ArrowFunctionExpression(_)
| Self::FunctionExpression(_)
| Self::ClassExpression(_)
| Self::ObjectExpression(_) => true,
Self::TemplateLiteral(template) => {
let test_quasis = in_boolean_position
&& template.quasis.iter().any(|quasi| {
quasi.value.cooked.as_ref().is_some_and(|cooked| !cooked.is_empty())
});
let test_expressions =
template.expressions.iter().all(|expr| expr.is_constant(false, semantic));
test_quasis || test_expressions
}
Self::ArrayExpression(expr) => {
if in_boolean_position {
return true;
}
expr.elements.iter().all(|element| element.is_constant(false, semantic))
}
Self::UnaryExpression(expr) => match expr.operator {
UnaryOperator::Void => true,
UnaryOperator::Typeof if in_boolean_position => true,
UnaryOperator::LogicalNot => expr.argument.is_constant(true, semantic),
_ => expr.argument.is_constant(false, semantic),
},
Self::BinaryExpression(expr) => {
expr.operator != BinaryOperator::In
&& expr.left.is_constant(false, semantic)
&& expr.right.is_constant(false, semantic)
}
Self::LogicalExpression(expr) => {
let is_left_constant = expr.left.is_constant(in_boolean_position, semantic);
let is_right_constant = expr.right.is_constant(in_boolean_position, semantic);
let is_left_short_circuit =
is_left_constant && is_logical_identity(expr.operator, &expr.left);
let is_right_short_circuit = in_boolean_position
&& is_right_constant
&& is_logical_identity(expr.operator, &expr.right);
(is_left_constant && is_right_constant)
|| is_left_short_circuit
|| is_right_short_circuit
}
Self::NewExpression(_) => in_boolean_position,
Self::AssignmentExpression(expr) => match expr.operator {
AssignmentOperator::Assign => expr.right.is_constant(in_boolean_position, semantic),
AssignmentOperator::LogicalAnd if in_boolean_position => {
is_logical_identity(LogicalOperator::And, &expr.right)
}
AssignmentOperator::LogicalOr if in_boolean_position => {
is_logical_identity(LogicalOperator::Or, &expr.right)
}
_ => false,
},
Self::SequenceExpression(sequence_expr) => sequence_expr
.expressions
.iter()
.last()
.is_some_and(|last| last.is_constant(in_boolean_position, semantic)),
Self::CallExpression(call_expr) => call_expr.is_constant(in_boolean_position, semantic),
Self::ParenthesizedExpression(paren_expr) => {
paren_expr.expression.is_constant(in_boolean_position, semantic)
}
Self::Identifier(ident) => {
ident.name == "undefined" && semantic.is_reference_to_global_variable(ident)
}
_ if self.is_literal() => true,
_ => false,
}
}
}
impl<'a> IsConstant<'a, '_> for CallExpression<'a> {
fn is_constant(&self, _in_boolean_position: bool, semantic: &Semantic<'a>) -> bool {
if let Expression::Identifier(ident) = &self.callee {
if ident.name == "Boolean"
&& self
.arguments
.iter()
.next()
.map_or(true, |first| first.is_constant(true, semantic))
{
return semantic.is_reference_to_global_variable(ident);
}
}
false
}
}
impl<'a> IsConstant<'a, '_> for Argument<'a> {
fn is_constant(&self, in_boolean_position: bool, semantic: &Semantic<'a>) -> bool {
match self {
Self::SpreadElement(element) => element.is_constant(in_boolean_position, semantic),
match_expression!(Self) => {
self.to_expression().is_constant(in_boolean_position, semantic)
}
}
}
}
impl<'a> IsConstant<'a, '_> for ArrayExpressionElement<'a> {
fn is_constant(&self, in_boolean_position: bool, semantic: &Semantic<'a>) -> bool {
match self {
Self::SpreadElement(element) => element.is_constant(in_boolean_position, semantic),
match_expression!(Self) => {
self.to_expression().is_constant(in_boolean_position, semantic)
}
Self::Elision(_) => true,
}
}
}
impl<'a> IsConstant<'a, '_> for SpreadElement<'a> {
fn is_constant(&self, in_boolean_position: bool, semantic: &Semantic<'a>) -> bool {
self.argument.is_constant(in_boolean_position, semantic)
}
}
/// Return the innermost `Function` or `ArrowFunctionExpression` Node
/// enclosing the specified node
pub fn get_enclosing_function<'a, 'b>(
node: &'b AstNode<'a>,
semantic: &'b Semantic<'a>,
) -> Option<&'b AstNode<'a>> {
let mut current_node = node;
loop {
if matches!(current_node.kind(), AstKind::Program(_)) {
return None;
}
if matches!(current_node.kind(), AstKind::Function(_) | AstKind::ArrowFunctionExpression(_))
{
return Some(current_node);
}
current_node = semantic.nodes().parent_node(current_node.id())?;
}
}
/// Returns if `arg` is the `n`th (0-indexed) argument of `call`.
pub fn is_nth_argument<'a>(call: &CallExpression<'a>, arg: &Argument<'a>, n: usize) -> bool {
let nth = &call.arguments[n];
nth.span() == arg.span()
}
/// Jump to the outer most of chained parentheses if any
pub fn outermost_paren<'a, 'b>(
node: &'b AstNode<'a>,
semantic: &'b Semantic<'a>,
) -> &'b AstNode<'a> {
let mut node = node;
loop {
if let Some(parent) = semantic.nodes().parent_node(node.id()) {
if let AstKind::ParenthesizedExpression(_) = parent.kind() {
node = parent;
continue;
}
}
break;
}
node
}
pub fn outermost_paren_parent<'a, 'b>(
node: &'b AstNode<'a>,
semantic: &'b Semantic<'a>,
) -> Option<&'b AstNode<'a>> {
semantic
.nodes()
.ancestors(node.id())
.skip(1)
.find(|parent| !matches!(parent.kind(), AstKind::ParenthesizedExpression(_)))
}
pub fn nth_outermost_paren_parent<'a, 'b>(
node: &'b AstNode<'a>,
semantic: &'b Semantic<'a>,
n: usize,
) -> Option<&'b AstNode<'a>> {
semantic
.nodes()
.ancestors(node.id())
.skip(1)
.filter(|parent| !matches!(parent.kind(), AstKind::ParenthesizedExpression(_)))
.nth(n)
}
/// Iterate over parents of `node`, skipping nodes that are also ignored by
/// [`Expression::get_inner_expression`].
pub fn iter_outer_expressions<'a, 's>(
semantic: &'s Semantic<'a>,
node_id: NodeId,
) -> impl Iterator<Item = AstKind<'a>> + 's {
semantic.nodes().ancestor_kinds(node_id).skip(1).filter(|parent| {
!matches!(
parent,
AstKind::ParenthesizedExpression(_)
| AstKind::TSAsExpression(_)
| AstKind::TSSatisfiesExpression(_)
| AstKind::TSInstantiationExpression(_)
| AstKind::TSNonNullExpression(_)
| AstKind::TSTypeAssertion(_)
)
})
}
pub fn get_declaration_of_variable<'a, 'b>(
ident: &IdentifierReference<'a>,
semantic: &'b Semantic<'a>,
) -> Option<&'b AstNode<'a>> {
let symbol_id = get_symbol_id_of_variable(ident, semantic)?;
let symbol_table = semantic.symbols();
Some(semantic.nodes().get_node(symbol_table.get_declaration(symbol_id)))
}
pub fn get_declaration_from_reference_id<'a, 'b>(
reference_id: ReferenceId,
semantic: &'b Semantic<'a>,
) -> Option<&'b AstNode<'a>> {
let symbol_table = semantic.symbols();
let symbol_id = symbol_table.get_reference(reference_id).symbol_id()?;
Some(semantic.nodes().get_node(symbol_table.get_declaration(symbol_id)))
}
pub fn get_symbol_id_of_variable(
ident: &IdentifierReference,
semantic: &Semantic<'_>,
) -> Option<SymbolId> {
semantic.symbols().get_reference(ident.reference_id()).symbol_id()
}
pub fn extract_regex_flags<'a>(
args: &'a oxc_allocator::Vec<'a, Argument<'a>>,
) -> Option<RegExpFlags> {
if args.len() <= 1 {
return None;
}
let flag_arg = match &args[1] {
Argument::StringLiteral(flag_arg) => flag_arg.value.clone(),
Argument::TemplateLiteral(template) if template.is_no_substitution_template() => {
template.quasi().expect("no-substitution templates always have a quasi")
}
_ => return None,
};
let mut flags = RegExpFlags::empty();
for ch in flag_arg.chars() {
let flag = RegExpFlags::try_from(ch).ok()?;
flags |= flag;
}
Some(flags)
}
pub fn is_method_call<'a>(
call_expr: &CallExpression<'a>,
objects: Option<&[&'a str]>,
methods: Option<&[&'a str]>,
min_arg_count: Option<usize>,
max_arg_count: Option<usize>,
) -> bool {
if let Some(min_arg_count) = min_arg_count {
if call_expr.arguments.len() < min_arg_count {
return false;
}
}
if let Some(max_arg_count) = max_arg_count {
if call_expr.arguments.len() > max_arg_count {
return false;
}
}
let callee_without_parentheses = call_expr.callee.without_parentheses();
let member_expr = match callee_without_parentheses {
match_member_expression!(Expression) => callee_without_parentheses.to_member_expression(),
Expression::ChainExpression(chain) => match chain.expression.member_expression() {
Some(e) => e,
None => return false,
},
_ => return false,
};
if let Some(objects) = objects {
let Expression::Identifier(ident) = member_expr.object().without_parentheses() else {
return false;
};
if !objects.contains(&ident.name.as_str()) {
return false;
}
}
if let Some(methods) = methods {
let Some(static_property_name) = member_expr.static_property_name() else {
return false;
};
if !methods.contains(&static_property_name) {
return false;
}
}
true
}
pub fn is_new_expression<'a>(
new_expr: &NewExpression<'a>,
names: &[&'a str],
min_arg_count: Option<usize>,
max_arg_count: Option<usize>,
) -> bool {
if let Some(min_arg_count) = min_arg_count {
if new_expr.arguments.len() < min_arg_count {
return false;
}
}
if let Some(max_arg_count) = max_arg_count {
if new_expr.arguments.len() > max_arg_count {
return false;
}
}
let Expression::Identifier(ident) = new_expr.callee.without_parentheses() else {
return false;
};
if !names.contains(&ident.name.as_str()) {
return false;
}
true
}
pub fn call_expr_method_callee_info<'a>(
call_expr: &'a CallExpression<'a>,
) -> Option<(Span, &'a str)> {
let member_expr = call_expr.callee.without_parentheses().as_member_expression()?;
member_expr.static_property_info()
}
pub fn get_new_expr_ident_name<'a>(new_expr: &'a NewExpression<'a>) -> Option<&'a str> {
let Expression::Identifier(ident) = new_expr.callee.without_parentheses() else {
return None;
};
Some(ident.name.as_str())
}
pub fn is_global_require_call(call_expr: &CallExpression, ctx: &Semantic) -> bool {
if call_expr.arguments.len() != 1 {
return false;
}
call_expr.callee.is_global_reference_name("require", ctx.symbols())
}
pub fn is_function_node(node: &AstNode) -> bool {
match node.kind() {
AstKind::Function(f) if f.is_function_declaration() => true,
AstKind::Function(f) if f.is_expression() => true,
AstKind::ArrowFunctionExpression(_) => true,
_ => false,
}
}
pub fn get_function_like_declaration<'b>(
node: &AstNode<'b>,
ctx: &Semantic<'b>,
) -> Option<&'b BindingIdentifier<'b>> {
let parent = outermost_paren_parent(node, ctx)?;
let decl = parent.kind().as_variable_declarator()?;
decl.id.get_binding_identifier()
}
/// Get the first identifier reference within a member expression chain or
/// standalone reference.
///
/// For example, when called on the right-hand side of this [`AssignmentExpression`]:
/// ```ts
/// let x = a
/// // ^
/// let y = a.b.c
/// // ^
/// ```
///
/// As this function walks down the member expression chain, if no identifier
/// reference is found, it returns [`Err`] with the leftmost expression.
/// ```ts
/// let x = 1 + 1
/// // ^^^^^ Err(BinaryExpression)
/// let y = this.foo.bar
/// // ^^^^ Err(ThisExpression)
/// ```
pub fn leftmost_identifier_reference<'a, 'b: 'a>(
expr: &'b Expression<'a>,
) -> Result<&'a IdentifierReference<'a>, &'b Expression<'a>> {
match expr {
Expression::Identifier(ident) => Ok(ident.as_ref()),
Expression::StaticMemberExpression(mem) => leftmost_identifier_reference(&mem.object),
Expression::ComputedMemberExpression(mem) => leftmost_identifier_reference(&mem.object),
Expression::PrivateFieldExpression(mem) => leftmost_identifier_reference(&mem.object),
_ => Err(expr),
}
}
fn is_definitely_non_error_type(ty: &TSType) -> bool {
match ty {
TSType::TSNumberKeyword(_)
| TSType::TSStringKeyword(_)
| TSType::TSBooleanKeyword(_)
| TSType::TSNullKeyword(_)
| TSType::TSUndefinedKeyword(_) => true,
TSType::TSUnionType(union) => union.types.iter().all(is_definitely_non_error_type),
TSType::TSIntersectionType(intersect) => {
intersect.types.iter().all(is_definitely_non_error_type)
}
_ => false,
}
}
pub fn could_be_error(ctx: &LintContext, expr: &Expression) -> bool {
match expr.get_inner_expression() {
Expression::NewExpression(_)
| Expression::AwaitExpression(_)
| Expression::CallExpression(_)
| Expression::ChainExpression(_)
| Expression::YieldExpression(_)
| Expression::PrivateFieldExpression(_)
| Expression::StaticMemberExpression(_)
| Expression::ComputedMemberExpression(_)
| Expression::TaggedTemplateExpression(_) => true,
Expression::AssignmentExpression(expr) => {
if matches!(expr.operator, AssignmentOperator::Assign | AssignmentOperator::LogicalAnd)
{
return could_be_error(ctx, &expr.right);
}
if matches!(
expr.operator,
AssignmentOperator::LogicalOr | AssignmentOperator::LogicalNullish
) {
return expr.left.get_expression().map_or(true, |expr| could_be_error(ctx, expr))
|| could_be_error(ctx, &expr.right);
}
false
}
Expression::SequenceExpression(expr) => {
expr.expressions.last().is_some_and(|expr| could_be_error(ctx, expr))
}
Expression::LogicalExpression(expr) => {
if matches!(expr.operator, LogicalOperator::And) {
return could_be_error(ctx, &expr.right);
}
could_be_error(ctx, &expr.left) || could_be_error(ctx, &expr.right)
}
Expression::ConditionalExpression(expr) => {
could_be_error(ctx, &expr.consequent) || could_be_error(ctx, &expr.alternate)
}
Expression::Identifier(ident) => {
let reference = ctx.symbols().get_reference(ident.reference_id());
let Some(symbol_id) = reference.symbol_id() else {
return true;
};
let decl = ctx.nodes().get_node(ctx.symbols().get_declaration(symbol_id));
match decl.kind() {
AstKind::VariableDeclarator(decl) => {
if let Some(init) = &decl.init {
could_be_error(ctx, init)
} else {
// TODO: warn about throwing undefined
false
}
}
AstKind::Function(_)
| AstKind::Class(_)
| AstKind::TSModuleDeclaration(_)
| AstKind::TSEnumDeclaration(_) => false,
AstKind::FormalParameter(param) => !param
.pattern
.type_annotation
.as_ref()
.is_some_and(|annot| is_definitely_non_error_type(&annot.type_annotation)),
_ => true,
}
}
_ => false,
}
}