estools / estraverse

ECMAScript JS AST traversal functions
BSD 2-Clause "Simplified" License
941 stars 131 forks source link

Unknown node type #121

Closed Nefcanto closed 1 year ago

Nefcanto commented 1 year ago

Hello,

This is my Menu.jsx content:

import FingerprintIcon from '@mui/icons-material/Fingerprint'

const ModulesMenu = [
    {
        title: "Entity Types",
        icon: FingerprintIcon,
        path: "/entityTypes",
        superAdmin: true
    }
]

export default ModulesMenu

And this is the AST generated for this, using nightly-esprima:

{
    "type": "Program",
    "body": [
        {
            "type": "ImportDeclaration",
            "specifiers": [
                {
                    "type": "ImportDefaultSpecifier",
                    "local": {
                        "type": "Identifier",
                        "name": "FingerprintIcon"
                    }
                }
            ],
            "source": {
                "type": "Literal",
                "value": "@mui/icons-material/Fingerprint",
                "raw": "'@mui/icons-material/Fingerprint'"
            }
        },
        {
            "type": "VariableDeclaration",
            "declarations": [
                {
                    "type": "VariableDeclarator",
                    "id": {
                        "type": "Identifier",
                        "name": "ModulesMenu"
                    },
                    "init": {
                        "type": "ArrayExpression",
                        "elements": [
                            {
                                "type": "ObjectExpression",
                                "properties": [
                                    {
                                        "type": "Property",
                                        "key": {
                                            "type": "Identifier",
                                            "name": "title"
                                        },
                                        "computed": false,
                                        "value": {
                                            "type": "Literal",
                                            "value": "Entity Types",
                                            "raw": "\"Entity Types\""
                                        },
                                        "kind": "init",
                                        "method": false,
                                        "shorthand": false
                                    },
                                    {
                                        "type": "Property",
                                        "key": {
                                            "type": "Identifier",
                                            "name": "icon"
                                        },
                                        "computed": false,
                                        "value": {
                                            "type": "Identifier",
                                            "name": "FingerprintIcon"
                                        },
                                        "kind": "init",
                                        "method": false,
                                        "shorthand": false
                                    },
                                    {
                                        "type": "Property",
                                        "key": {
                                            "type": "Identifier",
                                            "name": "path"
                                        },
                                        "computed": false,
                                        "value": {
                                            "type": "Literal",
                                            "value": "/entityTypes",
                                            "raw": "\"/entityTypes\""
                                        },
                                        "kind": "init",
                                        "method": false,
                                        "shorthand": false
                                    },
                                    {
                                        "type": "Property",
                                        "key": {
                                            "type": "Identifier",
                                            "name": "superAdmin"
                                        },
                                        "computed": false,
                                        "value": {
                                            "type": "Literal",
                                            "value": true,
                                            "raw": "true"
                                        },
                                        "kind": "init",
                                        "method": false,
                                        "shorthand": false
                                    }
                                ]
                            }
                        ]
                    }
                }
            ],
            "kind": "const"
        },
        {
            "type": "ExportDefaultDeclaration",
            "declaration": {
                "type": "Identifier",
                "name": "ModulesMenu"
            }
        }
    ],
    "sourceType": "module"
}

When I try this code on this AST, I get unknown node type error:

    estraverse.traverse(ast, {
        enter: function (node, parent) {
            console.log(node.type)
        },
    });

This is the full error stack:

undefined
Error: Unknown node type null.
    at Controller.traverse (/Temp/Temp/node_modules/estraverse/estraverse.js:469:31)
    at Object.traverse (/Temp/Temp/node_modules/estraverse/estraverse.js:670:27)
    at Object.<anonymous> (/Temp/Temp/LocalizationHelper.js:13:16)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:86:12)
    at node:internal/main/run_main_module:23:47

What is wrong here?

michaelficarra commented 1 year ago

If you want to traverse JSX nodes or any other nodes that are not JS nodes, you must use the custom visitor keys feature. See the README for documentation.