Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.

Commit

Permalink
Add @babel/plugin-syntax*
Browse files Browse the repository at this point in the history
  • Loading branch information
AsaAyers committed Nov 8, 2018
1 parent 251d0fe commit ba84f1f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 61 deletions.
89 changes: 28 additions & 61 deletions lib/core/parse-code.js
Expand Up @@ -52,40 +52,40 @@ function findIdentifiers(node, identifiers = []) {

export default function parseCode(code: string): Info {
const { traverse, types: t } = require("@babel/core")
const { Hub, NodePath } = traverse
const { parse } = require("@babel/core")
let ast = undefined

try {
ast = parse(code, {
sourceType: "module",
// TODO: Figure out the new parameters for parse. This needs to be able to
// parse EVERYTHING that is supported by Babel. Versions <= 6 didn't
// require installing additional packages, but now maybe it does. I
// haven't found a complete list of available plugins.
//
// From the FAQ https://babeljs.io/docs/en/babel-parser
//
// Q: Will the Babel parser support a plugin system?
//
// A: We currently aren't willing to commit to supporting the API for
// plugins or the resulting ecosystem (there is already enough work
// maintaining Babel's own plugin system). ...
//
// So what is `babel-plugin-flow` if the parser doesn't support plugins?
// Enable as many plugins as I can so that people don't need to configure
// anything.
plugins: [
// Error: Cannot find module 'babel-plugin-flow' from '/home/asa/repos/js-hyperclick'
"jsx",
"flow",
"doExpressions",
"objectRestSpread",
"decorators",
"classProperties",
"exportExtensions",
"asyncGenerators",
"functionBind",
"functionSent",
"dynamicImport",
"@babel/plugin-syntax-async-generators",
"@babel/plugin-syntax-bigint",
"@babel/plugin-syntax-class-properties",
["@babel/plugin-syntax-decorators", { decoratorsBeforeExport: false }],
"@babel/plugin-syntax-do-expressions",
"@babel/plugin-syntax-dynamic-import",
"@babel/plugin-syntax-export-default-from",
"@babel/plugin-syntax-export-namespace-from",
"@babel/plugin-syntax-flow",
"@babel/plugin-syntax-function-bind",
"@babel/plugin-syntax-function-sent",
"@babel/plugin-syntax-import-meta",
"@babel/plugin-syntax-json-strings",
"@babel/plugin-syntax-jsx",
"@babel/plugin-syntax-logical-assignment-operators",
"@babel/plugin-syntax-nullish-coalescing-operator",
"@babel/plugin-syntax-numeric-separator",
"@babel/plugin-syntax-object-rest-spread",
"@babel/plugin-syntax-optional-catch-binding",
"@babel/plugin-syntax-optional-chaining",
["@babel/plugin-syntax-pipeline-operator", { proposal: "minimal" }],
"@babel/plugin-syntax-throw-expressions",
// Even though Babel can parse typescript, I can't have it and flow
// enabled at the same time.
// "@babel/plugin-syntax-typescript",
],
})
} catch (parseError) {
Expand Down Expand Up @@ -326,40 +326,7 @@ export default function parseCode(code: string): Info {
}

try {
// TODO: Figure out the Babel7 solution. Hub doesn't exist now and was a
// workaround for https://github.com/babel/babel/issues/4640
const hub = new Hub({
buildCodeFrameError(node, message, Error) {
const loc = node && (node.loc || node._loc)

/* istanbul ignore else */
if (loc) {
const err = new Error(
`${message} (${loc.start.line}:${loc.start.column})`,
)
err[parseErrorTag] = true
return err
} else {
// Assume if we don't have a location, then it isn't a problem
// in the user's code and shouldn't be displayed as a parse
// error.
return new Error(message)
}
},
})
const path = NodePath.get({
hub: hub,
parentPath: null,
parent: ast,
container: ast,
key: "program",
}).setContext()

// On order to capture duplicate declaration errors I have to build a
// scope to get access to a "hub" that returns the error I need to catch.
//
// https://github.com/babel/babel/issues/4640
traverse(ast, visitors, path.scope)
traverse(ast, visitors)
} catch (e) {
/* istanbul ignore else */
if (e[parseErrorTag]) {
Expand Down
22 changes: 22 additions & 0 deletions package.json
Expand Up @@ -33,6 +33,28 @@
},
"dependencies": {
"@babel/core": "^7.1.5",
"@babel/plugin-syntax-async-generators": "^7.0.0",
"@babel/plugin-syntax-bigint": "^7.0.0",
"@babel/plugin-syntax-class-properties": "^7.0.0",
"@babel/plugin-syntax-decorators": "^7.1.0",
"@babel/plugin-syntax-do-expressions": "^7.0.0",
"@babel/plugin-syntax-dynamic-import": "^7.0.0",
"@babel/plugin-syntax-export-default-from": "^7.0.0",
"@babel/plugin-syntax-export-namespace-from": "^7.0.0",
"@babel/plugin-syntax-flow": "^7.0.0",
"@babel/plugin-syntax-function-bind": "^7.0.0",
"@babel/plugin-syntax-function-sent": "^7.0.0",
"@babel/plugin-syntax-import-meta": "^7.0.0",
"@babel/plugin-syntax-json-strings": "^7.0.0",
"@babel/plugin-syntax-jsx": "^7.0.0",
"@babel/plugin-syntax-logical-assignment-operators": "^7.0.0",
"@babel/plugin-syntax-nullish-coalescing-operator": "^7.0.0",
"@babel/plugin-syntax-numeric-separator": "^7.0.0",
"@babel/plugin-syntax-object-rest-spread": "^7.0.0",
"@babel/plugin-syntax-optional-catch-binding": "^7.0.0",
"@babel/plugin-syntax-optional-chaining": "^7.0.0",
"@babel/plugin-syntax-pipeline-operator": "^7.0.0",
"@babel/plugin-syntax-throw-expressions": "^7.0.0",
"acorn-to-esprima": "^1.0.4",
"atom-package-deps": "^4.6.0",
"escope": "^3.2.0",
Expand Down

0 comments on commit ba84f1f

Please sign in to comment.