Skip to content

Commit

Permalink
Make TypeScript/Flow get optionally applied
Browse files Browse the repository at this point in the history
  • Loading branch information
orta committed Aug 19, 2017
1 parent f9ab02b commit 93657b2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 37 deletions.
11 changes: 2 additions & 9 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
{
"presets": [
"es2015",
"stage-3"
],
"plugins": [
"transform-flow-strip-types",
"syntax-async-functions",
"transform-regenerator"
]
"presets": ["es2015", "stage-3"],
"plugins": ["syntax-async-functions", "transform-regenerator"]
}
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

// TODO

### 2.0.0-alpha.9

* Uses the Babel 7 alpha for all source compilation with JS, Flow+JS and TS. This worked without any changes to our
internal infra which is pretty awesome. All TS tests passed. Babel 7 is still in alpha, but so is Danger 2.0 - so I'm
happy to keep Danger in a pretty long alpha, till at least Babel 7 is in beta. - orta

### 2.0.0-alpha.8

* Uses the GitHub `diff_url` instead of the `diff` version header, as it conflicted with Peril - orta
Expand Down
23 changes: 12 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@
"@types/debug": "0.0.29",
"@types/jest": "^20.0.0",
"@types/node-fetch": "^1.6.6",
"babel-cli": "7.0.0-alpha.19",
"babel-core": "7.0.0-alpha.19",
"babel-plugin-syntax-async-functions": "7.0.0-alpha.19",
"babel-plugin-transform-flow-strip-types": "7.0.0-alpha.19",
"babel-plugin-transform-regenerator": "7.0.0-alpha.19",
"babel-plugin-transform-typescript": "7.0.0-alpha.19",
"babel-polyfill": "7.0.0-alpha.19",
"babel-preset-es2015": "7.0.0-alpha.19",
"babel-preset-stage-3": "7.0.0-alpha.19",
"babel-traverse": "7.0.0-alpha.19",
"danger-plugin-yarn": "^0.2.9",
"date-fns": "^1.28.3",
"husky": "^0.14.0",
Expand All @@ -69,19 +79,9 @@
"ts-node": "^3.2.1",
"tslint": "^5.3.0",
"tslint-config-prettier": "^1.0.0",
"typedoc": "orta/typedoc#0.5.10-no-types",
"typescript": "^2.4.2"
"typedoc": "orta/typedoc#0.5.10-no-types"
},
"dependencies": {
"babel-cli": "7.0.0-alpha.19",
"babel-core": "7.0.0-alpha-19",
"babel-plugin-syntax-async-functions": "7.0.0-alpha.19",
"babel-plugin-transform-flow-strip-types": "7.0.0-alpha.19",
"babel-plugin-transform-regenerator": "7.0.0-alpha.19",
"babel-polyfill": "7.0.0-alpha.19",
"babel-preset-es2015": "7.0.0-alpha.19",
"babel-preset-stage-3": "7.0.0-alpha.19",
"babel-traverse": "7.0.0-alpha-19",
"chalk": "^2.0.0",
"commander": "^2.9.0",
"debug": "^3.0.0",
Expand All @@ -97,6 +97,7 @@
"parse-link-header": "^1.0.1",
"pinpoint": "^1.1.0",
"rfc6902": "^1.3.0",
"typescript": "^2.4.2",
"vm2": "patriksimek/vm2",
"voca": "^1.2.0"
},
Expand Down
36 changes: 21 additions & 15 deletions source/runner/DangerfileRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@ import { DangerContext } from "../runner/Dangerfile"

import { NodeVM, NodeVMOptions } from "vm2"

let hasTypeScript = false
let hasBabel = false
let hasTypeScript = false
let hasFlow = false

declare const regeneratorRuntime: any

// You know you're being a dangerous badass when you have this many linter disables. Deal with it.
try {
require.resolve("typescript") // tslint:disable-line
hasTypeScript = true
} catch (e) {} // tslint:disable-line

try {
require.resolve("babel-core") // tslint:disable-line
require("babel-polyfill") // tslint:disable-line
hasBabel = true

try {
require.resolve("babel-plugin-transform-typescript") // tslint:disable-line
hasTypeScript = true
} catch (e) {} // tslint:disable-line

try {
require.resolve("babel-plugin-transform-flow-strip-types") // tslint:disable-line
hasFlow = true
} catch (e) {} // tslint:disable-line
} catch (e) {} // tslint:disable-line

/**
Expand Down Expand Up @@ -52,10 +60,11 @@ export async function createDangerfileRuntimeEnvironment(dangerfileContext: Dang
function compile(code: string, filename: string) {
const filetype = path.extname(filename)
let result = code
if (hasTypeScript && filetype.startsWith(".ts")) {
result = typescriptify(code)

if (hasBabel && hasTypeScript && !filename.includes("node_modules") && filetype.startsWith(".ts")) {
result = babelify(code, filename, ["transform-typescript"])
} else if (hasBabel && !filename.includes("node_modules") && filetype.startsWith(".js")) {
result = babelify(code, filename)
result = babelify(code, filename, hasFlow ? ["transform-flow-strip-types"] : [])
}

return result
Expand Down Expand Up @@ -159,20 +168,17 @@ export function cleanDangerfile(contents: string): string {
return contents.replace(es6Pattern, "// Removed import").replace(requirePattern, "// Removed require")
}

const typescriptify = (content: string): string => {
const ts = require("typescript") // tslint:disable-line
let result = ts.transpileModule(content, {})
return result.outputText
}

const babelify = (content: string, filename: string): string => {
const babelify = (content: string, filename: string, extraPlugins: string[]): string => {
const babel = require("babel-core") // tslint:disable-line
const options = babel.loadOptions({})

const fileOpts = {
filename,
filenameRelative: filename,
sourceMap: false,
sourceFileName: null,
sourceMapTarget: null,
plugins: [...extraPlugins, ...options.plugins],
}

const result = babel.transform(content, fileOpts)
Expand Down
14 changes: 12 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ babel-code-frame@^6.22.0, babel-code-frame@^6.8.0:
esutils "^2.0.2"
js-tokens "^3.0.0"

babel-core@7.0.0-alpha-19, babel-core@7.0.0-alpha.19:
babel-core@7.0.0-alpha.19:
version "7.0.0-alpha.19"
resolved "https://registry.yarnpkg.com/babel-core/-/babel-core-7.0.0-alpha.19.tgz#6127e8dcbe2fa1ce0864c7698aceed369d3b1b9d"
dependencies:
Expand Down Expand Up @@ -479,6 +479,10 @@ babel-plugin-syntax-optional-catch-binding@7.0.0-alpha.19:
version "7.0.0-alpha.19"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-optional-catch-binding/-/babel-plugin-syntax-optional-catch-binding-7.0.0-alpha.19.tgz#15479086d1c18ef63b02491ff96691116f17bb35"

babel-plugin-syntax-typescript@7.0.0-alpha.19:
version "7.0.0-alpha.19"
resolved "https://registry.yarnpkg.com/babel-plugin-syntax-typescript/-/babel-plugin-syntax-typescript-7.0.0-alpha.19.tgz#fc5876863db297549f5d840d248eac3310241f84"

babel-plugin-transform-async-generator-functions@7.0.0-alpha.19:
version "7.0.0-alpha.19"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-async-generator-functions/-/babel-plugin-transform-async-generator-functions-7.0.0-alpha.19.tgz#9e9b7d0aee6b09f58756c484bf3f472225a551c2"
Expand Down Expand Up @@ -670,6 +674,12 @@ babel-plugin-transform-strict-mode@^6.24.1:
babel-runtime "^6.22.0"
babel-types "^6.24.1"

babel-plugin-transform-typescript@7.0.0-alpha.19:
version "7.0.0-alpha.19"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-typescript/-/babel-plugin-transform-typescript-7.0.0-alpha.19.tgz#9b06c066393d6752e6e539e8b0b5929a78feb5b1"
dependencies:
babel-plugin-syntax-typescript "7.0.0-alpha.19"

babel-plugin-transform-unicode-property-regex@^2.0.2:
version "2.0.3"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-unicode-property-regex/-/babel-plugin-transform-unicode-property-regex-2.0.3.tgz#db13fe12e7380dd6ce0e9206cff3eb4116483ece"
Expand Down Expand Up @@ -802,7 +812,7 @@ babel-traverse@6.12.0:
invariant "^2.2.0"
lodash "^4.2.0"

babel-traverse@7.0.0-alpha-19, babel-traverse@7.0.0-alpha.19:
babel-traverse@7.0.0-alpha.19:
version "7.0.0-alpha.19"
resolved "https://registry.yarnpkg.com/babel-traverse/-/babel-traverse-7.0.0-alpha.19.tgz#083f3f00a413fd9ec38383c0d5ae79511d9ed53d"
dependencies:
Expand Down

0 comments on commit 93657b2

Please sign in to comment.