escodegen: this[type] is not a function

I’m need to modify js file. So I parse my JSX with esprima, make some changes and then I’m trying to code AST into js. But I have this error this[type] is not a function

Code

import React from 'react';
import { Route, IndexRoute } from 'react-router';
import { Map } from 'immutable';

import App from './containers/App';

export const pages = new Map({
    index: '/'

});

/*eslint-disable react/jsx-max-props-per-line*/
/*eslint-disable react/jsx-sort-props*/
/*eslint-disable max-len*/
export default (
    <Route path = { pages.get('index') } component = { App }>
        <IndexRoute component = { App } />
    </Route>
    
);

Parsing file:

        const file = fs.readFileSync(routes, 'utf8');
        const parsedFile = esprima.parse(file, {
            sourceType: 'module',
            jsx: true,
            comment: true
        });
        let token = false;
        for (let i = 0; i < parsedFile.body.length; i++) {
            if (parsedFile.body[i].type === 'ExportNamedDeclaration') {
                const declarations = parsedFile.body[i].declaration.declarations;

                for (let j = 0; j < declarations.length; j++) {
                    if (declarations[j].id.name === 'pages') {
                        const properties = declarations[j].init.arguments[0].properties;
                        properties.push({
                            type: 'Property',
                            key: {type: 'Identifier', name: 'test'},
                            computed: false,
                            value: {type: 'Literal', value: '/test'},
                            kind: 'init',
                            method: false,
                            shorthand: false
                        });
                    }
                }
            }
            token = true;
        }

Escoding:

        const code = escodegen.generate(parsedFile, {
            format: {
                indent: {
                    style: '    ',
                    base: 0,
                    adjustMultilineComment: true
                },
                newline: '\n',
                space: ' ',
                json: false,
                renumber: false,
                hexadecimal: false,
                quotes: 'single',
                escapeless: false,
                compact: false,
                parentheses: true,
                semicolons: true,
                safeConcatenation: false
            },
            moz: {
                starlessGenerator: false,
                parenthesizedComprehensionBlock: false,
                comprehensionExpressionStartsWithAssignment: false
            },
            parse: null,
            comment: true,
            sourceMap: undefined,
            sourceMapRoot: null,
            sourceMapWithCode: false,
            file: undefined,
            directive: false,
            verbatim: undefined
        });

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 7
  • Comments: 22 (9 by maintainers)

Most upvoted comments

Any update on this issue? Do you plan to resolve it soon?

Would we be able to sync this bad boy in here? https://github.com/wallabyjs/escodegen

I’m not actively working on it.