Skip to content

Commit

Permalink
fix(async-methods): optimize eslint selector
Browse files Browse the repository at this point in the history
  • Loading branch information
d0whc3r committed Sep 15, 2019
1 parent ee0b477 commit ce7b1d3
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/rules/async-methods.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule } from 'eslint';
import ts from 'typescript';
import { getDecorator, stencilComponentContext } from '../utils';
import { stencilComponentContext } from '../utils';
import * as tsutils from 'tsutils';

const rule: Rule.RuleModule = {
Expand All @@ -22,24 +22,28 @@ const rule: Rule.RuleModule = {

return {
...stencil.rules,
'MethodDefinition': (node: any) => {
if (getDecorator(node, 'Method')) {
const method = parserServices.esTreeNodeToTSNodeMap.get(node);
const signature = typeChecker.getSignatureFromDeclaration(method);
const returnType = typeChecker.getReturnTypeOfSignature(signature!);
if (!tsutils.isThenableType(typeChecker, method, returnType)) {
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node) as ts.Node;
const text = String(originalNode.getFullText());
context.report({
node: node.key,
message: `External @Method() ${node.key.name}() must return a Promise. Consider prefixing the method with async, such as @Method async ${node.key.name}().`,
fix(fixer) {
const result = text.replace('@Method()\n', '@Method()\nasync')
.replace('@Method() ', '@Method() async');
return fixer.replaceText(node, result);
}
});
}
'MethodDefinition > Decorator[expression.callee.name=Method]': (decoratorNode: any) => {
if (!stencil.isComponent()) {
return;
}
const node = decoratorNode.parent;
const method = parserServices.esTreeNodeToTSNodeMap.get(node);
const signature = typeChecker.getSignatureFromDeclaration(method);
const returnType = typeChecker.getReturnTypeOfSignature(signature!);
if (!tsutils.isThenableType(typeChecker, method, returnType)) {
const originalNode = parserServices.esTreeNodeToTSNodeMap.get(node) as ts.Node;
const text = String(originalNode.getFullText());
context.report({
node: node.key,
message: `External @Method() ${node.key.name}() must return a Promise. Consider prefixing the method with async, such as @Method() async ${node.key.name}().`,
fix(fixer) {
const result = text.replace('@Method()\n', '@Method()\nasync')
.replace('@Method() ', '@Method() async')
.replace('async public', 'public async')
.replace('async private', 'private async');
return fixer.replaceText(node, result);
}
});
}
}
};
Expand Down

0 comments on commit ce7b1d3

Please sign in to comment.