Skip to content

Commit

Permalink
Tooling API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
wcjohnson committed Oct 9, 2017
1 parent 09ccb87 commit 4b6ee6b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 14 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"@oigroup/babylon-lightscript": "3.0.0-alpha.6",
"@oigroup/lightscript-runtime": "^0.1.1",
"ast-loc-utils": "^1.1.0",
"babel-types": "^6.25.0"
"babel-types": "^6.25.0",
"find-babel-config": "^1.1.0"
},
"files": [
"lib"
Expand Down
13 changes: 8 additions & 5 deletions src/config.lsc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as babylonLightScript from "@oigroup/babylon-lightscript";
import packageMetadata from "../package.json";
import * as babylonLightScript from "@oigroup/babylon-lightscript"
import packageMetadata from "../package.json"
import parseConfigurationDirectives from './util/parseConfigurationDirectives'

export getMetadata() -> {
Expand Down Expand Up @@ -74,9 +74,8 @@ export getMetadata() -> {
}
}

getFileTypeInfo(file) ->
if not file?.opts?.filename: return { isLightScript: true, isLSX: false }
filename = file.opts.filename
export getFileTypeInfoFromFileName(filename) ->
if not filename: return { isLightScript: true, isLSX: false }

if filename.includes(".lsx"): return { isLightScript: true, isLSX: true }

Expand All @@ -88,6 +87,10 @@ getFileTypeInfo(file) ->

return { isLightScript: false, isLSX: false }

getFileTypeInfo(file) ->
if not file?.opts?.filename: return { isLightScript: true, isLSX: false }
getFileTypeInfoFromFileName(file.opts.filename)

export getParserOpts(pluginOpts, initialParserOpts) ->
parserOpts = initialParserOpts or {}
parserOpts.parser = babylonLightScript.parse
Expand Down
55 changes: 47 additions & 8 deletions src/tooling.lsc
Original file line number Diff line number Diff line change
@@ -1,20 +1,59 @@
// API for external tools, such as the linter, that share functionality with
// the compiler.
import parseConfigurationDirectives from './util/parseConfigurationDirectives'
import { getFileTypeInfoFromFileName, getParserOpts } from './config'
import * as babylon from "@oigroup/babylon-lightscript"

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]

// Given a file path, locate the applicable .babelrc and extract the options
// pertinent to the LightScript plugin.
locateBabelConfig(filePath) ->
export locateBabelConfig(filePath) ->
// Check if filePath is nonsense
if (not filePath) or filePath == "unknown" or filePath == "repl":
return null
// Use find-babel-config
// Extract compiler options from either preset or plugin if they exist.
undefined
confData = findBabelConfig.sync(filePath)
confData.config

export getPluginConfig(babelConfig, preset = "@oigroup/babel-preset-lightscript", plugin = "@oigroup/babel-plugin-lightscript") ->
if not babelConfig: return {}
let entry = babelConfig~findConfigEntry("presets", preset)
if not entry: now entry = babelConfig~findConfigEntry("plugins", plugin)

entry or {}

// Obtain compiler configuration information using the same algorithms used
// by the compiler itself. Loads .babelrc and configuration directives.
export getCompilerConfiguration(filePath, code) ->
undefined
export getCompilerConfiguration(filePath, code, opts = {}) ->
pluginOpts = getPluginConfig(locateBabelConfig(filePath), opts.preset, opts.plugin)

// Merge special opts
for key k, val v in opts:
if (k != "preset" and k != "plugin"):
pluginOpts[k] = v

fileTypeInfo = getFileTypeInfoFromFileName(filePath)
if fileTypeInfo.isLightScript and (pluginOpts.isLightScript == undefined):
pluginOpts.isLightScript = true

directiveOpts = parseConfigurationDirectives(code)
Object.assign(pluginOpts, directiveOpts)

pluginOpts

// Parse code, including configuration directives, generating parser options
// the same way the compiler does.
export parse(code, baseParserOpts, baseCompilerConfig) ->
undefined
// the same way the compiler does. Returns a babylon ast.
export parse(compilerConfig, code, baseParserOpts) ->
parserOpts = getParserOpts(compilerConfig, baseParserOpts)
parserOpts.parser(code, parserOpts)

// Let tools have access to babylon if they need it.
export { babylon }
24 changes: 24 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
var locateBabelConfig = require("../lib/tooling").locateBabelConfig
var getPluginConfig = require("../lib/tooling").getPluginConfig

require("babel-helper-plugin-test-runner")(__dirname);

// Tooling API tests
// XXX: make this a lot better
function assert(fn, msg) {
console.log()
if (!fn()) {
if (msg)
throw new Error(msg);
else
throw new Error("Assertion failed");
} else {
console.log("✔︎ " + fn.toString())
}
}

assert(() => locateBabelConfig(".").plugins[0][0] === "@oigroup/babel-plugin-lightscript-self-host")
assert(() => {
var config = locateBabelConfig(".");
var pluginConfig = getPluginConfig(config, "@oigroup/babel-preset-lightscript-self-host", "@oigroup/babel-plugin-lightscript-self-host");
return (pluginConfig.existential === true)
})

0 comments on commit 4b6ee6b

Please sign in to comment.