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

Fix: dot notation rule failing to catch string template (fixes #9350) #9357

Merged
merged 1 commit into from Sep 29, 2017

Conversation

philquinn
Copy link
Contributor

@philquinn philquinn commented Sep 27, 2017

Fixes #9350

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

[ ] Documentation update
[X ] 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
[ ] Other, please explain:

What changes did you make? (Give an overview)

I added a check for TemplateLiteral and if it is the case then it checks the quasi associated as long as no expression is found, as if it has an expression is not a valid dot notation.

As the fix would be reused in my chance I pulled that into a function.

Added a few test cases.

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

Nothing in particular

@eslintbot
Copy link

Thanks for the pull request, @philquinn! I took a look to make sure it's ready for merging and found some changes are needed:

  • The commit summary must be 72 characters or shorter. Please check out our guide for how to properly format your commit summary and update it on this pull request.

Can you please update the pull request to address these?

(More information can be found in our pull request guide.)

@jsf-clabot
Copy link

jsf-clabot commented Sep 27, 2017

CLA assistant check
All committers have signed the CLA.

@philquinn philquinn force-pushed the fix-dot-notation-with-templates branch from 209a2ff to da74411 Compare September 27, 2017 01:34
@eslintbot
Copy link

Thanks for the pull request, @philquinn! I took a look to make sure it's ready for merging and found some changes are needed:

  • The commit summary must be 72 characters or shorter. Please check out our guide for how to properly format your commit summary and update it on this pull request.

Can you please update the pull request to address these?

(More information can be found in our pull request guide.)

@philquinn philquinn force-pushed the fix-dot-notation-with-templates branch from da74411 to ad841b7 Compare September 27, 2017 01:36
@eslintbot
Copy link

LGTM

@not-an-aardvark not-an-aardvark changed the title Fix: fix for dot notation rule failing to catch string template in ce… Fix: dot notation rule failing to catch string template (fixes #9350) Sep 27, 2017
Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

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

Thanks for the pull request! This looks pretty good overall. I left a few small suggestions.

node.computed &&
node.property.type === "TemplateLiteral" &&
node.property.expressions.length === 0 &&
node.property.quasis.length === 1 &&
Copy link
Member

Choose a reason for hiding this comment

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

I think checking node.property.quasis.length is redundant here, because that will always be the case when node.property.expressions.length === 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Really fair, I was being a bit defensive with checking here

node.property.expressions.length === 0 &&
node.property.quasis.length === 1 &&
validIdentifier.test(node.property.quasis[0].value.raw) &&
(allowKeywords || keywords.indexOf(String(node.property.quasis[0].value.raw)) === -1)
Copy link
Member

Choose a reason for hiding this comment

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

I think calling String() is unnecessary here, because the value of the template literal will always be a string.

validIdentifier.test(node.property.quasis[0].value.raw) &&
(allowKeywords || keywords.indexOf(String(node.property.quasis[0].value.raw)) === -1)
) {
if (!(allowPattern && allowPattern.test(node.property.quasis[0].value.raw))) {
Copy link
Member

Choose a reason for hiding this comment

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

In general I think it would be better to use the .cooked property of the template literal in these cases, rather than the .raw property. The difference is that if a template literal contains escape sequences like \n, the .cooked property will contain the "computed" escape sequence (in this case, a newline) whereas the .raw property will contain the raw escape sequence that was present in the string (in this case, a backslash followed by an n).

Using cooked here would ensure that the rule reports an error when linting strange code like foo[`b\ar`], which is equivalent to foo.bar.

});
}
}
if (
Copy link
Member

Choose a reason for hiding this comment

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

It seems like there's some duplicated code here relating to checking keywords, checking valid identifiers, and reporting. I think that could be avoided by doing something like this:

function checkComputedProperty(node, value) {
    if (validIdentifier.test(value) && /* ... */ ) {
        // ...
    }
}

return {
    MemberExpression(node) {
        if (node.computed && node.property.type === "Literal") {
            checkComputedProperty(node, node.property.value);
        } else if (node.computed && node.property.type === "TemplateLiteral" && node.property.expressions.length === 0) {
            checkComputedProperty(node, node.property.quasis[0].value.cooked);
        }

    }
};

This would also make fixDotNotationWithValue unnecessary, since there would only be one context.report call in the file. In other words, this would extract all of the value-checking logic into a function rather than only extracting the autofix logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah agree thats cleaner. Will change now.

@philquinn philquinn force-pushed the fix-dot-notation-with-templates branch from ad841b7 to ab4769b Compare September 27, 2017 07:24
@eslintbot
Copy link

LGTM

@philquinn
Copy link
Contributor Author

@not-an-aardvark Updated PR there. Thanks for the review.

Copy link
Member

@not-an-aardvark not-an-aardvark left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thanks!

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion bug ESLint is working incorrectly rule Relates to ESLint's core rules labels Sep 29, 2017
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.

LGTM, thanks for contributing!

@philquinn
Copy link
Contributor Author

Thanks for the reviews

@kaicataldo
Copy link
Member

LGTM. Thanks for contributing to ESLint!

@kaicataldo kaicataldo merged commit ff2be59 into eslint:master Sep 29, 2017
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Mar 29, 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 Mar 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dot-notation doesn't work if backtick is used
7 participants