Skip to content
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

feat: suggest the closest match based on the Levenshtein distance algorithm #2010

Merged
merged 3 commits into from Nov 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/webpack-cli/lib/bootstrap.js
Expand Up @@ -3,6 +3,7 @@ const { core } = require('./utils/cli-flags');
const logger = require('./utils/logger');
const { isCommandUsed } = require('./utils/arg-utils');
const argParser = require('./utils/arg-parser');
const leven = require('leven');

process.title = 'webpack-cli';

Expand Down Expand Up @@ -39,6 +40,11 @@ const runCLI = async (cliArgs) => {
if (parsedArgs.unknownArgs.length > 0) {
parsedArgs.unknownArgs.forEach(async (unknown) => {
logger.error(`Unknown argument: ${unknown}`);
const strippedFlag = unknown.substr(2);
const { name: suggestion } = core.find((flag) => leven(strippedFlag, flag.name) < 3);
if (suggestion) {
logger.raw(`Did you mean --${suggestion}?`);
}
snitin315 marked this conversation as resolved.
Show resolved Hide resolved
});

process.exit(2);
Expand Down
1 change: 1 addition & 0 deletions packages/webpack-cli/package.json
Expand Up @@ -37,6 +37,7 @@
"execa": "^4.1.0",
"import-local": "^3.0.2",
"interpret": "^2.2.0",
"leven": "^3.1.0",
"rechoir": "^0.7.0",
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
"v8-compile-cache": "^2.2.0",
"webpack-merge": "^4.2.2"
Expand Down
9 changes: 8 additions & 1 deletion test/unknown/unknown.test.js
Expand Up @@ -2,8 +2,15 @@ const { run } = require('../utils/test-utils');

describe('unknown behaviour', () => {
it('warns the user if an unknown flag is passed in', () => {
const { stderr } = run(__dirname, ['--unknown']);
const { stderr, exitCode } = run(__dirname, ['--unknown']);
expect(stderr).toBeTruthy();
expect(stderr).toContain('Unknown argument: --unknown');
expect(exitCode).toBe(2);
});
it('suggests the closest match to an unknown flag', () => {
const { stderr, stdout, exitCode } = run(__dirname, ['--entyr', './a.js']);
expect(stderr).toContain('Unknown argument: --entyr');
jamesgeorge007 marked this conversation as resolved.
Show resolved Hide resolved
expect(stdout).toContain('Did you mean --entry?');
expect(exitCode).toBe(2);
});
});