Skip to content

Commit 64a5b48

Browse files
refactor: SyntaxError and Warning
1 parent 1fdc462 commit 64a5b48

File tree

9 files changed

+71
-54
lines changed

9 files changed

+71
-54
lines changed

src/SyntaxError.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import formatCodeFrame from 'babel-code-frame';
22

33
export default class SyntaxError extends Error {
4-
constructor(err) {
5-
super(err);
4+
constructor(error) {
5+
super(error);
66

7-
this.name = 'Syntax Error';
8-
this.message = err.reason ? err.reason : err.message;
7+
const { reason, message, line, column, source } = error;
98

10-
if (err.line && err.column) {
11-
this.message += ` (${err.line}:${err.column})`;
9+
this.name = 'SyntaxError';
10+
this.message = reason || message;
1211

13-
if (err.source) {
14-
this.message += `\n\n${formatCodeFrame(
15-
err.source,
16-
err.line,
17-
err.column
18-
)}\n`;
12+
if (line && column) {
13+
this.message += ` (${line}:${column})`;
14+
15+
if (source) {
16+
this.message += `\n\n${formatCodeFrame(source, line, column)}\n`;
1917
}
2018
}
2119

22-
Error.captureStackTrace(this, this.constructor);
20+
if (Error.captureStackTrace) {
21+
Error.captureStackTrace(this, this.constructor);
22+
}
2323
}
2424
}

src/Warning.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default class Warning extends Error {
2+
constructor(warning) {
3+
super(warning);
4+
5+
this.name = 'Warning';
6+
this.message = warning.toString();
7+
8+
if (Error.captureStackTrace) {
9+
Error.captureStackTrace(this, this.constructor);
10+
}
11+
}
12+
}

src/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import postcss from 'postcss';
1414

1515
import schema from './options.json';
1616
import plugin from './plugin';
17+
import Warning from './Warning';
1718
import SyntaxError from './SyntaxError';
1819

1920
let runtimeFile = require.resolve('./runtime');
@@ -94,7 +95,9 @@ export default function loader(content, map, meta) {
9495
postcss(plugins)
9596
.process(contentOrAst, postcssOptions)
9697
.then((result) => {
97-
result.warnings().forEach((msg) => this.emitWarning(msg.toString()));
98+
result
99+
.warnings()
100+
.forEach((warning) => this.emitWarning(new Warning(warning)));
98101

99102
if (meta && meta.messages) {
100103
// eslint-disable-next-line no-param-reassign

test/SyntaxError.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import SyntaxError from '../src/SyntaxError';
22

3+
import webpack from './helpers/compiler';
4+
35
describe('SyntaxError', () => {
46
test('basic', () => {
57
expect(new SyntaxError(new Error()).toString()).toMatchSnapshot();
@@ -36,4 +38,11 @@ describe('SyntaxError', () => {
3638
new SyntaxError(errorWithLineAndColumnAndSource).toString()
3739
).toMatchSnapshot();
3840
});
41+
42+
test('integration', async () => {
43+
const stats = await webpack('broken.js');
44+
45+
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
46+
expect(stats.compilation.errors).toMatchSnapshot('errors');
47+
});
3948
});
Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`SyntaxError basic 1`] = `"Syntax Error"`;
3+
exports[`SyntaxError basic 1`] = `"SyntaxError"`;
44

5-
exports[`SyntaxError basic 2`] = `"Syntax Error: message"`;
5+
exports[`SyntaxError basic 2`] = `"SyntaxError: message"`;
66

7-
exports[`SyntaxError basic 3`] = `"Syntax Error: message"`;
7+
exports[`SyntaxError basic 3`] = `"SyntaxError: message"`;
88

9-
exports[`SyntaxError basic 4`] = `"Syntax Error: message"`;
9+
exports[`SyntaxError basic 4`] = `"SyntaxError: message"`;
1010

11-
exports[`SyntaxError basic 5`] = `"Syntax Error: reason"`;
11+
exports[`SyntaxError basic 5`] = `"SyntaxError: reason"`;
1212

13-
exports[`SyntaxError basic 6`] = `"Syntax Error: message (1:1)"`;
13+
exports[`SyntaxError basic 6`] = `"SyntaxError: message (1:1)"`;
1414

1515
exports[`SyntaxError basic 7`] = `
16-
"Syntax Error: message (1:1)
16+
"SyntaxError: message (1:1)
1717
1818
> 1 | .class { invalid }
1919
| ^
2020
"
2121
`;
22+
23+
exports[`SyntaxError integration: errors 1`] = `
24+
Array [
25+
[ModuleBuildError: Module build failed (from /home/evilebottnawi/IdeaProjects/css-loader/src/index.js):
26+
SyntaxError: Unknown word (2:3)
27+
28+
1 | .some {
29+
> 2 | invalid css;
30+
| ^
31+
3 | }
32+
4 |
33+
],
34+
]
35+
`;
36+
37+
exports[`SyntaxError integration: warnings 1`] = `Array []`;

test/__snapshots__/import-option.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ exports[`import true from modules: warnings 1`] = `Array []`;
225225
exports[`import true invalid: errors 1`] = `
226226
Array [
227227
[ModuleBuildError: Module build failed (from /home/evilebottnawi/IdeaProjects/css-loader/src/index.js):
228-
Syntax Error: Unexpected format (1:1)
228+
SyntaxError: Unexpected format (1:1)
229229
230230
> 1 | @import;
231231
| ^

test/__snapshots__/loader.test.js.snap

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ module.exports = function escape(url) {
176176

177177
exports[`loader basic: warnings 1`] = `Array []`;
178178

179-
exports[`loader empty: errors 1`] = `Array []`;
179+
exports[`loader empty options: errors 1`] = `Array []`;
180180

181-
exports[`loader empty: module 1`] = `
181+
exports[`loader empty options: module 1`] = `
182182
Array [
183183
Array [
184184
1,
@@ -188,7 +188,7 @@ Array [
188188
]
189189
`;
190190

191-
exports[`loader empty: warnings 1`] = `Array []`;
191+
exports[`loader empty options: warnings 1`] = `Array []`;
192192

193193
exports[`loader error when no loader for url assets: errors 1`] = `
194194
Array [
@@ -200,22 +200,6 @@ You may need an appropriate loader to handle this file type.
200200

201201
exports[`loader error when no loader for url assets: warnings 1`] = `Array []`;
202202

203-
exports[`loader error when source code is invalid: errors 1`] = `
204-
Array [
205-
[ModuleBuildError: Module build failed (from /home/evilebottnawi/IdeaProjects/css-loader/src/index.js):
206-
Syntax Error: Unknown word (2:3)
207-
208-
1 | .some {
209-
> 2 | invalid css;
210-
| ^
211-
3 | }
212-
4 |
213-
],
214-
]
215-
`;
216-
217-
exports[`loader error when source code is invalid: warnings 1`] = `Array []`;
218-
219203
exports[`loader using together with "postcss-loader" (reuse ast): errors 1`] = `Array []`;
220204

221205
exports[`loader using together with "postcss-loader" (reuse ast): module 1`] = `

test/__snapshots__/validation-errors.test.js.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ValidationError: CSS Loader Invalid Options
1616
options.import should be boolean
1717
1818
at validateOptions (/home/evilebottnawi/IdeaProjects/css-loader/node_modules/schema-utils/src/validateOptions.js:32:11)
19-
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:29:3)],
19+
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:30:3)],
2020
]
2121
`;
2222

@@ -38,7 +38,7 @@ ValidationError: CSS Loader Invalid Options
3838
options.importLoaders should be number
3939
4040
at validateOptions (/home/evilebottnawi/IdeaProjects/css-loader/node_modules/schema-utils/src/validateOptions.js:32:11)
41-
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:29:3)],
41+
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:30:3)],
4242
]
4343
`;
4444

@@ -60,7 +60,7 @@ ValidationError: CSS Loader Invalid Options
6060
options.sourceMap should be boolean
6161
6262
at validateOptions (/home/evilebottnawi/IdeaProjects/css-loader/node_modules/schema-utils/src/validateOptions.js:32:11)
63-
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:29:3)],
63+
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:30:3)],
6464
]
6565
`;
6666

@@ -74,7 +74,7 @@ ValidationError: CSS Loader Invalid Options
7474
options should NOT have additional properties
7575
7676
at validateOptions (/home/evilebottnawi/IdeaProjects/css-loader/node_modules/schema-utils/src/validateOptions.js:32:11)
77-
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:29:3)],
77+
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:30:3)],
7878
]
7979
`;
8080

@@ -96,7 +96,7 @@ ValidationError: CSS Loader Invalid Options
9696
options.url should be boolean
9797
9898
at validateOptions (/home/evilebottnawi/IdeaProjects/css-loader/node_modules/schema-utils/src/validateOptions.js:32:11)
99-
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:29:3)],
99+
at Object.loader (/home/evilebottnawi/IdeaProjects/css-loader/src/index.js:30:3)],
100100
]
101101
`;
102102

test/loader.test.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('loader', () => {
1818
expect(stats.compilation.errors).toMatchSnapshot('errors');
1919
});
2020

21-
test('empty', async () => {
21+
test('empty options', async () => {
2222
const stats = await webpack('empty.js');
2323
const [, module] = stats.toJson().modules;
2424

@@ -28,13 +28,6 @@ describe('loader', () => {
2828
expect(stats.compilation.errors).toMatchSnapshot('errors');
2929
});
3030

31-
test('error when source code is invalid', async () => {
32-
const stats = await webpack('broken.js');
33-
34-
expect(stats.compilation.warnings).toMatchSnapshot('warnings');
35-
expect(stats.compilation.errors).toMatchSnapshot('errors');
36-
});
37-
3831
test('error when no loader for url assets', async () => {
3932
const config = {
4033
rules: [

0 commit comments

Comments
 (0)