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(eslint-plugin): [prom-func-async] report only function head #2872
Merged
bradzacher
merged 11 commits into
typescript-eslint:master
from
phaux:prom-func-async-report-head
Feb 28, 2021
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5a6e5df
feat(eslint-plugin): [prom-func-async] add fixer
phaux 74b7570
Merge branch 'master' into prom-func-async-fixer
phaux 94574ce
feat(eslint-plugin): report only head of function
phaux 85e6f61
fix(eslint-plugin): ignore abstract methods
phaux fa2a1a3
fix(eslint-plugin): fix fixer for object method
phaux dcab779
Merge branch 'master' into prom-func-async-fixer
phaux f568a9f
chore(eslint-plugin): revert: report function head
phaux 3b99f5e
feat(eslint-plugin): only report function head
phaux 0e14e71
Merge branch 'master' into prom-func-async-report-head
phaux 1045cbb
Merge branch 'master' into prom-func-async-report-head
phaux b14ae27
fix(eslint-plugin): don't report decorators
phaux File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
AST_TOKEN_TYPES, | ||
TSESLint, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
|
||
type FunctionNode = | ||
| TSESTree.ArrowFunctionExpression | ||
| TSESTree.FunctionExpression | ||
| TSESTree.FunctionDeclaration; | ||
|
||
/** | ||
* Creates a report location for the given function. | ||
* The location only encompasses the "start" of the function, and not the body | ||
* | ||
* eg. | ||
* | ||
* ``` | ||
* function foo(args) {} | ||
* ^^^^^^^^^^^^^^^^^^ | ||
* | ||
* get y(args) {} | ||
* ^^^^^^^^^^^ | ||
* | ||
* const x = (args) => {} | ||
* ^^^^^^^^^ | ||
* ``` | ||
*/ | ||
export function getFunctionHeadLoc( | ||
node: FunctionNode, | ||
sourceCode: TSESLint.SourceCode, | ||
): TSESTree.SourceLocation { | ||
/** | ||
* Returns start column position | ||
* @param node | ||
*/ | ||
function getLocStart(): TSESTree.LineAndColumnData { | ||
/* highlight method name */ | ||
const parent = node.parent; | ||
if ( | ||
parent && | ||
(parent.type === AST_NODE_TYPES.MethodDefinition || | ||
(parent.type === AST_NODE_TYPES.Property && parent.method)) | ||
) { | ||
return parent.loc.start; | ||
} | ||
|
||
return node.loc.start; | ||
} | ||
|
||
/** | ||
* Returns end column position | ||
* @param node | ||
*/ | ||
function getLocEnd(): TSESTree.LineAndColumnData { | ||
/* highlight `=>` */ | ||
if (node.type === AST_NODE_TYPES.ArrowFunctionExpression) { | ||
return sourceCode.getTokenBefore( | ||
node.body, | ||
token => | ||
token.type === AST_TOKEN_TYPES.Punctuator && token.value === '=>', | ||
)!.loc.end; | ||
} | ||
|
||
return sourceCode.getTokenBefore(node.body)!.loc.end; | ||
} | ||
|
||
return { | ||
start: getLocStart(), | ||
end: getLocEnd(), | ||
}; | ||
} |
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.
while we're here - one case I believe this code doesn't correctly handle is decorators because decorators are included within the method's start.
Eg this code will highlight:
this has confused some people in the past (eg #751)