Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Add code examples
Browse files Browse the repository at this point in the history
- Added code examples for prefer-while rule
  • Loading branch information
rwaskiewicz committed May 3, 2018
1 parent 187bc3d commit 61d8259
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/rules/code-examples/preferWhile.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* @license
* Copyright 2018 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Lint from "../../index";

// tslint:disable: object-literal-sort-keys
export const codeExamples = [
{
description: "Prefer `while` loops instead of `for` loops without an initializer and incrementor.",
config: Lint.Utils.dedent`
"rules": { "prefer-while": true }
`,
pass: Lint.Utils.dedent`
for(let x = 1; x < 10; x++) {
console.log(x);
}
for (let i = 0; i < 10; x+=1) {
console.log(x);
}
for (let i = 0; i < 10;) {
i += 1;
}
`,
fail: Lint.Utils.dedent`
for(;;) {
console.log(x);
}
for(;true===true;) {
console.log(x);
}
`,
},
];
2 changes: 2 additions & 0 deletions src/rules/preferWhileRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { isForStatement } from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
import { codeExamples } from "./code-examples/preferWhile.examples";

export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
Expand All @@ -31,6 +32,7 @@ export class Rule extends Lint.Rules.AbstractRule {
hasFix: true,
type: "style",
typescriptOnly: false,
codeExamples,
};
/* tslint:enable:object-literal-sort-keys */

Expand Down

0 comments on commit 61d8259

Please sign in to comment.