-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
refactor: avoid using file name to determine class identity. #603
Open
alexlafroscia
wants to merge
6
commits into
ember-cli:master
Choose a base branch
from
alexlafroscia:fix-require-tagless-on-non-components
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a85b09a
refactor: move module name helper into separate file
alexlafroscia d788c25
refactor: extract utility for getting import node
alexlafroscia d929f5e
feat: add helper for checking if an Identifier is a default export
alexlafroscia 39e3036
chore: update test cases to include imports
alexlafroscia 535cb40
refactor: avoid using file name in rules
alexlafroscia ba714c5
test(require-tagless-components): add Service tests
alexlafroscia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const { isImportDeclaration, isImportDefaultSpecifier } = require('./types'); | ||
const { getSourceModuleName } = require('./utils'); | ||
|
||
module.exports = { | ||
getSourceModuleNameForIdentifier, | ||
isDefaultImport, | ||
}; | ||
|
||
/** | ||
* Gets the ImportDeclaration node that an identifier was imported from | ||
* if it was imported | ||
* | ||
* @param {Object} context the context of the ESLint rule | ||
* @param {node} node the Idenfifier to find an import for | ||
* @returns {ImportDeclaration | undefined} The ImportDeclaration of the module the identifier was imported from, if it was imported | ||
*/ | ||
function getImportDeclarationNode(context, node) { | ||
const [program] = context.getAncestors(node); | ||
|
||
return program.body | ||
.filter(isImportDeclaration) | ||
.find(importDeclaration => | ||
importDeclaration.specifiers.some( | ||
specifier => specifier.local.name === getSourceModuleName(node) | ||
) | ||
); | ||
} | ||
|
||
function getImportSpecifierNode(context, node) { | ||
const importDeclaration = getImportDeclarationNode(context, node); | ||
|
||
return importDeclaration | ||
? importDeclaration.specifiers.find( | ||
specifier => specifier.local.name === getSourceModuleName(node) | ||
) | ||
: undefined; | ||
} | ||
|
||
/** | ||
* Gets the name of the module that an identifier was imported from, | ||
* if it was imported | ||
* | ||
* @param {Object} context the context of the ESLint rule | ||
* @param {node} node the Idenfifier to find an import for | ||
* @returns {string | undefined} The name of the module the identifier was imported from, if it was imported | ||
*/ | ||
function getSourceModuleNameForIdentifier(context, node) { | ||
const importDeclaration = getImportDeclarationNode(context, node); | ||
|
||
return importDeclaration ? importDeclaration.source.value : undefined; | ||
} | ||
|
||
/** | ||
* Determines whether the given Identifer was the default import from some module | ||
* | ||
* @param {Object} context the context of the ESLint rule | ||
* @param {node} node the Idenfifier to check | ||
* @returns {boolean} Whether or not the identifier is a default import from a module | ||
*/ | ||
function isDefaultImport(context, node) { | ||
const importSpecifier = getImportSpecifierNode(context, node); | ||
|
||
return isImportDefaultSpecifier(importSpecifier); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not push this complexity (checking for extends and parents and such) into each rule. Each rule should be able to make one simple util function call to check if the node is an Ember controller/component/etc, like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure I agree -- it seems like we have a specific
CallExpression
pattern we're looking for, which looks likeWe have the ability to tell ESLint those details -- why would we want to check every call expression if we don't have to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To my knowledge, there's no actual difference between using the more-specific selector vs. doing it ourselves: either eslint does the checks or we do the checks. In general, I would agree with you that it's nice to take advantage of using more-specific selectors, but I don't think that's appropriate here, because: