From 83e98812f2809f2d8d2f5bb54640f150bd415588 Mon Sep 17 00:00:00 2001 From: Xianming Zhong Date: Sat, 25 Aug 2018 16:05:55 +0800 Subject: [PATCH 1/3] Wrap non-CssSyntaxError into stylelint results --- src/index.js | 26 +++++++++++++------ .../typescript/ts-syntax-parse-error.ts | 17 ++++++++++++ test/typescript.test.js | 10 +++++++ 3 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 test/fixtures/typescript/ts-syntax-parse-error.ts diff --git a/src/index.js b/src/index.js index 87ecaac..bd4c7de 100644 --- a/src/index.js +++ b/src/index.js @@ -38,9 +38,11 @@ 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 '' @@ -48,12 +50,20 @@ module.exports = options => ({ }, // 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, { + errored: true, + parseErrors: [err] + }) + } } const interpolationLines = interpolationLinesMap[filepath] || [] const lineCorrection = sourceMapsCorrections[filepath] diff --git a/test/fixtures/typescript/ts-syntax-parse-error.ts b/test/fixtures/typescript/ts-syntax-parse-error.ts new file mode 100644 index 0000000..6900851 --- /dev/null +++ b/test/fixtures/typescript/ts-syntax-parse-error.ts @@ -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; +`; \ No newline at end of file diff --git a/test/typescript.test.js b/test/typescript.test.js index b0ccd27..e4d91cf 100644 --- a/test/typescript.test.js +++ b/test/typescript.test.js @@ -54,6 +54,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].name).toEqual('SyntaxError') + 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 => { From 99d1b3b06cf80d6105d4db05b647db6618b210c0 Mon Sep 17 00:00:00 2001 From: Xianming Zhong Date: Mon, 17 Sep 2018 14:21:19 +0800 Subject: [PATCH 2/3] make the inserted parseError follow stylelint$warning --- src/index.js | 10 +++++++++- test/typescript.test.js | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index bd4c7de..6e0c335 100644 --- a/src/index.js +++ b/src/index.js @@ -61,7 +61,15 @@ module.exports = options => ({ // For other errors, wrap them into the result return Object.assign({}, stylelintResult, { errored: true, - parseErrors: [err] + parseErrors: [ + { + line: err.loc && err.loc.line, + column: err.loc && err.loc.column, + rule: 'parseError', + severity: 'error', + text: `${err.message}` + } + ] }) } } diff --git a/test/typescript.test.js b/test/typescript.test.js index e4d91cf..1a651e7 100644 --- a/test/typescript.test.js +++ b/test/typescript.test.js @@ -59,7 +59,7 @@ describe('Typescript files, both TS and TSX should parse and report any errors c 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].name).toEqual('SyntaxError') + expect(data.results[0].parseErrors[0].rule).toEqual('parseError') done() }) }) From 36fe4e6b8b4ab6adc96abe0f5865cc93d709dbda Mon Sep 17 00:00:00 2001 From: Xianming Zhong Date: Mon, 17 Sep 2018 14:32:42 +0800 Subject: [PATCH 3/3] Update invalid fixture as the parser is updated --- test/fixtures/typescript/ts-syntax-parse-error.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/typescript/ts-syntax-parse-error.ts b/test/fixtures/typescript/ts-syntax-parse-error.ts index 6900851..30570f9 100644 --- a/test/fixtures/typescript/ts-syntax-parse-error.ts +++ b/test/fixtures/typescript/ts-syntax-parse-error.ts @@ -10,7 +10,7 @@ export interface IAccordionContainerState { selected: string; } -export const AccordionContainerDiv = styled.div<{ width?: string }>` +export const AccordionContainerDiv = styled.div<{ width#: string }>` border: solid ${(props: IAccordionContainerProps) => props.borderWidth}px; border-bottom-width: 0; width: auto;