]>
code.delx.au - gnu-emacs-elpa/blob - scopifier.js
5 var escope
= require('escope'),
6 esprima
= require('esprima'),
9 process
.stdin
.setEncoding('utf8');
11 process
.stdin
.on('readable', function () {
12 var chunk
= process
.stdin
.read();
18 process
.stdin
.on('end', function () {
26 // Gracefully handle parse errors by doing nothing.
28 ast
= esprima
.parse(whole
, {
32 analyzedScopes
= escope
.analyze(ast
).scopes
;
37 analyzedScopes
.forEach(function (scope
) {
38 if (scope
.level
=== undefined) {
40 if (scope
.upper
.functionExpressionScope
) {
41 // Pretend function expression scope doesn't exist.
42 scope
.level
= scope
.upper
.level
;
43 scope
.variables
= scope
.upper
.variables
.concat(scope
.variables
);
45 scope
.level
= scope
.upper
.level
+ 1;
50 if (scope
.functionExpressionScope
) {
51 // We've only given the scope a level for posterity's sake.
59 scope
.variables
.forEach(function (variable
) {
62 variable
.defs
.forEach(function (definition
) {
63 var range
= definition
.name
.range
;
70 variable
.references
.forEach(function (reference
) {
71 var range
= reference
.identifier
.range
,
72 isDefined
= definitions
.some(function (definition
) {
73 // Check for identical definitions.
74 return definition
[1] === range
[0] &&
75 definition
[2] === range
[1];
86 Array
.prototype.push
.apply(symbols
, definitions
);
87 Array
.prototype.push
.apply(symbols
, references
);
89 scope
.references
.forEach(function (reference
) {
91 if (reference
.resolved
) {
94 // Handle global references.
95 range
= reference
.identifier
.range
;
105 ast
.comments
.forEach(function (comment
) {
106 var range
= comment
.range
;
114 emacsified
= scopes
.concat(symbols
.concat(comments
));
116 emacsified
.forEach(function (instruction
) {
117 // Emacs starts counting from 1.
122 console
.log(JSON
.stringify(emacsified
));