-
-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ecmascript): add
ToBoolean
, ToNumber
, ToString
- Loading branch information
Showing
15 changed files
with
339 additions
and
261 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#[allow(clippy::wildcard_imports)] | ||
use oxc_ast::ast::*; | ||
|
||
use crate::{NumberValue, ToNumber}; | ||
|
||
/// `ToBoolean` | ||
/// | ||
/// <https://tc39.es/ecma262/#sec-toboolean> | ||
pub trait ToBoolean<'a> { | ||
fn to_boolean(&self) -> Option<bool>; | ||
} | ||
|
||
impl<'a> ToBoolean<'a> for Expression<'a> { | ||
fn to_boolean(&self) -> Option<bool> { | ||
match self { | ||
Expression::RegExpLiteral(_) | ||
| Expression::ArrayExpression(_) | ||
| Expression::ArrowFunctionExpression(_) | ||
| Expression::ClassExpression(_) | ||
| Expression::FunctionExpression(_) | ||
| Expression::NewExpression(_) | ||
| Expression::ObjectExpression(_) => Some(true), | ||
Expression::NullLiteral(_) => Some(false), | ||
Expression::BooleanLiteral(boolean_literal) => Some(boolean_literal.value), | ||
Expression::NumericLiteral(number_literal) => Some(number_literal.value != 0.0), | ||
Expression::BigIntLiteral(big_int_literal) => Some(!big_int_literal.is_zero()), | ||
Expression::StringLiteral(string_literal) => Some(!string_literal.value.is_empty()), | ||
Expression::TemplateLiteral(template_literal) => { | ||
// only for `` | ||
template_literal | ||
.quasis | ||
.first() | ||
.filter(|quasi| quasi.tail) | ||
.and_then(|quasi| quasi.value.cooked.as_ref()) | ||
.map(|cooked| !cooked.is_empty()) | ||
} | ||
Expression::Identifier(ident) => match ident.name.as_str() { | ||
"NaN" | "undefined" => Some(false), | ||
"Infinity" => Some(true), | ||
_ => None, | ||
}, | ||
Expression::AssignmentExpression(assign_expr) => { | ||
match assign_expr.operator { | ||
AssignmentOperator::LogicalAnd | AssignmentOperator::LogicalOr => None, | ||
// For ASSIGN, the value is the value of the RHS. | ||
_ => assign_expr.right.to_boolean(), | ||
} | ||
} | ||
Expression::LogicalExpression(logical_expr) => { | ||
match logical_expr.operator { | ||
// true && true -> true | ||
// true && false -> false | ||
// a && true -> None | ||
LogicalOperator::And => { | ||
let left = logical_expr.left.to_boolean(); | ||
let right = logical_expr.right.to_boolean(); | ||
match (left, right) { | ||
(Some(true), Some(true)) => Some(true), | ||
(Some(false), _) | (_, Some(false)) => Some(false), | ||
(None, _) | (_, None) => None, | ||
} | ||
} | ||
// true || false -> true | ||
// false || false -> false | ||
// a || b -> None | ||
LogicalOperator::Or => { | ||
let left = logical_expr.left.to_boolean(); | ||
let right = logical_expr.right.to_boolean(); | ||
|
||
match (left, right) { | ||
(Some(true), _) | (_, Some(true)) => Some(true), | ||
(Some(false), Some(false)) => Some(false), | ||
(None, _) | (_, None) => None, | ||
} | ||
} | ||
LogicalOperator::Coalesce => None, | ||
} | ||
} | ||
Expression::SequenceExpression(sequence_expr) => { | ||
// For sequence expression, the value is the value of the RHS. | ||
sequence_expr.expressions.last().and_then(ToBoolean::to_boolean) | ||
} | ||
Expression::UnaryExpression(unary_expr) => { | ||
if unary_expr.operator == UnaryOperator::Void { | ||
Some(false) | ||
} else if matches!( | ||
unary_expr.operator, | ||
UnaryOperator::BitwiseNot | ||
| UnaryOperator::UnaryPlus | ||
| UnaryOperator::UnaryNegation | ||
) { | ||
// ~0 -> true | ||
// +1 -> true | ||
// +0 -> false | ||
// -0 -> false | ||
self.to_number().map(|value| value != NumberValue::Number(0_f64)) | ||
} else if unary_expr.operator == UnaryOperator::LogicalNot { | ||
// !true -> false | ||
unary_expr.argument.to_boolean().map(|b| !b) | ||
} else { | ||
None | ||
} | ||
} | ||
_ => None, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
use std::borrow::Cow; | ||
|
||
#[allow(clippy::wildcard_imports)] | ||
use oxc_ast::ast::*; | ||
use oxc_syntax::operator::UnaryOperator; | ||
|
||
use crate::ToBoolean; | ||
|
||
/// `ToString` | ||
/// | ||
/// <https://tc39.es/ecma262/#sec-tostring> | ||
pub trait ToJsString<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>>; | ||
} | ||
|
||
impl<'a> ToJsString<'a> for Expression<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
match self { | ||
Expression::StringLiteral(lit) => lit.to_js_string(), | ||
Expression::TemplateLiteral(lit) => lit.to_js_string(), | ||
Expression::Identifier(ident) => ident.to_js_string(), | ||
Expression::NumericLiteral(lit) => lit.to_js_string(), | ||
Expression::BigIntLiteral(lit) => lit.to_js_string(), | ||
Expression::NullLiteral(lit) => lit.to_js_string(), | ||
Expression::BooleanLiteral(lit) => lit.to_js_string(), | ||
Expression::UnaryExpression(e) => e.to_js_string(), | ||
Expression::ArrayExpression(e) => e.to_js_string(), | ||
Expression::ObjectExpression(e) => e.to_js_string(), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for StringLiteral<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
Some(Cow::Borrowed(self.value.as_str())) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for TemplateLiteral<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
let mut str = String::new(); | ||
for (i, quasi) in self.quasis.iter().enumerate() { | ||
str.push_str(quasi.value.cooked.as_ref()?); | ||
|
||
if i < self.expressions.len() { | ||
let expr = &self.expressions[i]; | ||
let value = expr.to_js_string()?; | ||
str.push_str(&value); | ||
} | ||
} | ||
Some(Cow::Owned(str)) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for IdentifierReference<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
let name = self.name.as_str(); | ||
matches!(name, "undefined" | "Infinity" | "NaN").then(|| Cow::Borrowed(name)) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for NumericLiteral<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
// FIXME: to js number string | ||
Some(Cow::Owned(self.value.to_string())) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for BigIntLiteral<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
// FIXME: to js bigint string | ||
Some(Cow::Owned(self.raw.to_string())) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for BooleanLiteral { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
Some(Cow::Borrowed(if self.value { "true" } else { "false" })) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for NullLiteral { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
Some(Cow::Borrowed("null")) | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for UnaryExpression<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
match self.operator { | ||
UnaryOperator::Void => Some(Cow::Borrowed("undefined")), | ||
UnaryOperator::LogicalNot => self | ||
.argument | ||
.to_boolean() | ||
.map(|boolean| Cow::Borrowed(if boolean { "false" } else { "true" })), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for ArrayExpression<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
// TODO: https://github.com/google/closure-compiler/blob/e13f5cd0a5d3d35f2db1e6c03fdf67ef02946009/src/com/google/javascript/jscomp/NodeUtil.java#L302-L303 | ||
None | ||
} | ||
} | ||
|
||
impl<'a> ToJsString<'a> for ObjectExpression<'a> { | ||
fn to_js_string(&self) -> Option<Cow<'a, str>> { | ||
Some(Cow::Borrowed("[object Object]")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
crates/oxc_linter/src/rules/eslint/array_callback_return/return_checker.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.