diff --git a/package.json b/package.json index fd576e8..c0614c2 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/packages/goldstein/index.spec.js b/packages/goldstein/index.spec.js index 19fe724..b10e504 100644 --- a/packages/goldstein/index.spec.js +++ b/packages/goldstein/index.spec.js @@ -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); diff --git a/packages/parser/index.js b/packages/parser/index.js index d6cfbaa..1f61137 100644 --- a/packages/parser/index.js +++ b/packages/parser/index.js @@ -1,4 +1,5 @@ import {Parser} from 'acorn'; +import {attachComments} from 'estree-util-attach-comments'; export const extendParser = (keywords) => { const parser = Parser.extend(...keywords); @@ -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, }; }; diff --git a/packages/printer/visitors/try-statement.spec.js b/packages/printer/visitors/try-statement.spec.js index c833928..4fd3877 100644 --- a/packages/printer/visitors/try-statement.spec.js +++ b/packages/printer/visitors/try-statement.spec.js @@ -49,4 +49,3 @@ test('goldstein: printer: visitors: try catch', (t) => { t.equal(result, `${source}\n`); t.end(); }); -