Browse Source

Move `extractNames` to its own file

gh-438-b
Oskar Segersvärd 9 years ago
parent
commit
bb53cbbf02
  1. 2
      src/Module.js
  2. 33
      src/ast/Scope.js
  3. 31
      src/ast/extractNames.js

2
src/Module.js

@ -10,7 +10,7 @@ import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
import { SyntheticDefaultDeclaration, SyntheticNamespaceDeclaration } from './Declaration.js';
import { isFalsy, isTruthy } from './ast/conditions.js';
import { emptyBlockStatement } from './ast/create.js';
import { extractNames } from './ast/Scope.js';
import extractNames from './ast/extractNames.js';
export default class Module {
constructor ({ id, code, originalCode, ast, sourceMapChain, bundle }) {

33
src/ast/Scope.js

@ -1,37 +1,6 @@
import { blank, keys } from '../utils/object.js';
import Declaration from '../Declaration.js';
const extractors = {
Identifier ( names, param ) {
names.push( param.name );
},
ObjectPattern ( names, param ) {
param.properties.forEach( prop => {
extractors[ prop.value.type ]( names, prop.value );
});
},
ArrayPattern ( names, param ) {
param.elements.forEach( element => {
if ( element ) extractors[ element.type ]( names, element );
});
},
RestElement ( names, param ) {
extractors[ param.argument.type ]( names, param.argument );
},
AssignmentPattern ( names, param ) {
extractors[ param.left.type ]( names, param.left );
}
};
export function extractNames ( param ) {
const names = [];
extractors[ param.type ]( names, param );
return names;
}
import extractNames from './extractNames.js';
export default class Scope {
constructor ( options ) {

31
src/ast/extractNames.js

@ -0,0 +1,31 @@
export default function extractNames ( param ) {
const names = [];
extractors[ param.type ]( names, param );
return names;
}
const extractors = {
Identifier ( names, param ) {
names.push( param.name );
},
ObjectPattern ( names, param ) {
param.properties.forEach( prop => {
extractors[ prop.value.type ]( names, prop.value );
});
},
ArrayPattern ( names, param ) {
param.elements.forEach( element => {
if ( element ) extractors[ element.type ]( names, element );
});
},
RestElement ( names, param ) {
extractors[ param.argument.type ]( names, param.argument );
},
AssignmentPattern ( names, param ) {
extractors[ param.left.type ]( names, param.left );
}
};
Loading…
Cancel
Save