-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_expr.js
More file actions
33 lines (33 loc) · 1006 Bytes
/
Copy pathdebug_expr.js
File metadata and controls
33 lines (33 loc) · 1006 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const ts = require('typescript')
const src = `
const count = 5
console.log(count)
count = 10
`
const sourceFile = ts.createSourceFile('logic.ts', src, ts.ScriptTarget.Latest, true)
const scopeStack = [new Set()]
const isShadowed = (name) => {
for (let i = scopeStack.length - 1; i >= 0; i--) {
if (scopeStack[i].has(name)) return true
}
return false
}
const visit = (node) => {
let pushedScope = false
if (ts.isBlock(node) || ts.isSourceFile(node) || ts.isFunctionDeclaration(node) || ts.isArrowFunction(node)) {
scopeStack.push(new Set())
pushedScope = true
}
if (ts.isVariableDeclaration(node) && ts.isIdentifier(node.name)) {
scopeStack[scopeStack.length - 1].add(node.name.text)
}
if (ts.isBinaryExpression(node)) {
const left = node.left
if (ts.isIdentifier(left)) {
console.log('Binary expr:', left.text, 'shadowed?', isShadowed(left.text))
}
}
ts.forEachChild(node, visit)
if (pushedScope) scopeStack.pop()
}
visit(sourceFile)