Skip to content

Commit

Permalink
Merge pull request #346 from danger/babel7plus
Browse files Browse the repository at this point in the history
Bring back the typescript compiler
  • Loading branch information
orta committed Aug 19, 2017
2 parents 9dba62f + 0ea1d9b commit 2fcf881
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 3 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,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
happy to keep Danger in a pretty long alpha, till at least Babel 7 is in beta.

It also still supports using TypeScript via the "`typescript"` module, if you have that installed. - orta

### 2.0.0-alpha.8

Expand Down
2 changes: 1 addition & 1 deletion dangerfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { distanceInWords } from "date-fns"

// For some reason we're getting type errors on this includes module?
// Wonder if we could move to the includes function in ES2015?
import includes from "lodash.includes"
import * as includes from "lodash.includes"
const sentence = danger.utils.sentence

schedule(async () => {
Expand Down
21 changes: 17 additions & 4 deletions source/runner/DangerfileRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,28 @@ import { DangerContext } from "../runner/Dangerfile"

import { NodeVM, NodeVMOptions } from "vm2"

let hasNativeTypeScript = false
let hasBabel = false
let hasTypeScript = false
let hasBabelTypeScript = 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
hasNativeTypeScript = 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
hasBabelTypeScript = true
} catch (e) {} // tslint:disable-line

try {
Expand Down Expand Up @@ -60,8 +66,9 @@ export async function createDangerfileRuntimeEnvironment(dangerfileContext: Dang
function compile(code: string, filename: string) {
const filetype = path.extname(filename)
let result = code

if (hasBabel && hasTypeScript && !filename.includes("node_modules") && filetype.startsWith(".ts")) {
if (hasNativeTypeScript && !filename.includes("node_modules") && filetype.startsWith(".ts")) {
result = typescriptify(code)
} else if (hasBabel && hasBabelTypeScript && !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, hasFlow ? ["transform-flow-strip-types"] : [])
Expand Down Expand Up @@ -168,6 +175,12 @@ 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, extraPlugins: string[]): string => {
const babel = require("babel-core") // tslint:disable-line
const options = babel.loadOptions({})
Expand Down

0 comments on commit 2fcf881

Please sign in to comment.