Skip to content

Commit aa0e0e5

Browse files
committed
Support more modern JavaScript syntax
Fixes #5
1 parent 59ee2e9 commit aa0e0e5

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
import esprima from 'esprima';
1+
import {parse} from '@babel/parser';
22
import {gulpPlugin} from 'gulp-plugin-extras';
33

44
export default function gulpJsValidate({module: useModule = true} = {}) {
5-
const parse = useModule ? esprima.parseModule : esprima.parseScript;
6-
75
return gulpPlugin('gulp-jsvalidate', file => {
8-
const {errors} = parse(file.contents.toString(), {tolerant: true});
6+
try {
7+
parse(file.contents.toString(), {
8+
sourceType: useModule ? 'module' : 'script',
9+
sourceFilename: file.basename,
10+
});
11+
} catch (error) {
12+
if (error instanceof SyntaxError) {
13+
const formattedError = new Error(`\n${error.message}`);
14+
formattedError.isPresentable = true;
15+
throw formattedError;
16+
}
917

10-
if (errors.length > 0) {
11-
const error = new Error(`\n${errors.join('\n')}`);
12-
error.isPresentable = true;
1318
throw error;
1419
}
1520

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
},
1313
"type": "module",
1414
"exports": "./index.js",
15+
"sideEffects": false,
1516
"engines": {
1617
"node": ">=18"
1718
},
@@ -36,15 +37,15 @@
3637
"code"
3738
],
3839
"dependencies": {
39-
"esprima": "^4.0.1",
40-
"gulp-plugin-extras": "^0.2.2"
40+
"@babel/parser": "^7.25.3",
41+
"gulp-plugin-extras": "^1.0.0"
4142
},
4243
"devDependencies": {
43-
"ava": "^5.3.1",
44-
"gulp": "^4.0.2",
45-
"p-event": "^6.0.0",
44+
"ava": "^6.1.3",
45+
"gulp": "^5.0.0",
46+
"p-event": "^6.0.1",
4647
"vinyl": "^3.0.0",
47-
"xo": "^0.56.0"
48+
"xo": "^0.59.3"
4849
},
4950
"peerDependencies": {
5051
"gulp": ">=4"

test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,19 @@ test('main', async t => {
99
const errorPromise = pEvent(stream);
1010

1111
stream.end(new Vinyl({
12-
contents: Buffer.from('const foo = \'bar;'),
12+
path: import.meta.url,
13+
contents: Buffer.from('const foo = \'bar;\nlet undefinedVariable;undefinedVariable ??= \'Hello\';'),
1314
}));
1415

15-
await t.throwsAsync(errorPromise, {message: /Unexpected token/});
16+
await t.throwsAsync(errorPromise, {message: /Unterminated string constant/});
1617
});
1718

1819
test('supports `import`', async t => {
1920
const stream = gulpJsvalidate();
2021
const errorPromise = pEvent(stream, 'data');
2122

2223
stream.end(new Vinyl({
24+
path: import.meta.url,
2325
contents: Buffer.from('import foo from \'foo\';'),
2426
}));
2527

0 commit comments

Comments
 (0)