lexical-scope-1.2.0/000077500000000000000000000000001257706207400142545ustar00rootroot00000000000000lexical-scope-1.2.0/.travis.yml000066400000000000000000000000541257706207400163640ustar00rootroot00000000000000language: node_js node_js: - 0.8 - 0.10 lexical-scope-1.2.0/LICENSE000066400000000000000000000020611257706207400152600ustar00rootroot00000000000000This software is released under the MIT license: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. lexical-scope-1.2.0/example/000077500000000000000000000000001257706207400157075ustar00rootroot00000000000000lexical-scope-1.2.0/example/detect.js000066400000000000000000000002541257706207400175160ustar00rootroot00000000000000var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/src.js'); var scope = detect(src); console.log(JSON.stringify(scope,null,2)); lexical-scope-1.2.0/example/src.js000066400000000000000000000005271257706207400170400ustar00rootroot00000000000000var x = 5; var y = 3, z = 2; w.foo(); w = 2; RAWR=444; RAWR.foo(); BLARG=3; foo(function () { var BAR = 3; process.nextTick(function (ZZZZZZZZZZZZ) { console.log('beep boop'); var xyz = 4; x += 10; x.zzzzzz; ZZZ=6; }); function doom () { } ZZZ.foo(); }); console.log(xyz); lexical-scope-1.2.0/index.js000066400000000000000000000137751257706207400157360ustar00rootroot00000000000000var astw = require('astw'); module.exports = function (src) { var locals = {}; var implicit = {}; var exported = {}; var implicitProps = {}; if (typeof src === 'string') { src = String(src).replace(/^#![^\n]*\n/, ''); } if (src && typeof src === 'object' && typeof src.copy === 'function' && typeof src.toString === 'function') { src = src.toString('utf8'); } var walk = astw(src); walk(function (node) { if (node.type === 'VariableDeclaration') { // take off the leading `var ` var id = getScope(node); for (var i = 0; i < node.declarations.length; i++) { var d = node.declarations[i]; locals[id][d.id.name] = d; } } else if (node.type === 'CatchClause') { var id = getScope(node); locals[id][node.param.name] = node.param } else if (isFunction(node)) { var id = getScope(node.parent); if (node.id) locals[id][node.id.name] = node; var nid = node.params.length && getScope(node); if (nid && !locals[nid]) locals[nid] = {}; for (var i = 0; i < node.params.length; i++) { var p = node.params[i]; locals[nid][p.name] = p; } } }); walk(function (node) { if (node.type === 'Identifier' && lookup(node) === undefined) { if (node.parent.type === 'Property' && node.parent.key === node) return; if (node.parent.type === 'MemberExpression' && node.parent.property === node) return; if (isFunction(node.parent)) return; if (node.parent.type === 'LabeledStatement') return; if (node.parent.type === 'ContinueStatement') return; if (node.parent.type === 'BreakStatement') return; if (node.parent.type === 'AssignmentExpression') { var isLeft0 = node.parent.left.type === 'MemberExpression' && node.parent.left.object === node.name ; var isLeft1 = node.parent.left.type === 'Identifier' && node.parent.left.name === node.name ; if (isLeft0 || isLeft1) { exported[node.name] = keyOf(node).length; } } if (!exported[node.name] || exported[node.name] < keyOf(node).length) { implicit[node.name] = keyOf(node).length; if (!implicitProps[node.name]) implicitProps[node.name] = {}; if (node.parent && node.parent.type === 'MemberExpression' && node.parent.property.type === 'Identifier') { implicitProps[node.name][node.parent.property.name] = true; } else if (node.parent && node.parent.type === 'CallExpression' && node.parent.callee === node) { implicitProps[node.name]['()'] = true; } else { implicitProps[node.name]['*'] = true; } } } }); var localScopes = {}; var lks = objectKeys(locals); for (var i = 0; i < lks.length; i++) { var key = lks[i]; localScopes[key] = objectKeys(locals[key]); } var props = {}; var pkeys = objectKeys(implicitProps); for (var i = 0; i < pkeys.length; i++) { props[pkeys[i]] = objectKeys(implicitProps[pkeys[i]]); } return { locals: localScopes, globals: { implicit: objectKeys(implicit), implicitProperties: props, exported: objectKeys(exported) } }; function lookup (node) { for (var p = node; p; p = p.parent) { if (isFunction(p) || p.type === 'Program') { var id = getScope(p); if (locals[id][node.name]) { return id; } } } return undefined; } function getScope (node) { for ( var p = node; !isFunction(p) && p.type !== 'Program'; p = p.parent ); var id = idOf(p); if (!locals[id]) locals[id] = {}; return id; } }; function isFunction (x) { return x.type === 'FunctionDeclaration' || x.type === 'FunctionExpression' ; } function idOf (node) { var id = []; for (var n = node; n.type !== 'Program'; n = n.parent) { var key = keyOf(n).join('.'); id.unshift(key); } return id.join('.'); } function keyOf (node) { if (node.lexicalScopeKey) return node.lexicalScopeKey; var p = node.parent; var ks = objectKeys(p); var kv = { keys : [], values : [], top : [] }; for (var i = 0; i < ks.length; i++) { var key = ks[i]; kv.keys.push(key); kv.values.push(p[key]); kv.top.push(undefined); if (isArray(p[key])) { var keys = objectKeys(p[key]); kv.keys.push.apply(kv.keys, keys); kv.values.push.apply(kv.values, p[key]); var nkeys = []; for (var j = 0; j < keys.length; j++) nkeys.push(key); kv.top.push.apply(kv.top, nkeys); } } var ix = indexOf(kv.values, node); var res = []; if (kv.top[ix]) res.push(kv.top[ix]); if (kv.keys[ix]) res.push(kv.keys[ix]); if (node.parent.type === 'CallExpression') { res.unshift.apply(res, keyOf(node.parent.parent)); } return node.lexicalScopeKey = res; } var isArray = Array.isArray || function (xs) { return Object.prototype.toString.call(xs) === '[object Array]'; }; var objectKeys = Object.keys || function (obj) { var keys = []; for (var key in obj) keys.push(key); return keys; }; function indexOf (xs, x) { if (xs.indexOf) return xs.indexOf(x); for (var i = 0; i < xs.length; i++) { if (x === xs[i]) return i; } return -1; } lexical-scope-1.2.0/package.json000066400000000000000000000020571257706207400165460ustar00rootroot00000000000000{ "name": "lexical-scope", "version": "1.2.0", "description": "detect global and local lexical identifiers from javascript source code", "main": "index.js", "dependencies": { "astw": "^2.0.0" }, "devDependencies": { "tape": "~2.4.1", "brfs": "~0.0.3" }, "scripts": { "test": "tape test/*.js" }, "testling": { "files": "test/*.js", "browsers": [ "ie/6", "ie/7", "ie/8", "ie/9", "ie/10", "chrome/20", "chrome/latest", "firefox/10", "firefox/15", "firefox/latest", "safari/latest", "opera/11.0", "opera/latest" ] }, "repository": { "type": "git", "url": "git://github.com/substack/lexical-scope.git" }, "homepage": "https://github.com/substack/lexical-scope", "keywords": [ "ast", "variable", "name", "lexical", "local", "global", "implicit", "exported" ], "author": { "name": "James Halliday", "email": "mail@substack.net", "url": "http://substack.net" }, "license": "MIT" } lexical-scope-1.2.0/readme.markdown000066400000000000000000000052761257706207400172670ustar00rootroot00000000000000# lexical-scope detect global and local lexical identifiers from javascript source code [![browser support](http://ci.testling.com/substack/lexical-scope.png)](http://ci.testling.com/substack/lexical-scope) [![build status](https://secure.travis-ci.org/substack/lexical-scope.png)](http://travis-ci.org/substack/lexical-scope) # example ``` js var detect = require('lexical-scope'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/src.js'); var scope = detect(src); console.log(JSON.stringify(scope,null,2)); ``` input: ``` var x = 5; var y = 3, z = 2; w.foo(); w = 2; RAWR=444; RAWR.foo(); BLARG=3; foo(function () { var BAR = 3; process.nextTick(function (ZZZZZZZZZZZZ) { console.log('beep boop'); var xyz = 4; x += 10; x.zzzzzz; ZZZ=6; }); function doom () { } ZZZ.foo(); }); console.log(xyz); ``` output: ``` $ node example/detect.js { "locals": { "": [ "x", "y", "z" ], "body.7.expression.body.7.arguments.0": [ "BAR", "doom" ], "body.7.expression.body.7.arguments.0.body.body.1.expression.body.1.arguments.0": [ "xyz", "ZZZZZZZZZZZZ" ], "body.7.expression.body.7.arguments.0.body.body.2": [] }, "globals": { "implicit": [ "w", "foo", "process", "console", "xyz" ], "implicitProperties": { "w": [ "foo" ], "foo": [ "()" ], "process": [ "nextTick" ], "console": [ "log" ], "xyz": [ "*" ] }, "exported": [ "w", "RAWR", "BLARG", "ZZZ" ] } } ``` # live demo If you are using a modern browser, you can go to http://lexical-scope.forbeslindesay.co.uk/ for a live demo. # methods ``` js var detect = require('lexical-scope') ``` ## var scope = detect(src) Return a `scope` structure from a javascript source string `src`. `scope.locals` maps scope name keys to an array of local variable names declared with `var`. The key name `''` refers to the top-level scope. `scope.globals.implicit` contains the global variable names that are expected to already exist in the environment by the script. `scope.globals.explicit` contains the global variable names that are exported by the script. `scope.globals.implicitProperties` contains the properties of global variable names that have been used. There are two special implicit property names: * `"()"` - when an implicit variable has been called * `"*"` - when an implicit variable has been used in a context that is not a property and not a call # install With [npm](https://npmjs.org) do: ``` npm install lexical-scope ``` # license MIT lexical-scope-1.2.0/test/000077500000000000000000000000001257706207400152335ustar00rootroot00000000000000lexical-scope-1.2.0/test/argument.js000066400000000000000000000007201257706207400174120ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/argument.js'); test('parameters from inline arguments', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, []); t.same(scope.globals.exported, []); t.same(scope.locals, { 'body.0': [ 'a' ], '': [ 'foo' ], 'body.0.body.body.1.argument': [ 'c' ] }); }); lexical-scope-1.2.0/test/assign_implicit.js000066400000000000000000000006051257706207400207500ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/assign_implicit.js'); test('assign from an implicit global', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, [ 'bar' ]); t.same(scope.globals.exported, []); t.same(scope.locals, { '': [ 'foo' ] }); }); lexical-scope-1.2.0/test/detect.js000066400000000000000000000007561257706207400170510ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/detect.js'); test('check locals and globals', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, [ 'w', 'foo', 'process', 'console', 'AAA', 'BBB', 'CCC', 'xyz' ]); t.same(scope.globals.exported, [ 'w', 'RAWR', 'BLARG', 'ZZZ' ]); t.same(scope.locals[''], [ 'x', 'y', 'z', 'beep' ]); }); lexical-scope-1.2.0/test/files/000077500000000000000000000000001257706207400163355ustar00rootroot00000000000000lexical-scope-1.2.0/test/files/argument.js000066400000000000000000000001171257706207400205140ustar00rootroot00000000000000function foo () { var a; return function (c) { a = c; }; } lexical-scope-1.2.0/test/files/assign_implicit.js000066400000000000000000000000241257706207400220450ustar00rootroot00000000000000var foo; foo = bar; lexical-scope-1.2.0/test/files/buffer_call.js000066400000000000000000000000331257706207400211330ustar00rootroot00000000000000console.log(Buffer('abc')) lexical-scope-1.2.0/test/files/buffer_isbuffer.js000066400000000000000000000000511257706207400220250ustar00rootroot00000000000000console.log(Buffer.isBuffer('whatever')) lexical-scope-1.2.0/test/files/buffer_var.js000066400000000000000000000000241257706207400210100ustar00rootroot00000000000000console.log(Buffer) lexical-scope-1.2.0/test/files/detect.js000066400000000000000000000006701257706207400201460ustar00rootroot00000000000000var x = 5; var y = 3, z = 2; w.foo(); w = 2; RAWR=444; RAWR.foo(); BLARG=3; foo(function () { var BAR = 3; process.nextTick(function (ZZZZZZZZZZZZ) { console.log('beep boop'); var xyz = 4; x += 10; x.zzzzzz; ZZZ=6; }); function doom () { if (AAA.aaa) {} BBB.bbb = 3; var z = 2 + CCC.x * 5; } ZZZ.foo(); }); function beep () {} console.log(xyz); lexical-scope-1.2.0/test/files/labels.js000066400000000000000000000002571257706207400201410ustar00rootroot00000000000000test_label1: while(true) { break test_label1; continue test_label1; } function nest() { test_label2: while(true) { break test_label2; continue test_label2; } };lexical-scope-1.2.0/test/files/multiple-exports.js000066400000000000000000000001341257706207400222260ustar00rootroot00000000000000exports.foo = function () { return bar; }; exports.bar = function (bar) { return bar; };lexical-scope-1.2.0/test/files/named_arg.js000066400000000000000000000001251257706207400206060ustar00rootroot00000000000000function foo (x) { var a = x; return function (c) { a += c; }; } lexical-scope-1.2.0/test/files/obj.js000066400000000000000000000000341257706207400174420ustar00rootroot00000000000000module.exports = {foo: bar} lexical-scope-1.2.0/test/files/return_hash.js000066400000000000000000000000601257706207400212110ustar00rootroot00000000000000function foo() { return { bar: true } } lexical-scope-1.2.0/test/files/right_hand.js000066400000000000000000000000741257706207400210030ustar00rootroot00000000000000exports.filename = __filename; exports.dirname = __dirname; lexical-scope-1.2.0/test/files/try_catch.js000066400000000000000000000000741257706207400206540ustar00rootroot00000000000000function foo() { try { } catch (ex) { foo(ex) } }lexical-scope-1.2.0/test/labels.js000066400000000000000000000006371257706207400170410ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/labels.js'); test('globals on the right-hand of a colon in an object literal', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, []); t.same(scope.globals.exported, []); t.same(scope.locals, { '': [ 'nest' ], 'body.1': [] }); }); lexical-scope-1.2.0/test/multiple-exports.js000066400000000000000000000007421257706207400211310ustar00rootroot00000000000000 var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/multiple-exports.js'); test('multiple-exports', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit.sort(), ['bar', 'exports'].sort()); t.same(scope.globals.exported, []); t.same(scope.locals, { '':[], 'body.1.expression.right': ['bar'], 'body.0.expression.right': [] }); }); lexical-scope-1.2.0/test/named_arg.js000066400000000000000000000007161257706207400175120ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/named_arg.js'); test('named argument parameter', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, []); t.same(scope.globals.exported, []); t.same(scope.locals, { 'body.0': [ 'a', 'x' ], '': [ 'foo' ], 'body.0.body.body.1.argument': [ 'c' ] }); }); lexical-scope-1.2.0/test/obj.js000066400000000000000000000006731257706207400163510ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/obj.js'); test('globals on the right-hand of a colon in an object literal', function (t) { t.plan(3); var scope = detect(src); t.same( scope.globals.implicit.sort(), [ 'bar', 'module' ].sort() ); t.same(scope.globals.exported, []); t.same(scope.locals, { '': [] }); }); lexical-scope-1.2.0/test/package.json000066400000000000000000000000541257706207400175200ustar00rootroot00000000000000{ "browserify": { "transform": "brfs" } } lexical-scope-1.2.0/test/props.js000066400000000000000000000023511257706207400167350ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = { call: fs.readFileSync(__dirname + '/files/buffer_call.js'), isbuffer: fs.readFileSync(__dirname + '/files/buffer_isbuffer.js'), v: fs.readFileSync(__dirname + '/files/buffer_var.js') }; test('implicit props: call', function (t) { t.plan(3); var scope = detect(src.call); t.deepEqual(scope.locals, { '': [] }); t.deepEqual(scope.globals.implicit, [ 'console', 'Buffer' ]); t.deepEqual(scope.globals.implicitProperties, { console: [ 'log' ], Buffer: [ '()' ] }) }); test('implicit props: isBuffer', function (t) { t.plan(3); var scope = detect(src.isbuffer); t.deepEqual(scope.locals, { '': [] }); t.deepEqual(scope.globals.implicit, [ 'console', 'Buffer' ]); t.deepEqual(scope.globals.implicitProperties, { console: [ 'log' ], Buffer: [ 'isBuffer' ] }) }); test('implicit props: var', function (t) { t.plan(3); var scope = detect(src.v); t.deepEqual(scope.locals, { '': [] }); t.deepEqual(scope.globals.implicit, [ 'console', 'Buffer' ]); t.deepEqual(scope.globals.implicitProperties, { console: [ 'log' ], Buffer: [ '*' ] }) }); lexical-scope-1.2.0/test/return_hash.js000066400000000000000000000005651257706207400201210ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/return_hash.js'); test('return hash', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, []); t.same(scope.globals.exported, []); t.same(scope.locals, { 'body.0': [], '': [ 'foo' ] }); }); lexical-scope-1.2.0/test/right_hand.js000066400000000000000000000007051257706207400177020ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/right_hand.js'); test('globals on the right-hand of assignment', function (t) { t.plan(3); var scope = detect(src); t.same( scope.globals.implicit.sort(), [ 'exports', '__dirname', '__filename' ].sort() ); t.same(scope.globals.exported, []); t.same(scope.locals, { '': [] }); }); lexical-scope-1.2.0/test/shebang.js000066400000000000000000000007731257706207400172070ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = '#!/beep/boop blah\n' + fs.readFileSync(__dirname + '/files/detect.js') ; test('shebangs', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, [ 'w', 'foo', 'process', 'console', 'AAA', 'BBB', 'CCC', 'xyz' ]); t.same(scope.globals.exported, [ 'w', 'RAWR', 'BLARG', 'ZZZ' ]); t.same(scope.locals[''], [ 'x', 'y', 'z', 'beep' ]); }); lexical-scope-1.2.0/test/try_catch.js000066400000000000000000000006331257706207400175530ustar00rootroot00000000000000var test = require('tape'); var detect = require('../'); var fs = require('fs'); var src = fs.readFileSync(__dirname + '/files/try_catch.js'); test('the exception in a try catch block is a local', function (t) { t.plan(3); var scope = detect(src); t.same(scope.globals.implicit, []); t.same(scope.globals.exported, []); t.same(scope.locals, { '': [ 'foo' ], 'body.0': [ 'ex' ] }); });