Skip to content

Commit

Permalink
Report correct error when parsing files with broken syntax. Fixes #3204
Browse files Browse the repository at this point in the history
… (#4364)
  • Loading branch information
hudochenkov committed Oct 19, 2019
1 parent 369f6fe commit a81fdd7
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 13 deletions.
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/broken-syntax/broken-syntax.css
@@ -0,0 +1 @@
}
1 change: 1 addition & 0 deletions lib/__tests__/fixtures/broken-syntax/correct-syntax.css
@@ -0,0 +1 @@
a {}
7 changes: 7 additions & 0 deletions lib/__tests__/fixtures/broken-syntax/stylelint.config.js
@@ -0,0 +1,7 @@
'use strict';

module.exports = {
rules: {
'length-zero-no-unit': true,
},
};
53 changes: 42 additions & 11 deletions lib/__tests__/standalone-parseErrors.test.js
Expand Up @@ -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');
Expand All @@ -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)',
},
],
});
});
5 changes: 3 additions & 2 deletions lib/standalone.js
Expand Up @@ -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);
});
});

Expand Down Expand Up @@ -298,9 +298,10 @@ module.exports = function(
function handleError(
stylelint /*: stylelint$internalApi */,
error /*: Object */,
filePath = null,
) /*: Promise<stylelint$result> */ {
if (error.name === 'CssSyntaxError') {
return createStylelintResult(stylelint, null, null, error);
return createStylelintResult(stylelint, null, filePath, error);
} else {
throw error;
}
Expand Down

0 comments on commit a81fdd7

Please sign in to comment.