Skip to content

Commit

Permalink
parse errors shouldn't cause other import errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Nov 3, 2020
1 parent 98567d2 commit ebe58d5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 3 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"name": "Run tests",
"program": "${workspaceFolder}/node_modules/.bin/jest",
"cwd": "${workspaceFolder}/packages/compat",
"args": ["--runInBand", "--testPathPattern", "tests/stage2.test.js"]
"args": ["--runInBand", "--testPathPattern", "tests/audit.test.js"], //, "--test-name-pattern", "traverse"],
"outputCapture": "std"
},
{
"type": "node",
Expand Down Expand Up @@ -134,7 +135,7 @@
"name": "Audit static-app",
"program": "${workspaceFolder}/node_modules/.bin/embroider-compat-audit",
"cwd": "${workspaceFolder}/test-packages/static-app",
"args": ["--reuse-build"],
"args": [],
"outputCapture": "std"
},
{
Expand Down
13 changes: 9 additions & 4 deletions packages/compat/src/audit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ interface InternalModule {
isCJS: boolean;
isAMD: boolean;
templateFindings: { message: string; detail: string; codeFrameIndex?: number }[];
failedToParse: boolean;
}

export interface Import {
Expand Down Expand Up @@ -305,7 +306,7 @@ export class Audit {
} else if (resolved) {
let target = this.modules.get(resolved)!;
for (let specifier of imp.specifiers) {
if (!this.moduleProvidesName(target, specifier.name)) {
if (!target.failedToParse && !this.moduleProvidesName(target, specifier.name)) {
if (specifier.name === 'default') {
let backtick = '`';
this.findings.push({
Expand Down Expand Up @@ -382,8 +383,9 @@ export class Audit {
this.pushFinding({
filename,
message: `failed to parse`,
detail: err.toString(),
detail: err.toString().replace(filename, explicitRelative(this.appDir, filename)),
});
module.failedToParse = true;
return [];
} else {
throw err;
Expand All @@ -400,8 +402,9 @@ export class Audit {
this.pushFinding({
filename,
message: `failed to compile template`,
detail: err.toString(),
detail: err.toString().replace(filename, explicitRelative(this.appDir, filename)),
});
module.failedToParse = true;
return [];
}
module.templateFindings = this.templateResolver.errorsIn(filename).map(err => ({
Expand All @@ -421,8 +424,9 @@ export class Audit {
this.pushFinding({
filename,
message: `failed to parse JSON`,
detail: err.toString(),
detail: err.toString().replace(filename, explicitRelative(this.appDir, filename)),
});
module.failedToParse = true;
return [];
}
return this.visitJS(filename, js, module);
Expand Down Expand Up @@ -462,6 +466,7 @@ export class Audit {
isCJS: false,
isAMD: false,
templateFindings: [],
failedToParse: false,
};
this.modules.set(filename, record);
this.moduleQueue.add(filename);
Expand Down
17 changes: 15 additions & 2 deletions packages/compat/tests/audit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,14 @@ describe('audit', function () {
'hello.hbs': `<NoSuchThing />`,
});
let result = await audit();
expect(result.findings).toEqual([
expect(withoutCodeFrames(result.findings)).toEqual([
{
message: 'Missing component',
detail: 'NoSuchThing',
filename: './hello.hbs',
},
]);
expect(result.findings[0].codeFrame).toBeDefined();
expect(Object.keys(result.modules).length).toBe(3);
});

Expand All @@ -255,7 +256,7 @@ describe('audit', function () {
},
});
let result = await audit();
expect(result.findings).toEqual([
expect(withoutCodeFrames(result.findings)).toEqual([
{
message: 'Missing component',
detail: 'NoSuchThing',
Expand All @@ -264,6 +265,18 @@ describe('audit', function () {
]);
expect(Object.keys(result.modules).length).toBe(4);
});

test('failure to parse JS is reported and does not cause cascading errors', async function () {
merge(app.files, {
'app.js': `import thing from './has-parse-error'`,
'has-parse-error.js': `export default function() {`,
});
let result = await audit();
expect(result.findings.map(f => ({ filename: f.filename, message: f.message }))).toEqual([
{ filename: './has-parse-error.js', message: 'failed to parse' },
]);
expect(Object.keys(result.modules).length).toBe(3);
});
});

function withoutCodeFrames(findings: Finding[]): Finding[] {
Expand Down

0 comments on commit ebe58d5

Please sign in to comment.