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

Attaching inline comments breaks up lines #464

Open
StefanBlamberg opened this issue Mar 25, 2024 · 0 comments
Open

Attaching inline comments breaks up lines #464

StefanBlamberg opened this issue Mar 25, 2024 · 0 comments

Comments

@StefanBlamberg
Copy link

When attaching comments and generating code there seems to be a bug with the placement of inline comments.
They always break up the line which can lead to problems, for example when returning objects in strict mode.
Lets take this sample code snippet:

function test() {
  "use strict";
  return /** some inline comment */ {
    someFunc: function() {}
  };
}

When parsing it with acorn and then attaching comments with escodegen and generating the code like this:

import { parse } from 'acorn';
import escodegen from '@javascript-obfuscator/escodegen';

const code = `
function test() {
  "use strict";
  return /** some inline comment */ {
    someFunc: function() {}
  };
}
`

const comments = [];
const tokens = [];
const ast = parse(code, {
  ecmaVersion: 'latest',
  ranges: true,
  locations: true,
  onComment: comments,
  onToken: tokens,
})

escodegen.attachComments(ast, comments, tokens);
console.log(escodegen.generate(ast, {comment: true}));

the output is generated as

function test() {
    'use strict';
    return /** some inline comment */
    {
        someFunc: function () {
        }
    };
}

Notice how the curly brace of the object got moved to a new line, which breaks due to the strict mode, similar to this error (https://stackoverflow.com/a/63953324/5843525)

It generally seems to be a problem that inline comments break up code lines in unexpected ways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant