Skip to content

Commit

Permalink
Feature: add typescript-support/warn
Browse files Browse the repository at this point in the history
- uses console.warn() instead of throwing an error
- useful for development time, when you don't want everything to stop because you have unused variable
  • Loading branch information
AviVahl committed Jul 9, 2018
1 parent d4bbf3a commit 2ab63ce
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/typescript-support/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"prepack": "yarn build"
},
"dependencies": {
"chalk": "^2.4.1",
"node-typescript-service": "^0.4.0",
"source-map-support": "^0.5.6"
},
Expand All @@ -22,7 +21,8 @@
},
"files": [
"cjs",
"src"
"src",
"warn.js"
],
"author": "Avi Vahl <avi.vahl@wix.com>",
"license": "MIT",
Expand Down
6 changes: 6 additions & 0 deletions packages/typescript-support/src/register-warn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { registerNodeExtension, formatDiagnostics } from './register-node-extension'

registerNodeExtension(diagnostics => {
// tslint:disable-next-line:no-console
console.warn(formatDiagnostics(diagnostics))
})
2 changes: 1 addition & 1 deletion packages/typescript-support/test/register-throw.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('using node -r typescript-support [file]', () => {
expect(output).to.include(`at runMe (${fileThatThrows}:11:15)`)
})

it('isolates two folders with different configs', () => {
it('isolates two folders with different configs and throws errors', () => {
const workingFile = join(fixturesDirectory, 'type-isolation', 'with-tsconfig-b', 'passes-type-check.ts')

const { output, exitCode } = runCommand(`node -r typescript-support ${workingFile}`)
Expand Down
69 changes: 69 additions & 0 deletions packages/typescript-support/test/register-warn.unit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { expect } from 'chai'
import { join, dirname } from 'path'
import { platform } from 'os'
import { runCommand } from './run-command'

const fixturesDirectory = dirname(require.resolve('test-fixtures/package.json'))

describe('using node -r typescript-support/warn [file]', () => {
describe('with tsconfig.json', () => {
it('prints syntactic errors', () => {
const fileWithSyntaxError = join(fixturesDirectory, 'errors', 'file-with-syntax-error.ts')

const { output, exitCode } = runCommand(`node -r typescript-support/warn ${fileWithSyntaxError}`)

expect(exitCode).to.equal(0)
expect(output).to.include(`')' expected`)
})

it('prints semantic errors', () => {
const fileWithTypeError = join(fixturesDirectory, 'errors', 'file-with-type-error.ts')

const { output, exitCode } = runCommand(`node -r typescript-support/warn ${fileWithTypeError}`)

expect(exitCode).to.equal(0)
expect(output).to.include(`Type '123' is not assignable to type 'string'`)
})

it('maps stack traces using source maps', () => {
const fileThatThrows = join(fixturesDirectory, 'source-maps', 'with-tsconfig', 'throwing.ts')

const { output, exitCode } = runCommand(`node -r typescript-support/warn ${fileThatThrows}`)

expect(exitCode).to.not.equal(0)
expect(output).to.include(`at runMe (${fileThatThrows}:11:15)`)
})

it('isolates two folders with different configs and prints errors', () => {
const workingFile = join(fixturesDirectory, 'type-isolation', 'with-tsconfig-b', 'passes-type-check.ts')

const { output, exitCode } = runCommand(`node -r typescript-support/warn ${workingFile}`)

expect(exitCode).to.equal(0)
expect(output).to.include(`Cannot find name 'describe'`)
expect(output).to.include('should-error.ts')
})

})

describe('no tsconfig.json', () => {
it('maps stack traces using source maps', () => {
const fileThatThrows = join(fixturesDirectory, 'source-maps', 'throwing-without-tsconfig.ts')

const { output, exitCode } = runCommand(`node -r typescript-support/warn ${fileThatThrows}`)

expect(exitCode).to.not.equal(0)
expect(output).to.include(`at runMe (${fileThatThrows}:9:11)`)
})

it('allows using imports', () => {
const fileWithImports = join(fixturesDirectory, 'no-tsconfig', 'imports.ts')

const { output, exitCode } = runCommand(`node -r typescript-support/warn ${fileWithImports}`)

expect(exitCode).to.equal(0)
expect(output).to.include(`Current platform is: ${platform()}`)
})
})

})
1 change: 1 addition & 0 deletions packages/typescript-support/warn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./cjs/register-warn')

0 comments on commit 2ab63ce

Please sign in to comment.