How to use the @babel/generator.default function in @babel/generator

To help you get started, we’ve selected a few @babel/generator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github erukiti / ast-book-sample / chapter2 / try-babel-generator.js View on Github external
const generate = require('@babel/generator').default

const ast = {
  type: 'ExpressionStatement',
  expression: {
    type: 'BinaryExpression', 
    operator: '+',
    left: {type: 'NumericLiteral', value: 1},
    right: {type: 'NumericLiteral', value: 2}
  }
}

const {code, map} = generate(ast)
console.log(code)
// 1 + 2;
github ismail-codar / fidan / packages / babel-plugin-fidan-jsx / build / ast.js View on Github external
const errorReport = (e, path, file) => {
    const nodeCode = generator_1.default(path.node).code;
    console.log("FILE: ", file.filename);
    console.log("PART: ", nodeCode);
    console.error("ERROR: ", e);
    debugger;
};
function setAttr(elem, name, value) {
github cube-js / cube.js / packages / cubejs-schema-compiler / compiler / DataSchemaCompiler.js View on Github external
transpileFile(file, errorsReport) {
    try {
      const ast = parse(
        file.content,
        {
          sourceFilename: file.fileName,
          sourceType: 'module',
          plugins: ['objectRestSpread']
        },
      );
      this.transpilers.forEach((t) => babelTraverse(ast, t.traverseObject()));
      const content = babelGenerator(ast, {}, file.content).code;
      return Object.assign({}, file, { content });
    } catch (e) {
      if (e.toString().indexOf('SyntaxError') !== -1) {
        const line = file.content.split('\n')[e.loc.line - 1];
        const spaces = Array(e.loc.column).fill(' ').join('');
        errorsReport.error(`Syntax error during '${file.fileName}' parsing: ${e.message}:\n${line}\n${spaces}^`);
      } else {
        errorsReport.error(e);
      }
    }
    return undefined;
  }
github parcel-bundler / parcel / packages / core / parcel-bundler / src / scope-hoisting / concat.js View on Github external
if (packager.options.minify) {
          mangleScope(path.scope);
        }
      },
    },
  });

  let opts = {
    sourceMaps: packager.options.sourceMaps,
    sourceFileName: packager.bundle.name,
    minified: packager.options.minify,
    comments: !packager.options.minify,
  };

  return generate(ast, opts);
};
github crubier / code-to-graph / transform.js View on Github external
)
        ];
        return {
          nodes: [thisNode, ...consequentNodes, ...alternateNodes],
          edges: [...thisEdges, ...consequentEdges, ...alternateEdges],
          entryNodes: [thisNode],
          exitNodes: [...consequentExitNodes, ...alternateExitNodes],
          breakNodes: [...consequentBreakNodes, ...alternateBreakNodes],
          subGraphs: []
        };
      }
      case "SwitchStatement":
        {
          const thisNode = cleanGraphNode({
            id: makeIdFromAstNode(statement),
            name: `switch ${generate(statement.discriminant).code} `,
            shape: "rhombus"
          });

          const scopeGraph = fp.reduce(
            (
              { nodes, edges, entryNodes, exitNodes, breakNodes },
              caseAstElement
            ) => {
              const {
                nodes: caseNodes,
                edges: caseEdges,
                entryNodes: caseEntryNodes,
                exitNodes: caseExitNodes,
                breakNodes: caseBreakNodes
              } = transformGeneralAstToGraph(caseAstElement.consequent);
              const caseEntryEdges = fp.map(
github accforgit / TransVue2Ts / commonutil.js View on Github external
const generateFn = node => {
  return generate(node, {
    comments: false
  }).code
}
github crubier / code-to-graph / transform.js View on Github external
];
        return {
          nodes: [thisNode, ...bodyNodes],
          edges: [...thisEdges, ...bodyEdges],
          entryNodes: [thisNode],
          exitNodes: [...bodyBreakNodes, thisNode],
          breakNodes: [],
          subGraphs: []
        };
      }
      case "ForOfStatement":
      case "ForStatement": {
        const thisNode = cleanGraphNode({
          id: makeIdFromAstNode(statement),
          name: `for ${generate(statement.init).code} ; ${
            generate(statement.test).code
          } ; ${generate(statement.update).code}`,
          shape: "rhombus"
        });
        const {
          nodes: bodyNodes,
          edges: bodyEdges,
          entryNodes: bodyEntryNodes,
          exitNodes: bodyExitNodes,
          breakNodes: bodyBreakNodes
        } = transformGeneralAstToGraph(statement.body);

        const thisEdges = [
          ...fp.map(
            node => ({
              from: thisNode.id,
              to: node.id,
github Foveluy / react-wechat / lib / convert.js View on Github external
//递归的`{foo+1+3+4}px`=>>"{{foo+1+3+4}}px"
        if (t.isBinaryExpression(exp)) {
          let small = ''
          small = ConvertBinaryExpression(small, exp)

          value += `{{${small}}}`
        } else {
          //正常的`{foo}px`=>>"{{foo}}px"
          value += `{{${exp.name}}}`
        }
      }
    })
    return value
  }

  return isNumber ? generate(node).code + 'px' : node.value
}
github xtuc / webassemblyjs / packages / wasm-text-gen / src / printers / javascript.js View on Github external
function genTemplate(fn, opts) {
  const ast = fn(opts);
  return generate(ast).code;
}
github niksy / modernizr-esm / build / source-entries.js View on Github external
async renderChunk(source, options) {
			const ast = parse(source, {
				sourceType: 'module'
			});
			traverse(ast, babelPlugin().visitor);
			traverse(ast, handleGlobalReference().visitor);
			const result = generate(ast);
			return `/** Original source code: https://github.com/Modernizr/Modernizr/blob/v${version}/src/${options.fileName} **/\n${result.code}`;
		}
	}

@babel/generator

Turns an AST into code.

MIT
Latest version published 18 days ago

Package Health Score

95 / 100
Full package analysis

Similar packages