-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: docs/code-examples
Are you sure you want to change the base?
Changes from 4 commits
3ca9f8c
3c702fb
cfa76a2
88d6a14
a7f0537
08441bf
5155583
6dfa0f6
93fef3a
5256228
e2d929d
4c29dd9
9bfc582
7a9f177
7a2bfbd
18fc973
8b853b2
82bf779
ef28083
4117f16
d9e20be
b0061e7
b5ebb9f
2b8a737
a4dc874
235561e
a6d0cc8
cbf20a8
9a037ba
808b74d
001c135
f1b0896
01cf478
569d8da
8716315
b381eab
124311b
2bb6aa9
f904497
42b058a
8e13390
375de6d
c89e0a7
e27439f
fb0da64
0c24e91
5c86dd4
a7be726
f399c3c
6bc44a2
49399eb
37dddca
9af0b65
921bd43
0503f6d
f47aeb3
7682184
baa413e
30d5e4a
2efd7c2
422ff59
c86193f
93fd0ac
ecab88c
6cd9b03
5af26e2
147ed02
9132ace
681218d
dc54347
13723b5
d04d026
1f16db8
0bc48cd
2cd3dee
db5fa51
175a586
fa86eaa
46098f7
c01bb2c
f82ef3c
d95dd91
1c31dc0
6989dd7
4a11f95
9d7db47
29fbace
94f0e23
dd45672
bd1f364
c1ef707
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,40 @@ export class Rule extends Lint.Rules.AbstractRule { | |
], | ||
type: "style", | ||
typescriptOnly: false, | ||
codeExamples: [ | ||
{ | ||
description: "Enforces to use shorthand for return.", | ||
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.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,22 @@ export class Rule extends Lint.Rules.AbstractRule { | |
options: null, | ||
type: "typescript", | ||
typescriptOnly: true, | ||
codeExamples: [ | ||
{ | ||
description: "Disallows an empty interface.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,20 @@ export class Rule extends Lint.Rules.AbstractRule { | |
optionExamples: [true], | ||
type: "functionality", | ||
typescriptOnly: false, | ||
codeExamples: [ | ||
{ | ||
description: "Disallow sparse arrays", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,24 @@ export class Rule extends Lint.Rules.AbstractRule { | |
optionExamples: [true], | ||
type: "style", | ||
typescriptOnly: false, | ||
codeExamples: [ | ||
{ | ||
description: "Disallow unnecessary callback wrappers", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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`.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Requires the inclusion of the radix parameter when calling |
||
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 */ | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
||
|
There was a problem hiding this comment.
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."