branch: master commit 397ef04ca3999a4070fbad48c0a153696e31087d Author: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com> Commit: Jackson Ray Hamilton <jack...@jacksonrayhamilton.com>
Detect globals. --- tokenizer/cli.js | 27 ++++++++++++++++++++++++--- 1 files changed, 24 insertions(+), 3 deletions(-) diff --git a/tokenizer/cli.js b/tokenizer/cli.js index 457ca74..9ac8262 100644 --- a/tokenizer/cli.js +++ b/tokenizer/cli.js @@ -16,16 +16,37 @@ process.stdin.on('readable', function () { }); process.stdin.on('end', function () { - var tokens; + var data, tokens; // Generate a syntax tree for the input. JSLINT(whole); + data = JSLINT.data(); // Minimize an otherwise-circular structure. - tokens = JSLINT.data().tokens.map(function (token) { + tokens = data.tokens.map(function (token) { + var origin = token, + level; + + // We always consider the function keyword to be "part" of the scope it + // creates, even if the name leaks in the case of function statements. + if (token.kind !== 'function') { + // Find a variable/parameter's origin. + while (origin.master) { + origin = origin.master; + } + } + + // Globality is not indicated by origin function. + if (token.kind !== 'function' && + data.global.indexOf(token.string) > -1) { + level = 0; + } else { + level = origin.function.level; + } + return { from: token.from, - level: token.function.level, + level: level, line: token.line, thru: token.thru };