How to use the acorn-jsx.parse function in acorn-jsx

To help you get started, we’ve selected a few acorn-jsx 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 fourcube / unused / src / lib.js View on Github external
fs.readFile(path, {encoding: 'utf-8'}, function(err, data) {
    if(err) {
      return cb(err, null);
    }
    // first apply a jsx transformation
    data = jsx.transform(data, {
      harmony: true,
      es6module: true
    });

    cb(null, acorn.parse(data, {
      sourceType: 'module',
      ecmaVersion: "6",
      locations: true,
      plugins: {
        'jsx': true
      }
    }));
  });
}
github zaach / jsxgettext / lib / jsxgettext.js View on Github external
sourceType: 'module',
      plugins: { jsx: { allowNamespaces: false } },
      onComment: function (block, text, start, end, line/*, column*/) {
        text = text.match(commentRegex) && text.replace(/^\//, '').trim();

        if (!text)
          return;

        astComments.push({
          line : line,
          value: text
        });
      },
      locations: true
    }, options.parserOptions && JSON.parse(options.parserOptions));
    var ast      = parser.parse(source, parserOptions);

    // finds comments that end on the previous line
    function findComments(comments, line) {
      return comments.map(function (node) {
        var commentLine = node.line.line;
        if (commentLine === line || commentLine + 1 === line) {
          return node.value;
        }
      }).filter(Boolean).join('\n');
    }

     walk.simple(ast, {'CallExpression': function (node) {
        var arg = getTranslatable(node, options);
        if (!arg)
          return;
github fourcube / unused / lib / lib.js View on Github external
fs.readFile(path, { encoding: 'utf-8' }, function (err, data) {
    if (err) {
      return cb(err, null);
    }
    // first apply a jsx transformation
    data = jsx.transform(data, {
      harmony: true,
      es6module: true
    });

    cb(null, acorn.parse(data, {
      sourceType: 'module',
      ecmaVersion: '6',
      locations: true,
      plugins: {
        'jsx': true
      }
    }));
  });
}
github phenomejs / phenome / test-acorn / compile.js View on Github external
function createSlot(node, parentNode) {
    let slotName = 'default';
    node.openingElement.attributes.forEach((attr) => {
      if (attr.name.name === 'name') slotName = attr.name.name;
    });
    const slotNode = acorn.parse(`this.slots.${slotName}`).body[0].expression;
    const children = node.children;
    delete node.children;
    delete node.openingElement;
    delete node.closingElement;
    Object.assign(node, slotNode);

    let index = 0;
    const parentElements = parentNode.arguments || parentNode.children || parentNode.elements;

    children.forEach((slotChild) => {
      let child = slotChild;
      if (child.type === 'JSXText') {
        child = transformJSXText(slotChild);
        if (!child) return;
      }
      const slotChildNode = acorn.parse(`!this.slots.${slotName} && child`).body[0].expression;
github phenomejs / phenome / test-acorn / compile.js View on Github external
node.declaration.properties.forEach((prop) => {
        if (prop.key.name === 'render' && prop.method === true) {
          const createElementNode = acorn.parse('var _h = this.$createElement;').body[0];
          prop.value.body.body.unshift(createElementNode);
        }
      });
    }
github phenomejs / phenome / test-acorn / compile.js View on Github external
function createSlot(node) {
    let slotName = 'default';
    node.openingElement.attributes.forEach((attr) => {
      if (attr.name.name === 'name') slotName = attr.name.name;
    });
    const children = [];
    node.children.forEach((slotChild) => {
      let child = slotChild;
      if (child.type === 'JSXText') {
        child = transformJSXText(slotChild);
        if (!child) return;
      }
      children.push(slotChild);
    });
    const slotNode = acorn.parse(`this.slots.${slotName}${children.length ? ' || []' : ''}`).body[0].expression;
    if (children.length) {
      slotNode.right.elements = children;
    }
    delete node.children;
    delete node.openingElement;
    delete node.closingElement;
    Object.assign(node, slotNode);
  }
  function transformJSXElement(node, parentNode) {
github mukeshsoni / frolic / app / compilers / react / react.js View on Github external
return new Promise((resolve, reject) => {
        try {
            var ast = acorn.parse(code, {plugins: {jsx: true}})
            var tokens = ast.body.map((node) => ({
                type: node.type,
                node,
                codeString: escodegen.generate(node)
            }))
            resolve(tokens)
        } catch(e) {
            console.log('error parsing code', e.toString())
            reject(e)
        }
    })
}
github SBoudrias / AST-query / lib / tree.js View on Github external
function Tree(source, escodegenOptions, acornOptions) {
  this.acornOptionDefaults = _.extend({}, acornOptionDefaults, acornOptions);
  this.comments = [];
  this.tokens = [];
  this.acornOptionDefaults.onComment = this.comments;
  this.acornOptionDefaults.onToken = this.tokens;
  this.tree = acorn.parse(source.toString(), this.acornOptionDefaults);
  this.tree = escodegen.attachComments(this.tree, this.comments, this.tokens);
  this.body = new Body(this.tree.body, this.acornOptionDefaults);
  this.escodegenOptions = _.extend({}, escodegenOptionDefaults, escodegenOptions);
}
github mukeshsoni / frolic / app / compilers / react-compiler / index.js View on Github external
return new Promise((resolve, reject) => {
        try {
            const ast = acorn.parse(code, {
                            plugins: {jsx: true},
                            sourceType: 'module',
                        })
            resolve('No Errors')
        } catch (e) {
            reject(e)
        }
    })
}
github treycordova / nativejsx / source / nativejsx.js View on Github external
.indexOf(safe.declarationType) !== -1

  allocator.reset()
  allocator.VARIABLE_PREFIX = typeof safe.variablePrefix === 'string'
    ? safe.variablePrefix
    : allocator.VARIABLE_PREFIX

  generators.DECLARATION_TYPE = isValidDeclarationType
    ? safe.declarationType
    : generators.DECLARATION_TYPE

  transformers.INLINE_NATIVEJSX_HELPERS = safe.prototypes === 'inline'
  transformers.MODULE_NATIVEJSX_HELPERS = safe.prototypes === 'module'

  const state = {transformed: false}
  const ast = acorn.parse(jsx, safe.acorn)

  walk.simple(ast, transformers, walker, state)

  if (state.transformed) {
    if (transformers.INLINE_NATIVEJSX_HELPERS) {
      ast.body = [
        setAttributesAST,
        setStylesAST,
        appendChildrenAST
      ].concat(ast.body)
    }

    return escodegen.generate(ast)
  } else {
    return jsx
  }

acorn-jsx

Modern, fast React.js JSX parser

MIT
Latest version published 3 years ago

Package Health Score

74 / 100
Full package analysis

Similar packages