Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiline CoffeeScript comments #181

Merged
merged 1 commit into from
Feb 17, 2024
Merged

support multiline CoffeeScript comments #181

merged 1 commit into from
Feb 17, 2024

Conversation

davidchambers
Copy link
Owner

This pull request adds support for multiline CoffeeScript comments.

CoffeeScript comments are now extracted by using CoffeeScript.helpers.extractAllCommentTokens in conjunction with CoffeeScript.tokens (previously we used a regular expression, /^([ \t]*)(.*)\n?/gm). Although comment tokens have a here property, I decided not to use it when I realized that CoffeeScript supports inline ### comments. Instead, we use locationData.last_line > locationData.first_line to determine whether a comment spans multiple lines.

rewriteCoffee is now very similar to rewriteJs, at a high level:

$ awk '$2 ~ /rewrite(Js|Coffee)/ { print $2; look = 1 } $0 == "};" { look = 0 } look && $1 == "//" && $2 ~ /:$/ { print }' lib/doctest.js
rewriteJs
  // 1: Parse source text to extract comments
  // 2: Preserve source text between comments
  // 3: Extract prefixed comment lines
  // 4: Coalesce related input and output lines
  // 5: Convert doctests to source text
  // 6: Sort verbatim and generated source text by original offsets
  // 7: Concatenate source text
rewriteCoffee
  // 1: Lex source text to extract comments
  // 2: Preserve source text between comments
  // 3: Extract prefixed comment lines
  // 4: Coalesce related input and output lines
  // 5: Convert doctests to source text
  // 6: Sort verbatim and generated source text by original offsets
  // 7: Concatenate source text

The only significant difference is that rewriteJs parses the source text whereas rewriteCoffee only lexes it.

rewriteJs now extracts lines from multiline comments with String.prototype.indexOf and String.prototype.slice rather than String.prototype.split.

let number = loc.start.line;
for (const text of value.split ('\n')) {
maybePushLine (text.replace (/^\s*[*]/, ''), offset, number);
offset += '\n'.length;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been offset += text.length + '\n'.length; (it was not actually a problem since offsets are only used for ordering).

@davidchambers davidchambers merged commit 107e3d3 into main Feb 17, 2024
6 checks passed
@davidchambers davidchambers deleted the multiline branch February 17, 2024 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants