Skip to content

Commit

Permalink
use CoffeeScript.nodes to extract comments
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Jan 4, 2024
1 parent 6aa7f6d commit 63b2ef5
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lib/rewriters/coffeescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,35 @@ import show from 'sanctuary-show';
import rewrite from '../rewrite.js';


function comments(node) {
return (
Object.values (node)
.flatMap (x => {
switch (Object.prototype.toString.call (x)) {
case '[object Array]': return x;
case '[object Object]': return [x];
default: return [];
}
})
.flatMap (comments)
.concat (node.comments ?? [])
);
}

const chunksCoffee = input => {
const chunks = {literals: new Map ([]), comments: new Map ([])};
let number = 0;
for (const text of input.match (/^.*(?=\n)/gm)) {
number += 1;
const match = /^([ \t]*)#(?!##)(.*)$/.exec (text);
if (match == null) {
chunks.literals.set (number, text);
} else {
const [, indent, uncommented] = match;
chunks.comments.set (number, {indent, number, text: uncommented});
}
let offset = 0;
for (const comment of comments (CoffeeScript.nodes (input))) {
const [start, end] = comment.locationData.range;
chunks.literals.set (offset, input.slice (offset, start));
chunks.comments.set (start, {
indent: ' '.repeat (comment.locationData.first_column),
number: comment.locationData.first_line + 1,
text: comment.content,
});
offset = end;
}
chunks.literals.set (offset, input.slice (offset));
return chunks;
};

Expand Down

0 comments on commit 63b2ef5

Please sign in to comment.