-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate.lsc
More file actions
64 lines (49 loc) · 2.04 KB
/
state.lsc
File metadata and controls
64 lines (49 loc) · 2.04 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import findBabelOptionsForPlugin from './util/findBabelOptionsForPlugin'
import parseConfigurationDirectives from './util/parseConfigurationDirectives'
import { registrationAPI } from './types'
import { registerLightscriptNodeTypes } from './lscNodeTypes'
import { Options } from './state/options'
import { Imports } from './state/imports'
import { setGlobalState } from './state/globalState'
import { parse } from '@lightscript/parser'
import mainPass from './visitors/main'
export class CompilerState:
constructor(babel) ->
this.babel = babel
this.options = new Options(this)
this.imports = new Imports(this, this.options)
debugDump() ->
rst = Object.assign({}, this)
delete rst.babel
rst
// Babel manipulateOptions hook
// Here we can learn the filename (opts.filename) we're parsing, plus any user
// provided options
manipulateOptions(opts, parserOpts, plugin): void ->
this.options.setOptions(opts, parserOpts, findBabelOptionsForPlugin(opts, plugin))
// Add references to the compiler accessible from the Babel config
opts.lscCompiler = this
parserOpts.lscCompiler = this
// Babel parserOverride hook
// Here we can parse configuration directives from the actual code
parserOverride(code, parserOpts) ->
this.options.setPreParseOptions(parserOpts, parseConfigurationDirectives(code))
lscParserOpts = this.options.getParserOptions()
//console.log("parseOverride", lscParserOpts)
parse(code, lscParserOpts)
// Determine if the file we're working on should be treated as LightScript.
isLightScript(): boolean ->
this.options.isLightScript
// Main program visitor
visitProgram(path): void ->
// Only transform LightScript files.
if not this.isLightScript(): return
// Initialize the state vector
this.programPath = path
this.imports.initialize()
setGlobalState(this)
// Patch the relevant instance of babel-types
{ babel } = this
registerLightscriptNodeTypes(babel.types, registrationAPI)
// Execute compiler passes
mainPass(this, path)