acorn-umd
Parse acorn ast for AMD, CommonJS, and ES6 definitions
Last updated 5 years ago by megawac .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install acorn-umd 
SYNC missed versions from official npm registry.

acorn-umd

Parse acorn ast for AMD, CommonJS, and ES6 definitions.

Travis build status Test Coverage Code Climate Dependency Status devDependency Status

The Nodes created align as closely as possible with the nodes generated for acorns ES6 import nodes. Example below

var acorn = require('acorn');
var umd = require('acorn-umd');

var code = `
    import {a, b, c as d} from 'library';
    import foo from 'foo-library';
    
    let _ = require('lodash');
`;

var ast = acorn.parse(code, {ecmaVersion: 6});
var imports = umd(ast, {
    es6: true, amd: false, cjs: true
});

console.log(imports);
[
  { type: 'CJSImport',
    reference: 
     { type: 'VariableDeclaration',
       start: 87,
       end: 113,
       declarations: [Object],
       kind: 'let' },
    specifiers: [ [Object] ],
    start: 87,
    end: 113,
    source: 
     { type: 'Literal',
       reference: [Object],
       value: 'lodash',
       raw: '\'lodash\'',
       start: 103,
       end: 111 }
  },
  { type: 'ImportDeclaration',
    start: 5,
    end: 42,
    specifiers: [ [Object], [Object], [Object] ],
    source: 
     { type: 'Literal',
       start: 32,
       end: 41,
       value: 'library',
       raw: '\'library\'' }
  },
  { type: 'ImportDeclaration',
    start: 47,
    end: 77,
    specifiers: [ [Object] ],
    source: 
     { type: 'Literal',
       start: 63,
       end: 76,
       value: 'foo-library',
       raw: '\'foo-library\'' }
  }
]

AMD Imports

let code = `
  foo();
  define(['foo', 'unused-import'], function($) {
      return $();
  });
`;

let ast = acorn.parse(code, {ecmaVersion: 6});
let parsed = umd(ast, {
  es6: false, amd: true, cjs: false
});

console.log(parsed);

[
 {
  type: 'AMDImport',
  reference: { DEFINE_NODE },
  start: 12,
  end: 81,

  specifiers: [ { type: 'Identifier', start: 54, end: 55, name: '$' } ],
  sources: 
   [ { type: 'Literal',
       reference: [Object],
       value: 'foo',
       raw: '\'foo\'',
       start: 20,
       end: 25 },
     { type: 'Literal',
       reference: [Object],
       value: 'unused-import',
       raw: '\'unused-import\'',
       start: 27,
       end: 42 } ],

  // Grouped [source, variable] name array
  imports: [ [ {SOURCE_NODE}, {VARIABLE_NODE} ], [ {SOURCE_NODE}, undefined ] 
 }
]

Get the scope of a node

import {sample} from 'lodash'

let parsed = umd(ast, {
  es6: false, amd: true, cjs: false
});

let node = sample(parsed);

let scope = node.scope; // scope is null if global otherwise a function node

Current Tags

  • 0.4.0                                ...           latest (5 years ago)

9 Versions

  • 0.4.0                                ...           5 years ago
  • 0.3.2                                ...           5 years ago
  • 0.3.1                                ...           5 years ago
  • 0.3.0                                ...           5 years ago
  • 0.2.2                                ...           5 years ago
  • 0.2.1                                ...           5 years ago
  • 0.2.0                                ...           5 years ago
  • 0.1.0                                ...           5 years ago
  • 0.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 57
Dependencies (4)
Dev Dependencies (18)

Copyright 2014 - 2016 © taobao.org |