Skip to content

Commit

Permalink
chore: adds better error logging for detect features
Browse files Browse the repository at this point in the history
  • Loading branch information
yowainwright committed Feb 3, 2025
1 parent afec4e2 commit e2b288a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 5 additions & 1 deletion detectFeatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,14 @@ const detectFeatures = (code, ecmaVersion, sourceType) => {
* @note Fail if any unsupported features were used.
*/
if (unsupportedFeatures.length > 0) {
throw new Error(
const error = new Error(
`Unsupported features detected: ${unsupportedFeatures.join(', ')}. ` +
`These require a higher ES version than ${ecmaVersion}.`
);
error.type = 'ES-Check';
error.features = unsupportedFeatures;
error.ecmaVersion = ecmaVersion;
throw error;
}

return {
Expand Down
9 changes: 4 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const path = require('path')
const supportsColor = require('supports-color')
const winston = require('winston')
const detectFeatures = require('./detectFeatures')
const { formatError } = require('./utils')
const pkg = require('./package.json')

/**
Expand Down Expand Up @@ -226,13 +227,11 @@ program
logger.debug(`Features found in ${file}: ${stringifiedFeatures}`);
const isSupported = unsupportedFeatures.length === 0;
if (!isSupported) {
logger.error(
`ES-Check: The file "${file}" uses these unsupported features: ${unsupportedFeatures.join(', ')}
but your target is ES${ecmaVersion}.`
);
const error = new Error(`Unsupported features used: ${unsupportedFeatures.join(', ')} but your target is ES${ecmaVersion}.`);
errArray.push({
err: new Error(`Unsupported features used: ${unsupportedFeatures.join(', ')}`),
err: error,
file,
stack: error.stack
});
}
})
Expand Down
15 changes: 15 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,25 @@ const checkMap = {
default: () => false,
};

const formatError = (filePath, error) => {
console.error('error: ES-Check: there were 1 ES version matching errors.');
console.info(`
ES-Check Error:
----
· erroring file: ${filePath}
· error: ${error.message}
· see the printed err.stack below for context
----
${error.stack}
`);
};

module.exports = {
checkVarKindMatch,
checkCalleeMatch,
checkOperatorMatch,
checkDefault,
checkMap,
formatError,
};

0 comments on commit e2b288a

Please sign in to comment.