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
…e features

Munter/netlify-plugin-checklinks#286 (comment)
(cherry picked from commit 5174cd0)
  • Loading branch information
papandreou committed Nov 11, 2020
1 parent 9117e90 commit 918f2f9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/assets/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,17 @@ 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,
asset: this
line: errorToReport.lineNumber,
index: errorToReport.index,
asset: this,
});
if (this.assetGraph) {
this.assetGraph.warn(err);
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 918f2f9

Please sign in to comment.