Browse Source

use estree-walker module

declarations-and-references
Rich-Harris 9 years ago
parent
commit
8d093d07d4
  1. 1
      package.json
  2. 4
      src/Module.js
  3. 2
      src/Statement.js
  4. 2
      src/ast/attachScopes.js
  5. 59
      src/ast/walk.js

1
package.json

@ -59,6 +59,7 @@
"dependencies": {
"acorn": "^2.3.0",
"chalk": "^1.0.0",
"estree-walker": "^0.1.3",
"magic-string": "^0.7.0",
"minimist": "^1.1.1",
"sander": "^0.3.3",

4
src/Module.js

@ -1,7 +1,7 @@
import { parse } from 'acorn';
import MagicString from 'magic-string';
import { walk } from 'estree-walker';
import Statement from './Statement';
import walk from './ast/walk';
import { blank, keys } from './utils/object';
import { basename, extname } from './utils/path';
import getLocation from './utils/getLocation';
@ -20,8 +20,6 @@ class SyntheticDefaultDeclaration {
addReference ( reference ) {
reference.declaration = this;
this.name = reference.name;
console.log( 'this.name', this.name )
}
}

2
src/Statement.js

@ -1,4 +1,4 @@
import walk from './ast/walk';
import { walk } from 'estree-walker';
import Scope from './ast/Scope';
import attachScopes from './ast/attachScopes';

2
src/ast/attachScopes.js

@ -1,4 +1,4 @@
import walk from './walk';
import { walk } from 'estree-walker';
import Scope from './Scope';
const blockDeclarations = {

59
src/ast/walk.js

@ -1,59 +0,0 @@
import { blank } from '../utils/object';
let shouldSkip;
let shouldAbort;
export default function walk ( ast, { enter, leave }) {
shouldAbort = false;
visit( ast, null, enter, leave );
}
let context = {
skip: () => shouldSkip = true,
abort: () => shouldAbort = true
};
let childKeys = blank();
let toString = Object.prototype.toString;
function isArray ( thing ) {
return toString.call( thing ) === '[object Array]';
}
function visit ( node, parent, enter, leave ) {
if ( !node || shouldAbort ) return;
if ( enter ) {
shouldSkip = false;
enter.call( context, node, parent );
if ( shouldSkip || shouldAbort ) return;
}
let keys = childKeys[ node.type ] || (
childKeys[ node.type ] = Object.keys( node ).filter( key => typeof node[ key ] === 'object' )
);
let key, value, i, j;
i = keys.length;
while ( i-- ) {
key = keys[i];
value = node[ key ];
if ( isArray( value ) ) {
j = value.length;
while ( j-- ) {
visit( value[j], node, enter, leave );
}
}
else if ( value && value.type ) {
visit( value, node, enter, leave );
}
}
if ( leave && !shouldAbort ) {
leave( node, parent );
}
}
Loading…
Cancel
Save