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

Chore: avoid loose equality assertions #9415

Merged
merged 3 commits into from Oct 14, 2017
Merged

Conversation

not-an-aardvark
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[ ] Documentation update
[ ] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[x] Other, please explain:

What changes did you make? (Give an overview)

This updates the no-restricted-syntax configuration to disallow assert.equal and assert.notEqual in favor of assert.strictEqual and assert.notStrictEqual. It also fixes a few places where the tests were relying on a loose equality check (e.g. comparing a single-element array to a string).

A few loose equality assertions in RuleTester were kept for backwards compatibility.

Is there anything you'd like reviewers to focus on?

Nothing in particular

This updates the `no-restricted-syntax` configuration to disallow
`assert.equal` and `assert.notEqual` in favor of `assert.strictEqual`
and `assert.notStrictEqual`. It also fixes a few places where the tests
were relying on a loose equality check (e.g. comparing a single-element
    array to a string).
@not-an-aardvark not-an-aardvark added the chore This change is not user-facing label Oct 12, 2017
@eslintbot
Copy link

LGTM

Copy link
Member

@platinumazure platinumazure left a comment

Choose a reason for hiding this comment

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

Left a few comments/suggestions, nothing that should be considered a blocker. Thanks for doing this!

assert.equal(actual.length, 1);
assert.equal(actual, expected);
assert.strictEqual(actual.length, 1);
assert.deepEqual(actual, [expected]);
Copy link
Member

Choose a reason for hiding this comment

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

Given that there's a length assertion above this line, we could assert for strict equality as follows:

assert.strictEqual(actual[0], expected);

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, it looks like assert.deepStrictEqual is supported on all necessary Node versions, so maybe we should switch to using that as well.

Copy link
Member

Choose a reason for hiding this comment

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

I'd be in favor of that for sure. Maybe we could add assert.deepEqual to no-restricted-properties as well?

@@ -127,14 +127,14 @@ describe("IgnoredPaths", () => {
const ignorePatterns = getIgnorePatterns(ignoredPaths);

assert.isNotNull(ignoredPaths.baseDir);
assert.equal(getIgnoreFiles(ignoredPaths), expectedIgnoreFile);
assert.deepEqual(getIgnoreFiles(ignoredPaths), [expectedIgnoreFile]);
Copy link
Member

Choose a reason for hiding this comment

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

Similarly, by asserting on length here, we could also do a strict comparison of the first element.

@@ -92,7 +92,7 @@ describe("options", () => {
const currentOptions = options.parse("--rulesdir /morerules");

assert.isArray(currentOptions.rulesdir);
assert.equal(currentOptions.rulesdir, "/morerules");
assert.deepEqual(currentOptions.rulesdir, ["/morerules"]);
Copy link
Member

Choose a reason for hiding this comment

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

Could achieve strict comparison by comparing length and first element.

@@ -9596,10 +9596,10 @@ assert.ok = ok;

// 5. The equality assertion tests shallow, coercive equality with
// ==.
// assert.equal(actual, expected, message_opt);
// assert.strictEqual(actual, expected, message_opt);
Copy link
Member

Choose a reason for hiding this comment

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

Should this be changed?


assert.equal = function equal(actual, expected, message) {
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
assert.strictEqual = function equal(actual, expected, message) {
Copy link
Member

Choose a reason for hiding this comment

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

Would it make more sense to reconfigure no-restricted-properties for this file to allow the local assert.equal? I'm assuming this is third party code, maybe I'm misunderstanding.

Copy link
Member Author

Choose a reason for hiding this comment

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

Oops, this was an accidental change resulting from a global find/replace.

@eslintbot
Copy link

LGTM

@kaicataldo kaicataldo merged commit febb897 into master Oct 14, 2017
@not-an-aardvark not-an-aardvark deleted the strict-equality-assertions branch October 14, 2017 18:21
@kaicataldo
Copy link
Member

Thanks for contributing to ESLint!

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Apr 13, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Apr 13, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
archived due to age This issue has been archived; please open a new issue for any further discussion chore This change is not user-facing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants