@friday-friday/luast-util-scope
v0.3.0
Published
Scope analysis for luast trees — identify local and global identifiers
Downloads
77
Maintainers
Readme
luast-util-scope
Scope analysis for luast trees — identify local and global identifiers.
What is this?
This package walks a luast syntax tree and determines which identifiers are
local and which are global. It understands all Lua scoping rules: local
declarations, function parameters, for loop variables, and nested block
scopes.
Install
npm install @friday-friday/luast-util-scopeUse
import luaparse from '@friday-friday/luaparse'
import {analyzeScope} from '@friday-friday/luast-util-scope'
const tree = luaparse.parse('local x = 1; print(x)', {ast: 'luast'})
const scope = analyzeScope(tree)
console.log(scope.globals)
// => [{type: 'identifier', name: 'print', …}]
console.log(scope.isLocal(tree.body[1].expression.base))
// => false (print is global)API
analyzeScope(tree)
Analyze scopes in a luast Root tree.
Returns a ScopeInfo object:
globals— array ofIdentifiernodes that are globalisLocal(node)— returnstrueif the givenIdentifiernode is local
Related
@friday-friday/luast— tree specification and types@friday-friday/luast-util-visit— tree visitor@friday-friday/luaparse— Lua parser
