Skip to content

Commit

Permalink
fix(rules): support .then() in ion-overlay-method-*-should-use-await …
Browse files Browse the repository at this point in the history
…rule (#62)
  • Loading branch information
imhoffd committed Oct 9, 2018
1 parent 126b4f3 commit 7318565
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
9 changes: 7 additions & 2 deletions src/ionOverlayMethodCreateShouldUseAwaitRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ const matchingControllers = [

class CreateMethodShouldUseAwaitWalker extends Lint.RuleWalker {
visitCallExpression(node: ts.CallExpression) {
debugger;
if (node.arguments.length > 0) {
const firstArgument = node.arguments[0];

if (isValidForRule(node, 'create', ...matchingControllers) && tsutils.isObjectLiteralExpression(firstArgument)) {
if (node.parent.kind !== ts.SyntaxKind.AwaitExpression) {
if (
!tsutils.isAwaitExpression(node.parent) &&
(!tsutils.isPropertyAccessExpression(node.parent) ||
!tsutils.isCallExpression(node.parent.parent) ||
!tsutils.isPropertyAccessExpression(node.parent.parent.expression) ||
node.parent.parent.expression.name.text !== 'then')
) {
this.addFailureAtNode(node, ruleMessage);
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/ionOverlayMethodPresentShouldUseAwaitRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ const matchingControllers = [

class CreateMethodShouldUseAwaitWalker extends Lint.RuleWalker {
visitCallExpression(node: ts.CallExpression) {
debugger;

let expression = node.expression;

if (tsutils.isPropertyAccessExpression(expression) && expression.name.text === 'present') {
if (node.parent.kind !== ts.SyntaxKind.AwaitExpression) {
if (
!tsutils.isAwaitExpression(node.parent) &&
(!tsutils.isPropertyAccessExpression(node.parent) ||
!tsutils.isCallExpression(node.parent.parent) ||
!tsutils.isPropertyAccessExpression(node.parent.parent.expression) ||
node.parent.parent.expression.name.text !== 'then')
) {
this.addFailureAtNode(node, ruleMessage);
}
}
Expand Down
17 changes: 17 additions & 0 deletions test/ionOverlayMethodCreateShouldUseAwaitRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ describe(ruleName, () => {
assertSuccess(ruleName, source);
});

it('should work when then() is used', () => {
let source = `
class DoSomething{
constructor(private actionSheetController: ActionSheetController){}
doWork(){
this.actionSheetController.create({
component: PopoverComponent,
ev: event,
translucent: true
}).then(() => {});
}
}
`;
assertSuccess(ruleName, source);
});

it('should work when the controller variable is named something else', () => {
let source = `
class DoSomething{
Expand Down
17 changes: 17 additions & 0 deletions test/ionOverlayMethodPresentShouldUseAwaitRule.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ describe(ruleName, () => {
`;
assertSuccess(ruleName, source);
});

it('should work when then() is used', () => {
let source = `
class DoSomething{
constructor(private actionSheetController: ActionSheetController){}
doWork(){
this.actionSheetController.create({
component: PopoverComponent,
ev: event,
translucent: true
}).then(() => {});
}
}
`;
assertSuccess(ruleName, source);
});
});

describe('failure', () => {
Expand Down

0 comments on commit 7318565

Please sign in to comment.