-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtooling.lsc
More file actions
93 lines (77 loc) · 3.06 KB
/
tooling.lsc
File metadata and controls
93 lines (77 loc) · 3.06 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// API for external tools, such as the linter, that share functionality with
// the compiler.
import { Options, compilerOptions, isNonceFilename } from './state/options'
import parseConfigurationDirectives from './util/parseConfigurationDirectives'
import * as parser from "@lightscript/parser"
import { lodashMethodMap, inlinedOperatorMap } from './state/imports'
import packageMetadata from '../package.json'
findBabelConfig = require('find-babel-config')
findConfigEntry(babelConfig, type, pluginName) ->
for elem p in (babelConfig[type] or []):
if p == pluginName:
return {}
elif p?[0] == pluginName:
return p[1]
tryConfigEntries(babelConfig, list) ->
if not babelConfig: return { config: {} }
let x
for elem [ type, pluginName ] in list:
now x = findConfigEntry(babelConfig, type, pluginName)
if x: return { source: type, config: x }
{ config: {} }
// Given a file path, locate the applicable .babelrc and extract the options
// pertinent to the LightScript plugin.
export locateBabelConfig(filePath) ->
// Check if filePath is nonsense
if filePath~isNonceFilename(): return null
// Use find-babel-config
confData = findBabelConfig.sync(filePath)
confData.config
export getPluginConfig(babelConfig) ->
babelConfig~tryConfigEntries([
["presets", "@lightscript"]
["presets", "module:@lightscript/babel-preset"]
["plugins", "module:@lightscript/transform"]
])
// Obtain compiler configuration information using the same algorithms used
// by the compiler itself. Loads .babelrc and configuration directives.
export getCompilerConfiguration(filePath, code, opts = {}, underride = {}) ->
{ config, source } = getPluginConfig(locateBabelConfig(filePath))
// Apply default preset decorator config
if source == "presets":
if not config.decoratorOpts:
config.decoratorOpts = { legacy: true }
// Merge special opts
for key k, val v in opts:
if (k != "preset" and k != "plugin"):
config[k] = v
for key k, val v in underride:
if not config[k]: config[k] = v
compilerOpts = new Options(null)
compilerOpts.setOptions({ filename: filePath }, opts.parserOpts, config)
directiveOpts = parseConfigurationDirectives(code)
compilerOpts.setPreParseOptions(null, directiveOpts)
compilerOpts
// Parse code, including configuration directives, generating parser options
// the same way the compiler does. Returns a babylon ast.
export parse(code, compilerConfig) ->
parser.parse(code, compilerConfig.getParserOptions())
// Export medatada in lscdiag format
export getCompilerMetadata() -> {
metadataVersion: 4
name: packageMetadata.name
version: packageMetadata.version
parser
parse
getCompilerConfiguration
options: compilerOptions
}
// Linter needs to know about implicit imports
export isPotentialImplicitLodashImport(compilerConfig, name): boolean ->
if compilerConfig.lodashEnabled():
if lodashMethodMap[name]: return true
false
export isPotentialInlinedOperator(compilerConfig, name): boolean ->
if compilerConfig.inlinedOperatorsEnabled():
if inlinedOperatorMap[name]: return true
false