Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Examples for tslint-documentation #2

Open
wants to merge 91 commits into
base: docs/code-examples
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
91 commits
Select commit Hold shift + click to select a range
3ca9f8c
completed-docs: corrected example config for tags (#3578)
reduckted Dec 19, 2017
3c702fb
TSLint rules for RxJS (#3599)
listepo-alterpost Dec 23, 2017
cfa76a2
fix no unnecessary space before parameter from arrow-parens fixer (#3…
mateuszwitkowski Jan 2, 2018
88d6a14
[new-rule-option] [object-literal-sort-key] Add shorthand-first optio…
brunolemos Jan 5, 2018
a7f0537
[docs] [ban-comma-operator] metadata updates (#3612)
Jan 8, 2018
08441bf
fix condition for deprecation of typeof-compare (#3429)
ajafff Jan 9, 2018
5155583
refactor: removed 'export = Linter` in linter.ts (#3438)
ajafff Jan 9, 2018
6dfa0f6
no-unsafe-any: allow object spread (#3439)
ajafff Jan 9, 2018
93fef3a
callable-types: add parens inside ArrayType (#3440)
ajafff Jan 9, 2018
5256228
stylish-formatter: make filename clickable (#3491)
ajafff Jan 9, 2018
e2d929d
launch.json: show stdout in debug console (#3464)
ajafff Jan 9, 2018
4c29dd9
no-unnecessary-type-assertion: extract checking of NonNullExpression …
ajafff Jan 9, 2018
9bfc582
Add test for no-unnecesary-type-assertion (#3535)
ajafff Jan 9, 2018
7a9f177
Remove test for plain test output (#3407)
ajafff Jan 9, 2018
7a2bfbd
no-unsafe-any: correctly handle types and type-only namespaces (#3487)
ajafff Jan 9, 2018
18fc973
strict-type-predicates: allow comparing typeof with expression (#3542)
ajafff Jan 9, 2018
8b853b2
Cleanup and refactoring of (rule|formatter)Loader (#3412)
ajafff Jan 9, 2018
82bf779
Added no-dynamic-delete rule (#3573)
Jan 10, 2018
ef28083
Override defaultSeverity defined in extended configs (#3449)
M0ns1gn0r Jan 10, 2018
4117f16
[new-rule] newline-per-chained-call (#3278)
Jan 10, 2018
d9e20be
Inherit defaultSeverity and apply it to preceding base configs (#3530)
ajafff Jan 10, 2018
b0061e7
[docs]Updated no-irregular-whitespace documentation (#3627)
rwaskiewicz Jan 10, 2018
b5ebb9f
Prepare release 5.9.0 (#3634)
adidahiya Jan 10, 2018
2b8a737
Disable typeof-compare in tslint:recommended (#3639)
adidahiya Jan 10, 2018
a4dc874
Resolve custom paths when loading rules (#3640)
adidahiya Jan 10, 2018
235561e
Prepare release 5.9.1
adidahiya Jan 10, 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
001c135
Improve code-example descriptions
Shinigami92 Jan 17, 2018
f1b0896
Remove buffers `noAssert` argument (#3770)
BridgeAR Apr 1, 2018
01cf478
Removes trailing whitespace from file-header (#3802)
cwgorman Apr 2, 2018
569d8da
Update index.md (#3773)
Apr 2, 2018
8716315
Improve documentation of cli flag --project (#3703)
a-xin Apr 5, 2018
b381eab
no-unbound-method: Fix description typo (#3732)
sehrope Apr 5, 2018
124311b
Fixed "manging" typo in no-this-assignment (#3673)
Apr 5, 2018
2bb6aa9
docs(changelog): fix wrong description (#3659)
rafaelss95 Apr 5, 2018
f904497
Update for docs/usage/configuration/index.md (#3661)
Apr 5, 2018
42b058a
Added 2 (+/-1) sentence rationales for 2/3 of rules (#3734)
Apr 5, 2018
8e13390
Build tslint with TypeScript 2.7.2. (#3819)
LucasSloan Apr 6, 2018
375de6d
promise-function-async: add options to specify function types to chec…
blair Apr 7, 2018
c89e0a7
Used bang in file-header (#3741)
Apr 9, 2018
e27439f
Clarified lint message "All imports are unused." (#3831)
jishi9 Apr 16, 2018
fb0da64
Updated max-line-length ignore-pattern example (#3834)
Apr 16, 2018
0c24e91
Add the option allow-empty-functions in no-empty (#3624)
markvincze Apr 18, 2018
5c86dd4
[docs] adds optional capability to provide code examples in rules' me…
Apr 19, 2018
a7be726
Fix build (#3853)
suchanlee Apr 26, 2018
f399c3c
Removed *nix-only comment from package.json (#3858)
Apr 26, 2018
6bc44a2
[new-fixer]: Add space before comment message fixer (#3845)
tanniiz Apr 26, 2018
49399eb
Added rule+file-specific error messages to rule crashes (#3836)
Apr 26, 2018
37dddca
Adding documentation example to deprecationRule (#3848)
felipeissa May 1, 2018
9af0b65
Improved "complete docs" docs (#3823)
304NotModified May 1, 2018
921bd43
[docs] no-string-throw: add missing examples. (#3701)
BB9z May 1, 2018
0503f6d
correct name and link for "object-literal-sort-keys" in changelog (#3…
mwld May 1, 2018
f47aeb3
no-unbound-method: add another exception (#3610)
ethanresnick May 1, 2018
7682184
Fix completed-docs rule (#3868)
rwaskiewicz May 2, 2018
baa413e
[new-rule]prefer-while rule (#3750)
rwaskiewicz May 3, 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
c86193f
Improve "no valid rules" error message to include file type (#3729)
OliverJAsh May 3, 2018
93fd0ac
Excluded native JSX elements from no-unsafe-any (#3699)
May 3, 2018
ecab88c
Prepare release 5.10.0 (#3871)
jkillian May 3, 2018
6cd9b03
Fix Copyright: @license JSDoc tag was missing (#3879)
bowenni May 7, 2018
5af26e2
Fix some typos in `no-floating-promises` rule docs. (#3886)
pe8ter May 7, 2018
147ed02
Fix typo (#3898)
mesaugat May 10, 2018
9132ace
Fix link to Configuration page (#3891)
May 10, 2018
681218d
[docs] Fix prefer-while docs (#3888)
rwaskiewicz May 10, 2018
dc54347
CS: test/rules/no-empty/default/test.ts.lint - fix missing newline at…
keradus May 10, 2018
13723b5
no-empty: fix lack of methods handling (#3897)
keradus May 10, 2018
d04d026
Fix typo (#3910)
liuxh0 May 15, 2018
1f16db8
Turn on strictPropertyInitialization for src/ and test/. (#3924)
rkirov May 23, 2018
0bc48cd
init patch; using Object.is to diff between pos/neg zero (#3903)
May 30, 2018
2cd3dee
bugfix - no-use-before-declare - exception for destructuring w/ renam…
May 30, 2018
db5fa51
Added fixer for switch-final-break (#3615)
May 30, 2018
175a586
Handle tsconfig.json errors without using JSON.stringify (#3908)
mertdeg2 Jun 7, 2018
fa86eaa
Deprecate no-unused-variable rule for typescript@next; also fix runti…
jkillian Jun 11, 2018
46098f7
don't run no-unused-variable tests when testing against version of TS…
jkillian Jun 11, 2018
c01bb2c
Use Buffer.allocUnsafe instead of the deprecated new Buffer() (#3985)
NaridaL Jun 26, 2018
f82ef3c
Implemented fixer for member-ordering and added corresponding tests. …
NaridaL Jun 26, 2018
d95dd91
Radix rule checks Number.parseInt(), window.Number.parseInt(), global…
david-cannady Jun 26, 2018
1c31dc0
[New rule] add fileNameCasing rule (#3978)
euclio Jun 26, 2018
6989dd7
no-implicit-dependencies should include peer dependencies even with "…
saberduck Jun 26, 2018
4a11f95
Output +/- on diff so added/removed empty lines are visible. (#3973)
NaridaL Jun 28, 2018
9d7db47
[new-rule-option] [no-implicit-dependencies] - add whitelist (#3839) …
ajcrites Jun 28, 2018
29fbace
Fix CI: no-implicit-dependencies test failure; typescript@next failur…
pablobirukov Jul 11, 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
34 changes: 34 additions & 0 deletions src/rules/arrowReturnShorthandRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,40 @@ export class Rule extends Lint.Rules.AbstractRule {
],
type: "style",
typescriptOnly: false,
codeExamples: [
{
description: "Enforces to use shorthand for return.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "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 } };
`,
},
{
description: "Enforces to use shorthand for return also for multiline.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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 calc = (x: number, y: number) => {
return { add: x + y, sub: x - y, mul: x * y }
};
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
16 changes: 16 additions & 0 deletions src/rules/classNameRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "style",
typescriptOnly: false,
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 { }
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
14 changes: 14 additions & 0 deletions src/rules/noAnyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "typescript",
typescriptOnly: true,
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;
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
16 changes: 16 additions & 0 deletions src/rules/noEmptyInterfaceRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,22 @@ export class Rule extends Lint.Rules.AbstractRule {
options: null,
type: "typescript",
typescriptOnly: true,
codeExamples: [
{
description: "Disallows an empty interface.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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 { }
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
14 changes: 14 additions & 0 deletions src/rules/noSparseArraysRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "functionality",
typescriptOnly: false,
codeExamples: [
{
description: "Disallow sparse arrays",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disallows*

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'];
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
18 changes: 18 additions & 0 deletions src/rules/noUnnecessaryCallbackWrapperRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "style",
typescriptOnly: false,
codeExamples: [
{
description: "Disallow unnecessary callback wrappers",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disallows*

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));
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
52 changes: 52 additions & 0 deletions src/rules/objectLiteralSortKeysRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,58 @@ export class Rule extends Lint.Rules.OptionallyTypedRule {
],
type: "maintainability",
typescriptOnly: false,
codeExamples: [
{
description: "Forces to sort keys in an object by alphabetical order",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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: "Forces to sort keys in an object by alphabetical order",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this still needs updated.

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
};
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
26 changes: 26 additions & 0 deletions src/rules/oneVariablePerDeclarationRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true, [true, OPTION_IGNORE_FOR_LOOP]],
type: "style",
typescriptOnly: false,
codeExamples: [
{
description: "Disallows multiple variable definitions in the same declaration statement.",
config: Lint.Utils.dedent`
"rules": { "one-variable-per-declaration": true }
`,
pass: Lint.Utils.dedent`
const foo = 1;
const bar = '2';
`,
fail: Lint.Utils.dedent`
const foo = 1, bar = '2';
`,
},
{
description: "Disallows multiple variable definitions in the same declaration statement but allows them in for-loops.",
config: Lint.Utils.dedent`
"rules": { "one-variable-per-declaration": [true, "ignore-for-loop"] }
`,
pass: Lint.Utils.dedent`
for (let i = 0, j = 10; i < 10; i++) {
doSomething(j, i);
}
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
22 changes: 22 additions & 0 deletions src/rules/onlyArrowFunctionsRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true, [true, OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS]],
type: "typescript",
typescriptOnly: false,
codeExamples: [
{
description: "Disallow functions with the function keyword",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disallows*

config: Lint.Utils.dedent`
"rules": { "only-arrow-functions": true }
`,
pass: Lint.Utils.dedent`
const myFunc = () => {
// do something ...
};
`,
fail: Lint.Utils.dedent`
function myFunc() {
// do something ...
};

const myFunc = function() {
// do something ...
};
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
35 changes: 35 additions & 0 deletions src/rules/preferTemplateRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,41 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true, [true, OPTION_SINGLE_CONCAT]],
type: "style",
typescriptOnly: false,
codeExamples: [
{
description: "Forces to use template-strings",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Enforces the use of template strings whenever possible."

config: Lint.Utils.dedent`
"rules": { "prefer-template": true }
`,
pass: Lint.Utils.dedent`
const x: number = 1;
const y: number = 1;
const myString: string = \`\${x} is equals \${y}\`;
`,
fail: Lint.Utils.dedent`
const x: number = 1;
const y: number = 1;
const myString: string = x + ' is equals ' + y;
`,
},
{
description: "Forces to use template-strings but allows up to one concatenation",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Enforces the use of template strings, but allows up to one concatenation."

config: Lint.Utils.dedent`
"rules": { "prefer-template": [true, "allow-single-concat"] }
`,
pass: Lint.Utils.dedent`
const x: number = 1;
const y: number = 1;
const myString: string = x + ' is equals 1';
const myString: string = \`\${x} is equals \${y}\`;
`,
fail: Lint.Utils.dedent`
const x: number = 1;
const y: number = 1;
const myString: string = x + ' is equals ' + y;
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
20 changes: 20 additions & 0 deletions src/rules/radixRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "functionality",
typescriptOnly: false,
codeExamples: [
{
description: "Requires the radix parameter to be specified when calling `parseInt`.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Requires the inclusion of the radix parameter when calling parseInt."

config: Lint.Utils.dedent`
"rules": { "radix": true }
`,
pass: Lint.Utils.dedent`
const x: string = '11';
const dec: number = parseInt(x, 10);
const bin: number = parseInt(x, 2);
const hex: number = parseInt(x, 16);
`,
fail: Lint.Utils.dedent`
const x: string = '11';
const dec: number = parseInt(x);
const bin: number = parseInt(x, 2);
const hex: number = parseInt(x, 16);
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
33 changes: 33 additions & 0 deletions src/rules/switchDefaultRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ export class Rule extends Lint.Rules.AbstractRule {
optionExamples: [true],
type: "functionality",
typescriptOnly: false,
codeExamples: [
{
description: "Require a `default` case in `switch` statements.",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires*

config: Lint.Utils.dedent`
"rules": { "switch-default": true }
`,
pass: Lint.Utils.dedent`
let foo: number = 1;
switch (foo) {
case 1:
doSomething();
break;
case 2:
doSomething2();
break;
default:
console.log('default');
break;
}
`,
fail: Lint.Utils.dedent`
let foo: number = 1;
switch (foo) {
case 1:
doSomething();
break;
case 2:
doSomething2();
break;
}
`,
},
],
};
/* tslint:enable:object-literal-sort-keys */

Expand Down
Loading