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 {
 | 
			
		||||
    pub fn value(&self) -> String {
 | 
			
		||||
        match &self.value {
 | 
			
		||||
            NonCodeValue::InlineComment { value } => value.clone(),
 | 
			
		||||
            NonCodeValue::InlineComment { value, style: _ } => value.clone(),
 | 
			
		||||
            NonCodeValue::BlockComment { value } => value.clone(),
 | 
			
		||||
            NonCodeValue::NewLineBlockComment { value } => value.clone(),
 | 
			
		||||
            NonCodeValue::NewLine => "\n\n".to_string(),
 | 
			
		||||
@ -716,7 +716,14 @@ impl NonCodeNode {
 | 
			
		||||
 | 
			
		||||
    pub fn format(&self, indentation: &str) -> String {
 | 
			
		||||
        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 } => {
 | 
			
		||||
                let add_start_new_line = if self.start == 0 { "" } else { "\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)]
 | 
			
		||||
#[ts(export)]
 | 
			
		||||
#[serde(tag = "type", rename_all = "camelCase")]
 | 
			
		||||
pub enum NonCodeValue {
 | 
			
		||||
    /// 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 {
 | 
			
		||||
        value: String,
 | 
			
		||||
        style: CommentStyle,
 | 
			
		||||
    },
 | 
			
		||||
    /// A block comment.
 | 
			
		||||
    /// An example of this is the following:
 | 
			
		||||
 | 
			
		||||
@ -2,7 +2,7 @@ use std::{collections::HashMap, str::FromStr};
 | 
			
		||||
 | 
			
		||||
use crate::{
 | 
			
		||||
    ast::types::{
 | 
			
		||||
        ArrayExpression, BinaryExpression, BinaryPart, BodyItem, CallExpression, ExpressionStatement,
 | 
			
		||||
        ArrayExpression, BinaryExpression, BinaryPart, BodyItem, CallExpression, CommentStyle, ExpressionStatement,
 | 
			
		||||
        FunctionExpression, Identifier, Literal, LiteralIdentifier, MemberExpression, MemberObject, NonCodeMeta,
 | 
			
		||||
        NonCodeNode, NonCodeValue, ObjectExpression, ObjectKeyInfo, ObjectProperty, PipeExpression, PipeSubstitution,
 | 
			
		||||
        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
 | 
			
		||||
            .iter()
 | 
			
		||||
            .map(|t| {
 | 
			
		||||
@ -340,7 +346,14 @@ impl Parser {
 | 
			
		||||
            } else if is_new_line_comment {
 | 
			
		||||
                NonCodeValue::BlockComment { value: full_string }
 | 
			
		||||
            } 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))
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user