Skip to content

Add a 'Did you mean ...' error message in case of an unrecognized API property #150

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const validator = require('./lib/config/validator');
const PrettyError = require('pretty-error');
const logger = require('./lib/logger');
const parseRuntime = require('./lib/config/parse-runtime');
const chalk = require('chalk');
const levenshtein = require('fast-levenshtein');

let webpackConfig = null;
let runtimeConfig = require('./lib/context').runtimeConfig;
Expand Down Expand Up @@ -633,6 +635,28 @@ const publicApiProxy = new Proxy(publicApi, {
};
}

if (typeof target[prop] === 'undefined') {
// Find the property with the closest Levenshtein distance
let similarProperty;
let minDistance = Number.MAX_VALUE;
for (const apiProperty in target) {
const distance = levenshtein.get(apiProperty, prop);
if (distance <= minDistance) {
similarProperty = apiProperty;
minDistance = distance;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you type something totally crazy that is completely not close to anything, it will suggest something, right? In Symfony's core, we use "3" as the maximum distance (if it's more than 3, we don't recommend anything): https://github.com/symfony/symfony/blob/1bb2bc322bc1c2d0e6e70c2a0fa9fc0be90757fd/src/Symfony/Component/ExpressionLanguage/SyntaxError.php#L34.

It probably makes sense to do the same thing here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2017-09-13_23-01-01

2017-09-13_23-02-08

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be exact, we don't use 3 most of the time. We use strlen(input) / 3 (we have a few cases using 3)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks! I've just changed that at sha: 2e216a4... and indeed, it is a bit more "forgiving" in appropriate situations.


let errorMessage = `${chalk.red(`Encore.${prop}`)} is not a recognized property or method.`;
if (minDistance < 3) {
errorMessage += ` Did you mean ${chalk.green(`Encore.${similarProperty}`)}?`;
}

const error = new Error(errorMessage);
console.log(new PrettyError().render(error));
process.exit(1); // eslint-disable-line
}

return target[prop];
}
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"clean-webpack-plugin": "^0.1.16",
"css-loader": "^0.26.2",
"extract-text-webpack-plugin": "^3.0.0",
"fast-levenshtein": "^2.0.6",
"file-loader": "^0.10.1",
"friendly-errors-webpack-plugin": "^1.6.1",
"fs-extra": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2156,7 +2156,7 @@ fast-deep-equal@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-1.0.0.tgz#96256a3bc975595eb36d82e9929d060d893439ff"

fast-levenshtein@~2.0.4:
fast-levenshtein@^2.0.6, fast-levenshtein@~2.0.4:
version "2.0.6"
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"

Expand Down