Skip to content

refactor: replace string-similarity w/ fastest-levenshtein #105

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
62 changes: 31 additions & 31 deletions packages/autofix/lib/rules/valid-typeof.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
/**
* @fileoverview add fixer to rule valid-typeof.
* @author Pig Fang<[email protected]>
*/
"use strict";
const ruleComposer = require("eslint-rule-composer");
const utils = require("../utils");
const stringSimilarity = require("string-similarity");
const rule = utils.getFixableRule("valid-typeof", false);
const VALID_TYPES = ["symbol", "undefined", "object", "boolean", "number", "string", "function"];
module.exports = ruleComposer.mapReports(
rule,
problem => {
problem.fix = fixer => {
const { node } = problem,
{ parent } = node;
const sibling = parent.left.type.endsWith("Literal") ? parent.left : parent.right;
const value = sibling.type === "Literal" ? sibling.value : sibling.quasis[0].value.cooked;
const best = stringSimilarity.findBestMatch(value, VALID_TYPES).bestMatch;
if (best.rating > 0.3) {
return fixer.replaceText(sibling, `"${best.target}"`);
}
return null;
};
return problem;
}
);
/**
* @fileoverview add fixer to rule valid-typeof.
* @author Pig Fang<[email protected]>
*/
"use strict";

const ruleComposer = require("eslint-rule-composer");
const utils = require("../utils");
const { closest } = require("fastest-levenshtein");

const rule = utils.getFixableRule("valid-typeof", false);
const VALID_TYPES = ["symbol", "undefined", "object", "boolean", "number", "string", "function"];

module.exports = ruleComposer.mapReports(
rule,
problem => {
problem.fix = fixer => {
const { node } = problem,
{ parent } = node;
const sibling = parent.left.type.endsWith("Literal") ? parent.left : parent.right;
const value = sibling.type === "Literal" ? sibling.value : sibling.quasis[0].value.cooked;
const best = closest(value, VALID_TYPES);

if (best.rating > 0.3) {
return fixer.replaceText(sibling, `'${best.target}'`);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know what is happening here. If I don't change this, the test fails (both on my computer and on CI).

I choose to simply change the quote style instead of modifying the test cases.

return null;
};
return problem;
}
);
2 changes: 1 addition & 1 deletion packages/autofix/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"eslint-rule-composer": "^0.3.0",
"espree": "^9.0.0",
"esutils": "^2.0.2",
"string-similarity": "^4.0.3"
"fastest-levenshtein": "^1.0.16"
},
"devDependencies": {
"@not-an-aardvark/node-release-script": "^0.1.0",
Expand Down
Loading