Replies: 5 comments 11 replies
-
Option: save all trivias in a struct Trivias {
comments: BTreeMap<u32, (CommentKind, Span)>,
eslint_configuration_comments: BTreeMap<u32, Span>, // configuration range
typescript_configuration_comments: ...,
}
-- For a printer, we can keep an iterator on |
Beta Was this translation helpful? Give feedback.
-
Option: hack it like rustfmt https://github.com/rust-lang/rustfmt/blob/master/Contributing.md#a-quick-tour-of-rustfmt |
Beta Was this translation helpful? Give feedback.
-
From this tsc doc https://github.com/microsoft/TypeScript-Compiler-Notes/blob/main/codebase/src/compiler/scanner.md#trivia It seems we need to get trivia ownership correct, otherwise it'll be a disaster while doing text edit and printing. |
Beta Was this translation helpful? Give feedback.
-
Just wanted to add my 2c to this, having the comments be available on nodes is extremely valuable for static analysis of javascript, consider the following example: This is TS compilers output, which has a I have a tool (written in JS) that analyzes custom elements, and we heavily rely on JSdoc during analysis, because it can completely change the way something should be interpreted, or if it should be interpreted at all. |
Beta Was this translation helpful? Give feedback.
-
I came up with a solution to track comments on AST nodes, by only adding 4 bytes to pub struct Span {
pub start: u32,
pub end: u32,
pub leading_comments_offset: Option<NonZeroU16>,
pub trailing_comments_offset: Option<NonZeroU16>,
} To get all leading and trailing comments, we compute if let Some(offset) = &span.leading_comments_offset {
let leading_comments = trivias.comments.range(span.start - offset as u32 .. span.start);
}
if let Some(offset) = &span.trailing_comments_offset {
let trailing_comments = trivias.comments.range(span.end .. span.end + offset as u32);
} Now the harder part is the inner comments between stuff that doesn't have nodes to attach to, for example: async /* xxx */ function /* yyy */ foo() {} prettier formats this to async function /* xxx */ /* yyy */ foo() {} Babel returns the attribute "innerComments" on the function AST, and prettier just finds a place to insert these inner comments. I don't really like this. A research within
I'm going to dig deeper to find a solution to these "inner comments". |
Beta Was this translation helpful? Give feedback.
-
Topic around trivia (comment) handling.
Explanation of trivia: https://github.com/basarat/typescript-book/blob/master/docs/compiler/ast-trivia.md
Research:
esbuild
Doesn't support comments, except some non-trivial comments. https://github.com/evanw/esbuild/search?q=comments&type=issues
eslint
Uses the token list for finding comments
estree
Could not standardize comments.
https://github.com/estree/estree/issues/201
tsc https://github.com/microsoft/TypeScript-Compiler-Notes/blob/main/codebase/src/compiler/scanner.md#trivia
swc
Beta Was this translation helpful? Give feedback.
All reactions