diff --git a/lib/__tests__/fixtures/broken-syntax/broken-syntax.css b/lib/__tests__/fixtures/broken-syntax/broken-syntax.css new file mode 100644 index 0000000000..5c34318c21 --- /dev/null +++ b/lib/__tests__/fixtures/broken-syntax/broken-syntax.css @@ -0,0 +1 @@ +} diff --git a/lib/__tests__/fixtures/broken-syntax/correct-syntax.css b/lib/__tests__/fixtures/broken-syntax/correct-syntax.css new file mode 100644 index 0000000000..077f6dd7c0 --- /dev/null +++ b/lib/__tests__/fixtures/broken-syntax/correct-syntax.css @@ -0,0 +1 @@ +a {} diff --git a/lib/__tests__/fixtures/broken-syntax/stylelint.config.js b/lib/__tests__/fixtures/broken-syntax/stylelint.config.js new file mode 100644 index 0000000000..01efb86be8 --- /dev/null +++ b/lib/__tests__/fixtures/broken-syntax/stylelint.config.js @@ -0,0 +1,7 @@ +'use strict'; + +module.exports = { + rules: { + 'length-zero-no-unit': true, + }, +}; diff --git a/lib/__tests__/standalone-parseErrors.test.js b/lib/__tests__/standalone-parseErrors.test.js index 6f82281e93..c1299b189e 100644 --- a/lib/__tests__/standalone-parseErrors.test.js +++ b/lib/__tests__/standalone-parseErrors.test.js @@ -2,6 +2,7 @@ const blockNoEmpty = require('../rules/block-no-empty'); const configBlockNoEmpty = require('./fixtures/config-block-no-empty'); +const path = require('path'); const standalone = require('../standalone'); jest.mock('../rules/block-no-empty'); @@ -14,16 +15,46 @@ blockNoEmpty.mockImplementation(() => { }; }); -describe('standalone with deprecations', () => { - it('works', () => { - return standalone({ - code: 'a {}', - config: configBlockNoEmpty, - }).then((data) => { - expect(data.output.indexOf('Some parseError')).not.toBe(-1); - expect(data.results).toHaveLength(1); - expect(data.results[0].parseErrors).toHaveLength(1); - expect(data.results[0].parseErrors[0].text).toBe('Some parseError'); - }); +test('standalone with deprecations', async () => { + const data = await standalone({ + code: 'a {}', + config: configBlockNoEmpty, + }); + + expect(data.output.indexOf('Some parseError')).not.toBe(-1); + expect(data.results).toHaveLength(1); + expect(data.results[0].parseErrors).toHaveLength(1); + expect(data.results[0].parseErrors[0].text).toBe('Some parseError'); +}); + +test('file with correct syntax reported correctly', async () => { + const data = await standalone({ + files: path.join(__dirname, 'fixtures/broken-syntax/correct-syntax.css'), + }); + + expect(data.results[0]).toMatchObject({ + parseErrors: [], + errored: false, + warnings: [], + }); +}); + +test('file with invalid syntax reported correctly', async () => { + const data = await standalone({ + files: [path.join(__dirname, 'fixtures/broken-syntax/broken-syntax.css')], + }); + + expect(data.results[0]).toMatchObject({ + parseErrors: [], + errored: true, + warnings: [ + { + column: 1, + line: 1, + rule: 'CssSyntaxError', + severity: 'error', + text: 'Unexpected } (CssSyntaxError)', + }, + ], }); }); diff --git a/lib/standalone.js b/lib/standalone.js index d23868242e..082dd21ba9 100644 --- a/lib/standalone.js +++ b/lib/standalone.js @@ -242,7 +242,7 @@ module.exports = function( // On any error, we should not cache the lint result fileCache.removeEntry(absoluteFilepath); - return handleError(stylelint, error); + return handleError(stylelint, error, absoluteFilepath); }); }); @@ -298,9 +298,10 @@ module.exports = function( function handleError( stylelint /*: stylelint$internalApi */, error /*: Object */, + filePath = null, ) /*: Promise */ { if (error.name === 'CssSyntaxError') { - return createStylelintResult(stylelint, null, null, error); + return createStylelintResult(stylelint, null, filePath, error); } else { throw error; }