Skip to content

Commit

Permalink
Print file name when parsing/transforming fails
Browse files Browse the repository at this point in the history
Fixes #327
  • Loading branch information
nene committed Mar 12, 2023
1 parent 93e4792 commit f2ea425
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/Cli.js
Expand Up @@ -38,19 +38,25 @@ export default class Cli {
}

transformFile(inFile, outFile) {
const {code, warnings} = this.transformer.run(io.read(inFile));
try {
const {code, warnings} = this.transformer.run(io.read(inFile));

// Log warnings if there are any
if (warnings.length > 0 && inFile) {
console.error(`${inFile}:`); // eslint-disable-line no-console
}
// Log warnings if there are any
if (warnings.length > 0 && inFile) {
console.error(`${inFile}:`); // eslint-disable-line no-console
}

warnings.forEach(({line, msg, type}) => {
console.error( // eslint-disable-line no-console
`${line}: warning ${msg} (${type})`
);
});
warnings.forEach(({line, msg, type}) => {
console.error( // eslint-disable-line no-console
`${line}: warning ${msg} (${type})`
);
});

io.write(outFile, code);
io.write(outFile, code);
}
catch (e) {
console.error(`Error transforming: ${inFile}\n`); // eslint-disable-line no-console
throw e;
}
}
}
22 changes: 22 additions & 0 deletions system-test/binTest.js
Expand Up @@ -89,4 +89,26 @@ describe('Smoke test for the executable script', function() {
});
});
});

describe('when parsing of file fails', () => {
beforeEach(() => {
fs.writeFileSync(
INPUT_WARNINGS_FILE,
'if (true) { @unknown!syntax; }\n'
);
});

afterEach(() => {
fs.unlinkSync(INPUT_WARNINGS_FILE);
});

it('writes error to STDERR', done => {
exec(`node ./bin/index.js --transform let ${INPUT_WARNINGS_FILE}`, (error, stdout, stderr) => {
expect(error.code).to.equal(1); // eslint-disable-line no-null/no-null
expect(stderr).to.contain(`Error transforming: ${INPUT_WARNINGS_FILE}\n`);
expect(stdout).to.equal('');
done();
});
});
});
});

0 comments on commit f2ea425

Please sign in to comment.