How to use the babel-core.transformFromAst function in babel-core

To help you get started, we’ve selected a few babel-core 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 tmpfs / trucks / test / spec / compiler / empty-attribute.js View on Github external
it('should generate AST for element with empty attribute', function(done) {
    const tpl = '<template id="x-foo">'
      + '<span></span></template>';

    const res = trucks.compile(tpl);

    expect(res.list).to.be.an('array').to.have.length(1);

    // component id
    expect(res.list[0].id).to.eql('x-foo');

    // function body AST
    expect(res.list[0].body).to.be.an('object');

    const result = babel.transformFromAst(res.list[0].body);
    expect(result.code).to.eql('skate.vdom.element("span", {\n'
      + '  "icon": ""\n'
      + '});');

    done();
  });
github tmpfs / trucks / test / spec / compiler / normalize.js View on Github external
const tpl = '<template id="x-foo">'
      + '  <span></span>'
      + '  <em></em>'
      + '</template>';

    const res = trucks.compile(tpl, {normalize: false});

    expect(res.list).to.be.an('array').to.have.length(1);

    // component id
    expect(res.list[0].id).to.eql('x-foo');

    // function body AST
    expect(res.list[0].body).to.be.an('object');

    const result = babel.transformFromAst(res.list[0].body);
    expect(result.code).to.eql(
      'skate.vdom.text("  ");\n' + 
      'skate.vdom.element("span");\n' + 
      'skate.vdom.text("  ");\n' + 
      'skate.vdom.element("em");' 
    );

    done();
  });
github VadimDez / ngx-img-fallback / node_modules / systemjs-builder / compilers / esm.js View on Github external
moduleIds: !opts.anonymous,
      moduleId: !opts.anonymous && load.name,
      code: true,
      resolveModuleSource: function(dep) {
        if (opts.normalize)
          return load.depMap[dep];
        else
          return dep;
      }
    };

    var source = load.metadata.originalSource || load.source;

    var output;
    if (load.metadata.ast)
      output = babel.transformFromAst(load.metadata.ast, source, babelOptions);
    else
      output = babel.transform(source, babelOptions);

    // NB this can be removed with merging of ()
    if (opts.systemGlobal != 'System')
      output.code = output.code.replace(/(\s|^)System\.register\(/, '$1' + opts.systemGlobal + '.register(');

    // for some reason Babel isn't respecting sourceFileName...
    if (output.map)
      output.map.sources[0] = load.path;

    return Promise.resolve({
      source: output.code,
      sourceMap: output.map
    });
  }
github tomchentw / babel-multi-env / src / buildSwitcher.js View on Github external
export function compileAst(ast, babelCoreOpts, watch = false) {
  try {
    return transformFromAst(ast, babelCoreOpts);
  } catch (err) {
    if (watch) {
      console.error(err);
      return { ignored: true };
    } else {
      throw err;
    }
  }
}
github Tencent / omi / packages / omip / my-app / scripts / taro-cli / src / h5.js View on Github external
function processOthers (code, filePath, fileType) {
  let ast = wxTransformer({
    code,
    sourcePath: filePath,
    isNormal: true,
    isTyped: Util.REG_TYPESCRIPT.test(filePath),
    adapter: 'h5'
  }).ast
  let taroImportDefaultName
  let hasAddNervJsImportDefaultName = false
  let hasJSX = false
  let isPage = fileType === FILE_TYPE.PAGE
  let hasComponentDidMount = false
  let hasComponentDidShow = false

  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-danger-remove-unused-import'), { ignore: ['@tarojs/taro', 'react', 'nervjs'] }]
    ]
  }).ast

  const ClassDeclarationOrExpression = {
    enter (astPath) {
      const node = astPath.node
      if (!node.superClass) return
      if (
        node.superClass.type === 'MemberExpression' &&
        node.superClass.object.name === taroImportDefaultName
      ) {
        node.superClass.object.name = taroImportDefaultName
        if (node.id === null) {
          const renameComponentClassName = '_TaroComponentClass'
github mattzeunert / FromJS / src / compilation / processJavaScriptCode.js View on Github external
return function processJavaScriptCode(code, options){
        code = removeSourceMapIfAny(code)

        const ast = babylon.parse(code, {
            strict: false,
            allowReturnOutsideFunction: true,
            sourceFilename: options !== undefined ? options.filename + ".dontprocess" : undefined
        });

        babelPlugin.sourceCode = code;

        var res = babel.transformFromAst(ast, code, {
            sourceMaps: true,
            compact: false,
            plugins: [
                sharedPlugin,
                babelPlugin
            ]
        });
        res.map.sourcesContent = undefined

        delete babelPlugin.sourceCode

        return res
    }
}
github rtsao / polypackage / packages / polypackage-core / src / stringify-entry-ast.js View on Github external
function stringifyCommonJSEntry(exportStatement) {
  return babel.transformFromAst({
    type: 'File',
    program: {
      type: 'Program',
      sourceType: 'module',
      body: [exportStatement]
    }
  }).code;
}
github Tencent / omi / packages / omip / my-app / scripts / taro-cli / src / weapp.js View on Github external
function parseComponentExportAst (ast, componentName, componentPath, componentType) {
  let componentRealPath = null
  let importExportName
  ast = babel.transformFromAst(ast, '', {
    plugins: [
      [require('babel-plugin-transform-define').default, constantsReplaceList]
    ]
  }).ast
  traverse(ast, {
    ExportNamedDeclaration (astPath) {
      const node = astPath.node
      const specifiers = node.specifiers
      const source = node.source
      if (source && source.type === 'StringLiteral') {
        specifiers.forEach(specifier => {
          const exported = specifier.exported
          if (_.kebabCase(exported.name) === componentName) {
            componentRealPath = Util.resolveScriptPath(path.resolve(path.dirname(componentPath), source.value))
          }
        })
github plasma-umass / Stopify / stopify-continuations-compiler / src / index.ts View on Github external
export function compileFromAst(src: babel.types.Program): h.Result {
    try {
        const babelOpts = {
            plugins: [ [ () =&gt; ({ visitor }), {
                reserved: [],
                global: t.memberExpression(t.identifier('$rts'), t.identifier('g'))
            } ] ],
            babelrc: false,
            ast: false,
            code: true,
            minified: false,
            comments: false,
        };

        const result = babel.transformFromAst(src, undefined, babelOpts);
        if (typeof result.code === 'string') {
            return h.ok(result.code);
        }
        else {
            return h.error('compile failed: no code returned');
        }
    }
    catch (exn) {
        return h.error(exn.toString());
    }
}
github plasma-umass / Stopify / src / helpers.ts View on Github external
plugs.forEach(trs => {
    const res = babel.transformFromAst(ast, code, {
      plugins: [...trs],
      babelrc: false,
    });
    code = res.code;
    ast = res.ast;
  });