Skip to content

Commit

Permalink
Fix reporting of errors in JavaScript assets that use module-exclusiv…
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Nov 11, 2020
1 parent 19024af commit 5174cd0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/assets/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,16 @@ class JavaScript extends Text {
this._parse(text, 'script');
parsedAsScript = true;
} catch (errorParsingAsScript) {
const message = `Parse error in ${this.urlOrDescription}\n${errorParsingAsScript.message}`;
const errorToReport = /sourceType: module/.test(
errorParsingAsScript.message
)
? errorParsingAsModule
: errorParsingAsScript;
const message = `Parse error in ${this.urlOrDescription}\n${errorToReport.message}`;
const err = new errors.ParseError({
message,
line: errorParsingAsScript.lineNumber,
index: errorParsingAsScript.index,
line: errorToReport.lineNumber,
index: errorToReport.index,
asset: this,
});
if (this.assetGraph) {
Expand Down
15 changes: 15 additions & 0 deletions test/assets/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ describe('assets/JavaScript', function () {
);
});

// https://github.com/Munter/netlify-plugin-checklinks/issues/286#issuecomment-725530864
it('should report a syntax error in a JavaScript asset that uses module features', async function () {
let firstWarning;
await new AssetGraph({
root: pathModule.resolve(__dirname, '../../testdata/assets/JavaScript/'),
})
.on('warn', (err) => (firstWarning = firstWarning || err))
.loadAssets('parseErrorAndModuleFeatures.js')
.populate();

expect(firstWarning, 'to be an', Error);
expect(firstWarning.message, 'to match', /Unexpected token/);
expect(firstWarning.message, 'to contain', '(2:18)');
});

it('should handle setting a new parseTree', function () {
const one = new AssetGraph().addAsset({
type: 'JavaScript',
Expand Down
2 changes: 2 additions & 0 deletions testdata/assets/JavaScript/parseErrorAndModuleFeatures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Component } from 'https://unpkg.com/preact?module';
class Foo { state = {}; }

0 comments on commit 5174cd0

Please sign in to comment.