Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
implements fixer for jasmine-no-lambda-expression-callbacks and acc…
Browse files Browse the repository at this point in the history
…ording tests
  • Loading branch information
BendingBender committed Dec 31, 2016
1 parent f85f16f commit 937625d
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 7 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
Change Log
===

v2.0.1
v2.1.0
---
* [new-fixer] `jasmine-no-lambda-expression-callbacks`
* [enhancement] Added preliminary checks for `jasmine-no-lambda-expression-callbacks` rule to skip walking file if there are no jasmine
top-level statements.

Expand Down
21 changes: 19 additions & 2 deletions src/jasmineNoLambdaExpressionCallbacksRule.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IRuleMetadata, RuleFailure, Rules, RuleWalker, Utils } from 'tslint/lib';
import { Fix, IRuleMetadata, Replacement, RuleFailure, Rules, RuleWalker, Utils } from 'tslint/lib';
import { ArrowFunction, CallExpression, Expression, SourceFile, SyntaxKind } from 'typescript';
import { isJasmineDescribe, isJasmineIt, isJasmineSetupTeardown, isJasmineTest } from './utils/jasmineUtils';
import find = require('lodash/find');
Expand Down Expand Up @@ -50,7 +50,7 @@ class JasmineNoLambdaExpressionCallbacksWalker extends RuleWalker {
const invalidLambdaExpression = this.getInvalidLambdaExpression(node);

if (invalidLambdaExpression) {
this.addFailureAtNode(invalidLambdaExpression, Rule.FAILURE_STRING);
this.addFailureAtNode(invalidLambdaExpression, Rule.FAILURE_STRING, this.getFix(invalidLambdaExpression));
}

super.visitCallExpression(node);
Expand Down Expand Up @@ -82,4 +82,21 @@ class JasmineNoLambdaExpressionCallbacksWalker extends RuleWalker {

return null;
}

private getFix(lambdaExpression:ArrowFunction):Fix {
const arrowToken = lambdaExpression.equalsGreaterThanToken;
const replacements = [
new Replacement(lambdaExpression.getStart(), 0, 'function'),
new Replacement(arrowToken.getStart(), lambdaExpression.body.getStart() - arrowToken.getStart(), '')
];

if (lambdaExpression.body.kind !== SyntaxKind.Block) {
replacements.push(
new Replacement(lambdaExpression.body.getStart(), 0, '{ return '),
new Replacement(lambdaExpression.getStart() + lambdaExpression.getWidth(), 0, '; }')
);
}

return new Fix(Rule.metadata.ruleName, replacements);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
describe("this is ok", function() {
it("this is ok", function() {});
it("this is ok", wrap(function() {}));

describe("this is still ok", wrapAgain(function() {}));
});

// still ok
(() => {})();

// still ok
someOtherCallWithLambda(() => {})

describe("this", function() {
it("is an error", function() {
});
});

fdescribe("this, too", function() { return doSomething(); });
fdescribe("this, too", function() { return doSomething(); });
fdescribe("this, too", function(){ return doSomething(); });

xdescribe("this, too", function() {
});

describe("this, too", function() {
it("is an error", function() {
});

fit("is an error", function() {
});

xit("is an error", function() {
});

beforeEach(function() {
});

beforeAll(function() {
});

afterEach(function() {
});

afterAll(function() {
});
});

fdescribe("this, too", wrapCallback(function() {
}));

describe("this, too", wrapCallback(function() {
it("hi", wrapAgain(function() {
}));

afterEach(yetAnotherWrap(function() {
}));
}));

Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ describe("this", () => {
});
~ [no-lambda-callback]

fdescribe("this, too", () => {
~~~~~~~
});
~ [no-lambda-callback]
fdescribe("this, too", () => doSomething());
~~~~~~~~~~~~~~~~~~~ [no-lambda-callback]
fdescribe("this, too", () =>doSomething());
~~~~~~~~~~~~~~~~~~ [no-lambda-callback]
fdescribe("this, too", ()=>doSomething());
~~~~~~~~~~~~~~~~~ [no-lambda-callback]

xdescribe("this, too", () => {
~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(function test() {
describe('some test in an IIFE', function() {});
})();

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(() => {
describe('some test in an IIFE', function() {});
})();

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(function test() {
describe('some test in an other IIFE', function() {});
}());

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function test() {
describe('some test in a callback', function() {});
}

0 comments on commit 937625d

Please sign in to comment.