use crate::*;
pub trait Alterator
where
Self: Checker,
{
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
Self::get_default(node, code, span, children)
}
fn get_text_span(node: &Node, code: &[u8], span: bool, text: bool) -> (String, Span) {
let text = if text {
String::from_utf8(code[node.object().start_byte()..node.object().end_byte()].to_vec())
.unwrap()
} else {
"".to_string()
};
if span {
let spos = node.object().start_position();
let epos = node.object().end_position();
(
text,
Some((spos.row + 1, spos.column + 1, epos.row + 1, epos.column + 1)),
)
} else {
(text, None)
}
}
fn get_default(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
let (text, span) = Self::get_text_span(node, code, span, node.object().child_count() == 0);
AstNode::new(node.object().kind(), text, span, children)
}
fn get_ast_node(
node: &Node,
code: &[u8],
children: Vec<AstNode>,
span: bool,
comment: bool,
) -> Option<AstNode> {
if comment && Self::is_comment(node) {
None
} else {
Some(Self::alterate(node, code, span, children))
}
}
}
impl Alterator for PreprocCode {}
impl Alterator for CcommentCode {}
impl Alterator for CppCode {
fn alterate(node: &Node, code: &[u8], span: bool, mut children: Vec<AstNode>) -> AstNode {
match Cpp::from(node.object().kind_id()) {
Cpp::StringLiteral | Cpp::CharLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
Cpp::PreprocDef | Cpp::PreprocFunctionDef | Cpp::PreprocCall => {
if let Some(last) = children.last() {
if last.r#type == "\n" {
children.pop();
}
}
Self::get_default(node, code, span, children)
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for CSharpCode {}
impl Alterator for PythonCode {}
impl Alterator for JavaCode {}
impl Alterator for MozjsCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Mozjs::from(node.object().kind_id()) {
Mozjs::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for JavascriptCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Javascript::from(node.object().kind_id()) {
Javascript::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for TypescriptCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Typescript::from(node.object().kind_id()) {
Typescript::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for TsxCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Tsx::from(node.object().kind_id()) {
Tsx::String => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for GoCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Go::from(node.object().kind_id()) {
Go::InterpretedStringLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for CssCode {}
impl Alterator for HtmlCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Html::from(node.object().kind_id()) {
Html::QuotedAttributeValue => {
if let [q1, attr, q2] = &children[..] {
let span = if span {
let (x, y, _, _) = q1.span.unwrap();
let (_, _, t, u) = q2.span.unwrap();
Some((x, y + 1, t, u - 1))
} else {
None
};
AstNode::new(attr.r#type, attr.value.clone(), span, Vec::new())
} else {
Self::get_default(node, code, span, children)
}
}
_ => Self::get_default(node, code, span, children),
}
}
}
impl Alterator for RustCode {
fn alterate(node: &Node, code: &[u8], span: bool, children: Vec<AstNode>) -> AstNode {
match Rust::from(node.object().kind_id()) {
Rust::StringLiteral | Rust::CharLiteral => {
let (text, span) = Self::get_text_span(node, code, span, true);
AstNode::new(node.object().kind(), text, span, Vec::new())
}
_ => Self::get_default(node, code, span, children),
}
}
}