tslint-clean-code
Version:
TSLint rules for enforcing Clean Code
118 lines • 6.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TestHelper_1 = require("./TestHelper");
describe('noCommentedOutCodeRule', function () {
var ruleName = 'no-commented-out-code';
context('when inside inline comment', function () {
it('should pass on single word', function () {
var script = "\n // lorem\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should pass on multiple words', function () {
var script = "\n // Lorem ipsum dolor sit\n\n const obj = {\n foo: \"bar\", // Lorem ipsum dolor sit\n fooz: 42, // Lorem ipsum dolor sit\n\n // Lorem ipsum dolor sit\n baz: true\n }\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should fail on commented-out code', function () {
var script = "\n // console.log(\"Lorem ipsum\");\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, [
noCommentedOutCodeError({
character: 13,
line: 2,
}),
]);
});
});
context('when inside block comment', function () {
it('should pass on single word', function () {
var script = "\n /*\n lorem\n */\n\n /* lorem */\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should pass on multiple words', function () {
var script = "\n /*\n Lorem ipsum dolor sit\n */\n\n /* Lorem ipsum dolor sit */\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should fail on commented-out code', function () {
var script = "\n /*\n console.log(\"Lorem ipsum\");\n */\n\n /* console.log(\"Lorem ipsum\"); */\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, [
noCommentedOutCodeError({
character: 13,
line: 2,
}),
noCommentedOutCodeError({
character: 13,
line: 6,
}),
]);
});
});
context('when inside JSDoc-style block comment', function () {
it('should pass on single word', function () {
var script = "\n /**\n * lorem\n */\n\n /** lorem */\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should pass on multiple words', function () {
var script = "\n /**\n * Lorem ipsum dolor sit\n */\n\n /** Lorem ipsum dolor sit */\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should pass on JSDoc with tags', function () {
var script = "\n /**\n * @constructor\n */\n\n /**\n * @param {string} foo\n * @param {number} bar\n */\n\n /**\n * @param {string} foo\n * @return {string}\n */\n\n /**\n * @return {string}\n */\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should fail on commented-out code', function () {
var script = "\n /**\n * console.log(\"Lorem ipsum\");\n */\n\n /** console.log(\"Lorem ipsum\"); */\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, [
noCommentedOutCodeError({
character: 13,
line: 2,
}),
noCommentedOutCodeError({
character: 13,
line: 6,
}),
]);
});
});
context('when tslint comment', function () {
it('should pass on tslint:disable comment', function () {
var script = "\n // tslint:disable:no-reserved-keywords\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should pass on tslint:enable comment', function () {
var script = "\n // tslint:enable:no-reserved-keywords\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
});
context('when comment contains TODO-like note', function () {
it('should allow commenting-out code if prefixed with uppercase TODO-like note', function () {
var script = "\n // TODO: a + b\n // NOTE: a + b\n // FIXME: a + b\n // BUG: a + b\n // HACK: a + b\n // XXX: a + b\n ";
TestHelper_1.TestHelper.assertNoViolation(ruleName, script);
});
it('should validate as usual if prefixed with unexpected TODO-like note', function () {
var script = "\n // todo: a + b\n // ToDo: a + b\n // Foo: a + b\n\n // TODO a + b\n // TODO() a + b\n // TODO(): a + b\n // TODO(foo) a + b\n // TODO(foo): a + b\n // TODO({foo: \"bar\"}) a + b\n // TODO({foo: \"bar\"}): a + b\n ";
TestHelper_1.TestHelper.assertViolations(ruleName, script, [
noCommentedOutCodeError({
character: 13,
line: 2,
}),
noCommentedOutCodeError({
character: 13,
line: 3,
}),
noCommentedOutCodeError({
character: 13,
line: 4,
}),
]);
});
});
});
function noCommentedOutCodeError(startPosition) {
return {
failure: 'No commented out code.',
name: 'file.ts',
ruleName: 'no-commented-out-code',
ruleSeverity: 'ERROR',
startPosition: startPosition,
};
}
//# sourceMappingURL=NoCommentedOutCodeRuleTests.js.map