Track if inline comments are line-style or block-style
Previously all inline comments were assumed to be line style.
This commit is contained in:
@ -707,7 +707,7 @@ pub struct NonCodeNode {
|
|||||||
impl NonCodeNode {
|
impl NonCodeNode {
|
||||||
pub fn value(&self) -> String {
|
pub fn value(&self) -> String {
|
||||||
match &self.value {
|
match &self.value {
|
||||||
NonCodeValue::InlineComment { value } => value.clone(),
|
NonCodeValue::InlineComment { value, style: _ } => value.clone(),
|
||||||
NonCodeValue::BlockComment { value } => value.clone(),
|
NonCodeValue::BlockComment { value } => value.clone(),
|
||||||
NonCodeValue::NewLineBlockComment { value } => value.clone(),
|
NonCodeValue::NewLineBlockComment { value } => value.clone(),
|
||||||
NonCodeValue::NewLine => "\n\n".to_string(),
|
NonCodeValue::NewLine => "\n\n".to_string(),
|
||||||
@ -716,7 +716,14 @@ impl NonCodeNode {
|
|||||||
|
|
||||||
pub fn format(&self, indentation: &str) -> String {
|
pub fn format(&self, indentation: &str) -> String {
|
||||||
match &self.value {
|
match &self.value {
|
||||||
NonCodeValue::InlineComment { value } => format!(" // {}\n", value),
|
NonCodeValue::InlineComment {
|
||||||
|
value,
|
||||||
|
style: CommentStyle::Line,
|
||||||
|
} => format!(" // {}\n", value),
|
||||||
|
NonCodeValue::InlineComment {
|
||||||
|
value,
|
||||||
|
style: CommentStyle::Block,
|
||||||
|
} => format!(" /* {} */\n", value),
|
||||||
NonCodeValue::BlockComment { value } => {
|
NonCodeValue::BlockComment { value } => {
|
||||||
let add_start_new_line = if self.start == 0 { "" } else { "\n" };
|
let add_start_new_line = if self.start == 0 { "" } else { "\n" };
|
||||||
if value.contains('\n') {
|
if value.contains('\n') {
|
||||||
@ -738,14 +745,27 @@ impl NonCodeNode {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
|
#[ts(export)]
|
||||||
|
#[serde(tag = "type", rename_all = "camelCase")]
|
||||||
|
pub enum CommentStyle {
|
||||||
|
/// Like // foo
|
||||||
|
Line,
|
||||||
|
/// Like /* foo */
|
||||||
|
Block,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
|
||||||
#[ts(export)]
|
#[ts(export)]
|
||||||
#[serde(tag = "type", rename_all = "camelCase")]
|
#[serde(tag = "type", rename_all = "camelCase")]
|
||||||
pub enum NonCodeValue {
|
pub enum NonCodeValue {
|
||||||
/// An inline comment.
|
/// An inline comment.
|
||||||
/// An example of this is the following: `1 + 1 // This is an inline comment`.
|
/// Here are examples:
|
||||||
|
/// `1 + 1 // This is an inline comment`.
|
||||||
|
/// `1 + 1 /* Here's another */`.
|
||||||
InlineComment {
|
InlineComment {
|
||||||
value: String,
|
value: String,
|
||||||
|
style: CommentStyle,
|
||||||
},
|
},
|
||||||
/// A block comment.
|
/// A block comment.
|
||||||
/// An example of this is the following:
|
/// An example of this is the following:
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use std::{collections::HashMap, str::FromStr};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::types::{
|
ast::types::{
|
||||||
ArrayExpression, BinaryExpression, BinaryPart, BodyItem, CallExpression, ExpressionStatement,
|
ArrayExpression, BinaryExpression, BinaryPart, BodyItem, CallExpression, CommentStyle, ExpressionStatement,
|
||||||
FunctionExpression, Identifier, Literal, LiteralIdentifier, MemberExpression, MemberObject, NonCodeMeta,
|
FunctionExpression, Identifier, Literal, LiteralIdentifier, MemberExpression, MemberObject, NonCodeMeta,
|
||||||
NonCodeNode, NonCodeValue, ObjectExpression, ObjectKeyInfo, ObjectProperty, PipeExpression, PipeSubstitution,
|
NonCodeNode, NonCodeValue, ObjectExpression, ObjectKeyInfo, ObjectProperty, PipeExpression, PipeSubstitution,
|
||||||
Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, VariableDeclaration, VariableDeclarator,
|
Program, ReturnStatement, UnaryExpression, UnaryOperator, Value, VariableDeclaration, VariableDeclarator,
|
||||||
@ -295,6 +295,12 @@ impl Parser {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let is_block_style = non_code_tokens
|
||||||
|
.iter()
|
||||||
|
.next()
|
||||||
|
.map(|tok| matches!(tok.token_type, TokenType::BlockComment))
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
let full_string = non_code_tokens
|
let full_string = non_code_tokens
|
||||||
.iter()
|
.iter()
|
||||||
.map(|t| {
|
.map(|t| {
|
||||||
@ -340,7 +346,14 @@ impl Parser {
|
|||||||
} else if is_new_line_comment {
|
} else if is_new_line_comment {
|
||||||
NonCodeValue::BlockComment { value: full_string }
|
NonCodeValue::BlockComment { value: full_string }
|
||||||
} else {
|
} else {
|
||||||
NonCodeValue::InlineComment { value: full_string }
|
NonCodeValue::InlineComment {
|
||||||
|
value: full_string,
|
||||||
|
style: if is_block_style {
|
||||||
|
CommentStyle::Block
|
||||||
|
} else {
|
||||||
|
CommentStyle::Line
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
Ok((Some(node), end_index - 1))
|
Ok((Some(node), end_index - 1))
|
||||||
|
|||||||
Reference in New Issue
Block a user