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

[docs] additional code examples for 12 rules #3869

Merged
merged 24 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f6c7d8e
codeExample support init implementation
Dec 21, 2017
06a2954
dedent/markdown errors need revisting
Jan 3, 2018
658e593
updates to new code example scheme as per ajafff
Jan 14, 2018
d075a5a
reverts prettier style updates in curly
Jan 14, 2018
938420c
curly docs he -> they
Jan 14, 2018
57c4188
ICodeExample config type any -> string
Jan 14, 2018
12bcc3a
Code example blocks now color-coded according to pass/fail (grn/red)
Jan 14, 2018
a6d0cc8
added code-examples to no-sparse-arrays, no-unnecessary-callback-wrap…
Shinigami92 Jan 14, 2018
cbf20a8
Update noUnnecessaryCallbackWrapperRule.ts
Shinigami92 Jan 14, 2018
9a037ba
added code-examples to only-arrow-functions, prefer-template and typedef
Shinigami92 Jan 14, 2018
808b74d
more examples
Shinigami92 Jan 15, 2018
227ae45
updates to buildDocs
Jan 16, 2018
001c135
Improve code-example descriptions
Shinigami92 Jan 17, 2018
5c78e6a
gemfile.lock??; removes unnecessary html/scss
Apr 9, 2018
67a0eb1
removes site dist
Apr 9, 2018
6ba33df
moving code examples to dedicated dir to avoid bloat in rule files
Apr 13, 2018
b6fa661
removing _site dir
Apr 13, 2018
30d5e4a
Merge branch 'docs/code-examples' of https://github.com/aervin/tslint…
Shinigami92 May 3, 2018
2efd7c2
move codeExamples into new directory
Shinigami92 May 3, 2018
422ff59
Merge branch 'master' of https://github.com/palantir/tslint into docs…
Shinigami92 May 3, 2018
94f0e23
changed year in code-examples copyrights
Shinigami92 Jul 12, 2018
dd45672
Merge branch 'master' into docs/code-examples
Shinigami92 Jul 12, 2018
bd1f364
Merge branch 'master' into docs/code-examples
Shinigami92 Jul 12, 2018
c1ef707
wrong import
Shinigami92 Jul 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/rules/arrowReturnShorthandRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as ts from "typescript";

import * as Lint from "../index";
import { hasCommentAfterPosition } from "../language/utils";
import { codeExamples } from "./code-examples/arrowReturnShorthand.examples";

const OPTION_MULTILINE = "multiline";

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

Expand Down
2 changes: 2 additions & 0 deletions src/rules/classNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import * as ts from "typescript";

import * as Lint from "../index";
import { isPascalCased } from "../utils";
import { codeExamples } from "./code-examples/className.examples";

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

Expand Down
57 changes: 57 additions & 0 deletions src/rules/code-examples/arrowReturnShorthand.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @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: "Enforces usage of the shorthand return syntax when an arrow function's body does not span multiple lines.",
config: Lint.Utils.dedent`
"rules": { "arrow-return-shorthand": true }
`,
pass: Lint.Utils.dedent`
const calc = (x: number, y: number) => ({ add: x + y, sub: x - y, mul: x * y });
const calc2 = (x: number, y: number) => {
return { add: x + y, sub: x - y, mul: x * y }
};
`,
fail: Lint.Utils.dedent`
const calc = (x: number, y: number) => { return { add: x + y, sub: x - y, mul: x * y } };
const calc2 = (x: number, y: number) => {
return { add: x + y, sub: x - y, mul: x * y }
};
`,
},
{
description: "Enforces usage of the shorthand return syntax even when an arrow function's body spans multiple lines.",
config: Lint.Utils.dedent`
"rules": { "arrow-return-shorthand": [true, "multiline"] }
`,
pass: Lint.Utils.dedent`
const calc = (x: number, y: number) => ({ add: x + y, sub: x - y, mul: x * y });
const calc2 = (x: number, y: number) =>
({ add: x + y, sub: x - y, mul: x * y });
`,
fail: Lint.Utils.dedent`
const calc = (x: number, y: number) => { return { add: x + y, sub: x - y, mul: x * y } };
const calc2 = (x: number, y: number) => {
return { add: x + y, sub: x - y, mul: x * y }
};
`,
},
];
36 changes: 36 additions & 0 deletions src/rules/code-examples/className.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @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: "Enforces PascalCased class and interface names.",
config: Lint.Utils.dedent`
"rules": { "class-name": true }
`,
pass: Lint.Utils.dedent`
class MyClass { }
interface MyInterface { }
`,
fail: Lint.Utils.dedent`
class myClass { }
interface myInterface { }
`,
},
];
2 changes: 1 addition & 1 deletion src/rules/code-examples/curly.examples.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
* 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.
Expand Down
34 changes: 34 additions & 0 deletions src/rules/code-examples/noAny.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @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: "Disallows usages of `any` as a type declaration.",
config: Lint.Utils.dedent`
"rules": { "no-any": true }
`,
pass: Lint.Utils.dedent`
let foo: object;
`,
fail: Lint.Utils.dedent`
let foo: any;
`,
},
];
36 changes: 36 additions & 0 deletions src/rules/code-examples/noEmptyInterface.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @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: "Disallows empty interfaces.",
config: Lint.Utils.dedent`
"rules": { "no-empty-interface": true }
`,
pass: Lint.Utils.dedent`
interface I {
foo: string;
}
`,
fail: Lint.Utils.dedent`
interface I { }
`,
},
];
34 changes: 34 additions & 0 deletions src/rules/code-examples/noSparseArrays.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* @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: "Disallows sparse arrays",
config: Lint.Utils.dedent`
"rules": { "no-sparse-arrays": true }
`,
pass: Lint.Utils.dedent`
const arr: string[] = ['elemenet1', 'element2'];
`,
fail: Lint.Utils.dedent`
const arr: string[] = ['elemenet1',, 'element2'];
`,
},
];
2 changes: 1 addition & 1 deletion src/rules/code-examples/noStringThrowRule.examples.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* @license
* Copyright 2013 Palantir Technologies, Inc.
* 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.
Expand Down
38 changes: 38 additions & 0 deletions src/rules/code-examples/noUnnecessaryCallbackWrapper.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @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: "Disallows unnecessary callback wrappers",
config: Lint.Utils.dedent`
"rules": { "no-unnecessary-callback-wrapper": true }
`,
pass: Lint.Utils.dedent`
const handleContent = (content) => console.log('Handle content:', content);
const handleError = (error) => console.log('Handle error:', error);
promise.then(handleContent).catch(handleError);
`,
fail: Lint.Utils.dedent`
const handleContent = (content) => console.log('Handle content:', content);
const handleError = (error) => console.log('Handle error:', error);
promise.then((content) => handleContent(content)).catch((error) => handleError(error));
`,
},
];
73 changes: 73 additions & 0 deletions src/rules/code-examples/objectLiteralSortKeys.examples.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* @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: "Requires that an object literal's keys be sorted alphabetically.",
config: Lint.Utils.dedent`
"rules": { "object-literal-sort-keys": true }
`,
pass: Lint.Utils.dedent`
let o = {
bar: 2,
foo: 1
};
`,
fail: Lint.Utils.dedent`
let o = {
foo: 1,
bar: 2
};
`,
},
{
description: Lint.Utils.dedent`Requires that an object literal's keys be sorted by interface-definition.
If there is no interface fallback to alphabetically.`,
config: Lint.Utils.dedent`
"rules": {
"object-literal-sort-keys": {
"options": "match-declaration-order"
}
}
`,
pass: Lint.Utils.dedent`
interface I {
foo: number;
bar: number;
}

let o: I = {
foo: 1,
bar: 2
};
`,
fail: Lint.Utils.dedent`
interface I {
foo: number;
bar: number;
}

let o: I = {
bar: 2,
foo: 1
};
`,
},
];
Loading