pionxzh / ast-types-x

Esprima-compatible implementation of the Mozilla JS Parser API
MIT License
0 stars 0 forks source link

Error: did not recognize object of type "PrivateIdentifier" #6

Closed more7dev closed 3 weeks ago

more7dev commented 3 weeks ago

I`m trying to use recast-x to parse JS code and I encountered an error:

Error: did not recognize object of type "PrivateIdentifier" at Object.getFieldNames (/path/to/project/node/node_modules/recast-x/node_modules/ast-types/lib/types.js:670:19) at getSortedChildNodes (/path/to/project/node/node_modules/recast-x/lib/comments.js:54:23) at getSortedChildNodes (/path/to/project/node/node_modules/recast-x/lib/comments.js:63:9) at decorateComment (/path/to/project/node/node_modules/recast-x/lib/comments.js:71:22) at decorateComment (/path/to/project/node/node_modules/recast-x/lib/comments.js:83:13) at decorateComment (/path/to/project/node/node_modules/recast-x/lib/comments.js:83:13) at decorateComment (/path/to/project/node/node_modules/recast-x/lib/comments.js:83:13) at /path/to/project/node/node_modules/recast-x/lib/comments.js:120:9 at Array.forEach () at attach (/path/to/project/node/node_modules/recast-x/lib/comments.js:118:14)

Here is how it can be reproduced in the simplest way:

const fs = require("fs");
const recast = require("recast-x");
const espree = require("espree");

let file = fs.openSync("Test.js", "r+");
let code = fs.readFileSync(file, "utf8");

let ast = recast.parse(code, {
    parser: {
        parse: function (source) {
            return espree.parse(source, {
                ecmaVersion: espree.latestEcmaVersion,
                tokens: true,
                loc: true,
                locations: true,
                comment: true,
                onComment: [],
                range: false,
                tolerant: true,
                jsx: false,
            });
        }
    }
});

I have attached file Test.js to parse. Test.js.zip

more7dev commented 3 weeks ago

Just to clarify the code example: this error occurs during parsing any file in which the comment exists inside private class method:

class Test {
    #privateMethod() {
        //comment in private method
    }
}

Without the comment it would be ok.

pionxzh commented 3 weeks ago

Can try recast-x v1.0.5

more7dev commented 3 weeks ago

I can confirm that it works in v1.0.5. Thank you for very fast fix.