123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- var _ = require('lodash');
- var tree = require('../lib/tree');
- var treeStructure = require('../structure').scopes;
- module.exports = function(scope) {
-
- if(!scope.ast.params) return;
-
-
- scope.params = scope.ast.params.map(function(p) {
- return {
- name: p.name,
-
-
- };
- });
-
-
-
-
-
- scope.varsDeclared = _.where(scope.flatExp, {type: 'VariableDeclarator'}).map(function(e) {
- return {
- name: e.name,
- ast: e,
-
- };
- });
-
- scope.symsDeclared = _.extend([], scope.varsDeclared, scope.params);
-
- var vars = Object.create(null);
-
- scope.varsRefd = [];
-
- (function harvest(ast) {
- // BUG: this doesn't get vars nested inside member expressions
- extractFirstLayer(ast, ['MemberExpression']).map(function(n) {
- var o = baseObject(n);
-
- if(o.type == 'Identifier' && !vars[o.name]) {
- vars[o.name] = true;
-
- scope.varsRefd.push({
- name: o.name,
- });
-
- return;
- }
-
-
- //console.log(o);
-
- console.log('-------------------------------------------'.blue.bold);
- console.log('harvesting');
- var y = tree.nextLayer(o);
- console.log(y);
- harvest(y);
- console.log('==========================================='.magenta);
- });
-
- })(scope.ast);
-
-
- return scope;
- /*
- { type: 'MemberExpression',
- object:
- { type: 'Identifier',
- name: 't',
- loc: { source: null, start: [Object], end: [Object] } },
- property:
- { type: 'Identifier',
- name: 'u',
- loc: { source: null, start: [Object], end: [Object] } },
- computed: false,
- loc:
- { source: null,
- start: { line: 17, column: 1 },
- end: { line: 17, column: 4 } } }
- { type: 'MemberExpression',
- object:
- { type: 'MemberExpression',
- object: { type: 'Identifier', name: 't', loc: [Object] },
- property: { type: 'Identifier', name: 'u', loc: [Object] },
- computed: false,
- loc: { source: null, start: [Object], end: [Object] } },
- property:
- { type: 'Identifier',
- name: 'k',
- loc: { source: null, start: [Object], end: [Object] } },
- computed: false,
- loc:
- { source: null,
- start: { line: 17, column: 1 },
- end: { line: 17, column: 6 } } }
- */
- };
- function baseObject(exp) {
- if(!exp.object) {
- console.log('!! Missing object '.red, exp);
- return;
- }
-
- // recurse, object is nested
- if(exp.object.type == 'MemberExpression') {
- return baseObject(exp.object);
- }
-
- //if(exp.object.type == 'Identifier' || exp.object.type == 'ThisExpression') {
- return exp.object;
- //}
-
- console.log('!! unexpected path in baseObject. '.red, exp);
- }
- // we need to pull out the 'object' of the deepest nested member expression
- // however, recurse into calculated expressions as other variables may be referenced
- // object references have deeply nested identifier names.
- function collectIdName(id) {
-
- if(id.type == 'Identifier');
-
- console.log(id);
-
- }
- function extractFirstLayer(ast, types) {
-
- if(ast instanceof Array) {
- var t = ast.reduce(function(acc, a) {
- acc.concat(extractFirstLayer(a, types));
- return acc;
- }, []);
- console.log('t:', t);
- return t;
- }
-
- var ts = _.extend({}, treeStructure);
-
- // don't recurse into the node types we are looking for
- types.map(function(t) {
- ts[t] = [];
- });
-
- var crawl = tree.dfSearch(ts);
-
-
- return crawl(ast.body, function(node, acc) {
-
- if(types.indexOf(node.type) > -1) {
- acc.push(node);
- }
-
- return acc;
- }, []);
- };
|