Skip to content
This repository was archived by the owner on May 14, 2021. It is now read-only.

Wrap non-CssSyntaxError into stylelint results #211

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,40 @@ module.exports = options => ({
)
return extractedCSS
} catch (e) {
// incorrect interpolations will throw CssSyntaxError and they'll be handled by stylelint
// Always save the error
errorWasThrown[absolutePath] = e
// Incorrect interpolations will throw CssSyntaxError and they'll be handled by stylelint
// so we can throw it out but not for others
if (e.name === 'CssSyntaxError') {
errorWasThrown[absolutePath] = true
throw e
}
return ''
}
},
// Fix sourcemaps
result(stylelintResult, filepath) {
if (errorWasThrown[filepath]) {
// We threw an error ourselves, in this case we have already put correct
// line/column numbers so no source maps are needed
// (and would actually break the line numbers)
delete errorWasThrown[filepath]
return stylelintResult
const err = errorWasThrown[filepath]
if (err) {
if (err.name === 'CssSyntaxError') {
// We threw an error ourselves, in this case we have already put correct
// line/column numbers so no source maps are needed
// (and would actually break the line numbers)
return stylelintResult
} else {
// For other errors, wrap them into the result
return Object.assign({}, stylelintResult, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this get displayed to the user?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Node.js API users, it is yes. But I found CLI didn't print very pretty output. The latest commit fixes it.

errored: true,
parseErrors: [
{
line: err.loc && err.loc.line,
column: err.loc && err.loc.column,
rule: 'parseError',
severity: 'error',
text: `${err.message}`
}
]
})
}
}
const interpolationLines = interpolationLinesMap[filepath] || []
const lineCorrection = sourceMapsCorrections[filepath]
Expand Down
17 changes: 17 additions & 0 deletions test/fixtures/typescript/ts-syntax-parse-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as React from 'react';
import styled, { keyframes } from 'styled-components';

export interface IAccordionContainerProps {
className?: string;
borderWidth: number,
onChange?(id: string): void;
}
export interface IAccordionContainerState {
selected: string;
}

export const AccordionContainerDiv = styled.div<{ width#: string }>`
border: solid ${(props: IAccordionContainerProps) => props.borderWidth}px;
border-bottom-width: 0;
width: auto;
`;
10 changes: 10 additions & 0 deletions test/typescript.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ describe('Typescript files, both TS and TSX should parse and report any errors c
})
})

it('should report errors when parsing typescript files with SyntaxError', done => {
const fixture = path.join(__dirname, './fixtures/typescript/ts-syntax-parse-error.ts')
doLint(fixture, done).then(data => {
expect(data.results.length).toEqual(1)
expect(data.results[0].parseErrors.length).toEqual(1)
expect(data.results[0].parseErrors[0].rule).toEqual('parseError')
done()
})
})

it('should report errors in TSX files(typescript + JSX)', done => {
const fixture = path.join(__dirname, './fixtures/typescript/ts-syntax-jsx-invalid.tsx')
doLint(fixture, done).then(data => {
Expand Down