Skip to content

Commit

Permalink
Fix types
Browse files Browse the repository at this point in the history
  • Loading branch information
ai committed Sep 22, 2021
1 parent d6a194d commit d1ba5f9
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 29 deletions.
14 changes: 8 additions & 6 deletions test/browser.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { bold, red, gray, yellow } from 'nanocolors'

import CssSyntaxError from '../lib/css-syntax-error.js'

beforeEach(() => {
jest.resetModules()
jest.doMock('fs', () => ({}))
Expand All @@ -10,17 +12,17 @@ beforeEach(() => {
it('shows code with colors', () => {
let postcss = require('../lib/postcss.js')

let error
let error: CssSyntaxError | undefined
try {
postcss.parse('a{')
} catch (e) {
if (e.name === 'CssSyntaxError') {
if (e instanceof CssSyntaxError) {
error = e
} else {
throw e
}
}
expect(error.showSourceCode(true)).toEqual(
expect(error?.showSourceCode(true)).toEqual(
bold(red('>')) +
gray(' 1 | ') +
'a' +
Expand All @@ -34,17 +36,17 @@ it('shows code with colors', () => {
it('shows code without colors', () => {
let postcss = require('../lib/postcss.js')

let error
let error: CssSyntaxError | undefined
try {
postcss.parse('a{')
} catch (e) {
if (e.name === 'CssSyntaxError') {
if (e instanceof CssSyntaxError) {
error = e
} else {
throw e
}
}
expect(error.showSourceCode(false)).toEqual('> 1 | a{\n' + ' | ^')
expect(error?.showSourceCode(false)).toEqual('> 1 | a{\n' + ' | ^')
})

it('generates source map without fs', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ it('walk() adds CSS position to error stack', () => {
let error = new Error('T')
error.stack = 'Error: T\n at b (b.js:2:4)\n at a (a.js:2:1)'
let root = parse(example, { from: '/c.css' })
let catched
let catched: any
try {
root.walk(() => {
throw error
Expand Down
18 changes: 12 additions & 6 deletions test/css-syntax-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import postcss, {
Rule
} from '../lib/postcss.js'

function isSyntaxError(e: unknown): e is CssSyntaxError {
return e instanceof Error && e.name === 'CssSyntaxError'
}

async function catchError(cb: () => Promise<any>): Promise<CssSyntaxError> {
try {
await cb()
} catch (e) {
if (e.name !== 'CssSyntaxError') throw e
return e
if (isSyntaxError(e)) {
return e
} else {
throw e
}
}
throw new Error('Error was not thrown')
}
Expand All @@ -25,17 +32,16 @@ function parseError(
css: string,
opts?: Pick<ProcessOptions, 'map' | 'from'>
): CssSyntaxError {
let error
try {
postcss.parse(css, opts)
} catch (e) {
if (e.name === 'CssSyntaxError') {
error = e
if (isSyntaxError(e)) {
return e
} else {
throw e
}
}
return error
throw new Error('Error was not thrown')
}

it('saves source', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ it('throws on just colon', () => {
})

it('does not suggest different parsers for CSS', () => {
let error
let error: any
try {
parse('a { one:: 1 }', { from: 'app.css' })
} catch (e) {
Expand Down Expand Up @@ -205,7 +205,7 @@ it('suggests postcss-less for Less sources', () => {
})

it('should give the correct column of missed semicolon with !important', () => {
let error
let error: any
try {
parse('a { \n color: red !important\n background-color: black;\n}')
} catch (e) {
Expand All @@ -215,7 +215,7 @@ it('should give the correct column of missed semicolon with !important', () => {
})

it('should give the correct column of missed semicolon without !important', () => {
let error
let error: any
try {
parse('a { \n color: red\n background-color: black;\n}')
} catch (e) {
Expand Down
13 changes: 7 additions & 6 deletions test/processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import postcss, {
Parser,
Stringifier
} from '../lib/postcss.js'
import CssSyntaxError from '../lib/css-syntax-error.js'
import LazyResult from '../lib/lazy-result.js'
import Processor from '../lib/processor.js'
import Rule from '../lib/rule.js'
Expand All @@ -36,7 +37,7 @@ async function catchError(cb: () => Promise<any>): Promise<Error> {
try {
await cb()
} catch (e) {
return e
if (e instanceof Error) return e
}
throw new Error('Error was not thrown')
}
Expand Down Expand Up @@ -147,19 +148,19 @@ it('inlines maps from previous result', () => {
})

it('throws with file name', () => {
let error
let error: CssSyntaxError | undefined
try {
new Processor([() => {}]).process('a {', { from: 'a.css' }).css
} catch (e) {
if (e.name === 'CssSyntaxError') {
if (e instanceof CssSyntaxError) {
error = e
} else {
throw e
}
}

expect(error.file).toEqual(pathResolve('a.css'))
expect(error.message).toMatch(/a.css:1:1: Unclosed block$/)
expect(error?.file).toEqual(pathResolve('a.css'))
expect(error?.message).toMatch(/a.css:1:1: Unclosed block$/)
})

it('allows to replace Root', () => {
Expand Down Expand Up @@ -373,7 +374,7 @@ it('remembers errors', async () => {
processing.root
}).toThrow('test')

let asyncError
let asyncError: any
try {
await processing
} catch (e) {
Expand Down
14 changes: 7 additions & 7 deletions test/visitor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ it('adds plugin to error', async () => {
throw rule.error('test')
}
}
let error
let error: any
try {
postcss([broken]).process('a{}', { from: 'broken.css' }).css
} catch (e) {
Expand All @@ -533,7 +533,7 @@ it('adds plugin to async error', async () => {
throw rule.error('test')
}
}
let error
let error: any
try {
await postcss([broken]).process('a{}', { from: 'broken.css' })
} catch (e) {
Expand All @@ -551,7 +551,7 @@ it('adds sync plugin to async error', async () => {
throw rule.error('test')
}
}
let error
let error: any
try {
await postcss([broken]).process('a{}', { from: 'broken.css' })
} catch (e) {
Expand All @@ -569,7 +569,7 @@ it('adds node to error', async () => {
throw new Error('test')
}
}
let error
let error: any
try {
postcss([broken]).process('a{}', { from: 'broken.css' }).css
} catch (e) {
Expand All @@ -588,7 +588,7 @@ it('adds node to async error', async () => {
throw new Error('test')
}
}
let error
let error: any
try {
await postcss([broken]).process('a{}', { from: 'broken.css' })
} catch (e) {
Expand All @@ -604,7 +604,7 @@ it('shows error on sync call async plugins', () => {
postcssPlugin: 'asyncPlugin',
async Rule() {}
}
let error
let error: any
try {
postcss([asyncPlugin]).process('a{}', { from: 'broken.css' }).css
} catch (e) {
Expand Down Expand Up @@ -1400,7 +1400,7 @@ it('throws error from async OnceExit', async () => {
from: 'a.css'
})

let error
let error: any
try {
await result
} catch (e) {
Expand Down

0 comments on commit d1ba5f9

Please sign in to comment.