Skip to content

Commit

Permalink
feature: goldstein: improve support of comments
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Feb 20, 2024
1 parent 0d997c3 commit 5728421
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"esbuild": "^0.20.1",
"esbuild-node-builtins": "^0.1.0",
"estree-to-babel": "^9.0.0",
"estree-util-attach-comments": "^3.0.0",
"putout": "^35.0.0",
"try-catch": "^3.0.1"
},
Expand Down
19 changes: 19 additions & 0 deletions packages/goldstein/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,25 @@ test('goldstein: parse: import', (t) => {
t.end();
});

test('goldstein: parse: comment', (t) => {
const result = compile(montag`
import hello from './hello.js';
// abc
const x = 5;
`);

const expected = montag`
import hello from './hello.js';
// abc
const x = 5;\n
`;

t.equal(result, expected);
t.end();
});

test('goldstein: print', (t) => {
const source = `const a = try f('hello');`;
const ast = parse(source);
Expand Down
14 changes: 7 additions & 7 deletions packages/parser/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Parser} from 'acorn';
import {attachComments} from 'estree-util-attach-comments';

export const extendParser = (keywords) => {
const parser = Parser.extend(...keywords);
Expand All @@ -17,17 +18,16 @@ const createParse = (parser) => (source) => {
locations: true,
comment: true,
ranges: true,
onComment: (a) => {
comments.push(a);
},
onComment: comments,
};

const result = parser.parse(source, options);
const tokens = Array.from(parser.tokenizer(source, options));
const ast = parser.parse(source, options);

attachComments(ast, comments);

return {
...result,
tokens,
...ast,
tokens: Array.from(parser.tokenizer(source, options)),
comments,
};
};
1 change: 0 additions & 1 deletion packages/printer/visitors/try-statement.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ test('goldstein: printer: visitors: try catch', (t) => {
t.equal(result, `${source}\n`);
t.end();
});

0 comments on commit 5728421

Please sign in to comment.