Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report correct error when parsing files with broken syntax. Fixes #3204 #4364

Merged
merged 1 commit into from Oct 19, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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