Skip to content
This repository was archived by the owner on Mar 7, 2019. It is now read-only.

Commit dafc78c

Browse files
feat: add experimental TypeScript ruleset πŸŽ‰
Fixes #39
1 parent 0049be8 commit dafc78c

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

β€ŽREADME.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ See the [tutorial](tutorial) directory for lots of example config files.
5151

5252
- @strv/javascript/environments/flow/recommended
5353

54+
#### For [TypeScript][typescript-docs]
55+
56+
> Requires configuration. See the docs for more info.
57+
58+
- @strv/javascript/environments/typescript/recommended
59+
5460
#### For [Mocha][mocha-docs]
5561

5662
- @strv/javascript/environments/mocha/recommended
@@ -75,6 +81,7 @@ This software is licensed under the **BSD-3-Clause License**. See the [LICENSE](
7581
[travis-url]: https://travis-ci.org/strvcom/eslint-config-javascript
7682
[eslint-version]: https://img.shields.io/badge/ESLint-^5.3.0-brightgreen.svg
7783
[eslint-fixing]: http://eslint.org/docs/user-guide/command-line-interface#fix
84+
[typescript-docs]: environments/typescript
7885
[flow-docs]: environments/flow
7986
[react-docs]: environments/react
8087
[nodejs-docs]: environments/nodejs

β€Ženvironments/typescript/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# TypeScript
2+
3+
> πŸ”₯ Experimental. Please report bugs, conflicts and other compatibility problems πŸ™.
4+
5+
These configuration files are suitable to lint TypeScript code.
6+
7+
> **⚠️ This ruleset requires some additional configuration steps in your project. Please read below for detailed instructions.**
8+
9+
## Installation
10+
11+
You must install an ESLint-compatible TypeScript parser and add it to your _.eslintrc.js_ file:
12+
13+
```sh
14+
npm i -D typescript-eslint-parser@latest
15+
```
16+
17+
In addition to using this ruleset, you should also choose one base ruleset depending on your target platform:
18+
19+
- `@strv/javascript/environments/nodejs/v10`
20+
- `@strv/javascript/environments/react/v16`
21+
22+
A full configuration for a TypeScript on Node.js project:
23+
24+
```js
25+
// .eslintrc.js
26+
'use strict'
27+
28+
module.exports = {
29+
parser: 'typescript-eslint-parser',
30+
31+
extends: [
32+
'@strv/javascript/environments/nodejs/v10',
33+
'@strv/javascript/environments/nodejs/optional',
34+
'@strv/javascript/environments/typescript/recommended',
35+
'@strv/javascript/coding-styles/recommended',
36+
],
37+
}
38+
```
39+
40+
To actually lint .ts files, you must pass the `--ext` flag to ESLint:
41+
42+
```sh
43+
eslint --ext ts --no-unused-disable-directives .
44+
```
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/**
2+
* Js-coding-standards
3+
*
4+
* @author Robert Rossmann <rr.rossmann@me.com>
5+
* @copyright 2018 STRV
6+
* @license http://choosealicense.com/licenses/bsd-3-clause BSD-3-Clause License
7+
*/
8+
9+
'use strict'
10+
11+
module.exports = {
12+
13+
plugins: [
14+
'typescript',
15+
],
16+
17+
parserOptions: {
18+
ecmaVersion: 2018,
19+
sourceType: 'module',
20+
},
21+
22+
env: {
23+
es6: true,
24+
},
25+
26+
rules: {
27+
// TS code is mostly self-documented and having JSDoc directives for everything is redundant
28+
// when you can easily infer return values and argument types from the code itself.
29+
'valid-jsdoc': 'off',
30+
31+
// Disabled because it generates false positives with interface declarations and TypeScript
32+
// blows up anyway during compilation when it encouters an undefined variable.
33+
'no-undef': 'off',
34+
35+
// Require that member overloads be consecutive
36+
// Grouping overloaded members together can improve readability of the code.
37+
'typescript/adjacent-overload-signatures': 'warn',
38+
39+
// Require PascalCased class and interface names
40+
// This rule aims to make it easy to differentiate classes from regular variables at a glance.
41+
'typescript/class-name-casing': 'warn',
42+
43+
// Require explicit return types on functions and class methods
44+
// Explicit types for function return values makes it clear to any calling code what type is
45+
// returned. This ensures that the return value is assigned to a variable of the correct type;
46+
// or in the case where there is no return value, that the calling code doesn't try to use the
47+
// undefined value when it shouldn't.
48+
'typescript/explicit-function-return-type': ['warn', {
49+
allowExpressions: true,
50+
}],
51+
52+
// Require explicit accessibility modifiers on class properties and methods
53+
// This rule aims to make code more readable and explicit about who can use which properties.
54+
'typescript/explicit-member-accessibility': 'warn',
55+
56+
// Require that interface names be prefixed with I
57+
// It can be hard to differentiate between classes and interfaces. Prefixing interfaces with "I"
58+
// can help telling them apart at a glance.
59+
'typescript/interface-name-prefix': ['warn', 'always'],
60+
61+
// Require a specific member delimiter style for interfaces and type literals
62+
// This rule aims to standardise the way interface and type literal members are delimited.
63+
'typescript/member-delimiter-style': ['warn', {
64+
delimiter: 'none',
65+
}],
66+
67+
// Require a consistent member declaration order
68+
// A consistent ordering of fields, methods and constructors can make interfaces, type literals,
69+
// classes and class expressions easier to read, navigate and edit.
70+
'typescript/member-ordering': 'warn',
71+
72+
// Enforces the use of `as Type` assertions instead of `<Type>` assertions
73+
// This rule aims to standardise the use of type assertion style across the codebase.
74+
'typescript/no-angle-bracket-type-assertion': 'warn',
75+
76+
// Disallow generic Array constructors
77+
// Use of the Array constructor to construct a new array is generally discouraged in favor of
78+
// array literal notation because of the single-argument pitfall and because the Array global
79+
// may be redefined.
80+
'typescript/no-array-constructor': 'error',
81+
82+
// Disallow the declaration of empty interfaces
83+
// An empty interface is equivalent to its supertype. If the interface does not implement a
84+
// supertype, then the interface is equivalent to an empty object ({}). In both cases it can be
85+
// omitted.
86+
'typescript/no-empty-interface': 'warn',
87+
88+
// Disallows explicit type declarations for variables or parameters initialized to a number,
89+
// string, or boolean
90+
// This rule disallows explicit type declarations on parameters, variables and properties where
91+
// the type can be easily inferred from its value.
92+
'typescript/no-explicit-any': 'warn',
93+
94+
95+
// Disallow the use of custom TypeScript modules and namespaces
96+
// Custom TypeScript modules (module foo {}) and namespaces (namespace foo {}) are considered
97+
// outdated ways to organize TypeScript code. ES2015 module syntax is now preferred
98+
// (import/export).
99+
'typescript/no-namespace': 'error',
100+
101+
// Disallow non-null assertions using the ! postfix operator
102+
// Using non-null assertions cancels the benefits of the strict null-checking mode.
103+
'typescript/no-non-null-assertion': 'warn',
104+
105+
// Disallow the use of parameter properties in class constructors
106+
// This rule disallows the use of parameter properties in constructors, forcing the user to
107+
// explicitly declare all properties in the class.
108+
'typescript/no-parameter-properties': 'warn',
109+
110+
// Disallow /// <reference path="" /> comments
111+
// Triple-slash reference directive comments should not be used anymore. Use import instead.
112+
'typescript/no-triple-slash-reference': 'error',
113+
114+
// Prevent TypeScript-specific constructs from being erroneously flagged as unused
115+
// This rule only has an effect when the no-unused-vars core rule is enabled. It ensures that
116+
// TypeScript-specific constructs, such as implemented interfaces, are not erroneously flagged
117+
// as unused.
118+
'typescript/no-unused-vars': 'error',
119+
120+
// Disallow the use of variables before they are defined
121+
// This rule will warn when it encounters a reference to an identifier that has not yet been
122+
// declared.
123+
'typescript/no-use-before-define': ['error', {
124+
functions: false,
125+
classes: false,
126+
typedefs: false,
127+
}],
128+
129+
// Disallows the use of require statements except in import statements
130+
// In other words, the use of forms such as var foo = require("foo") are banned. Instead use ES6
131+
// style imports or import foo = require("foo") imports.
132+
'typescript/no-var-requires': 'error',
133+
134+
// Require the use of the namespace keyword instead of the module keyword to declare custom
135+
// TypeScript modules
136+
// In an effort to prevent further confusion between custom TypeScript modules and the new
137+
// ES2015 modules, starting with TypeScript v1.5 the keyword namespace is now the preferred way
138+
// to declare custom TypeScript modules.
139+
'typescript/prefer-namespace-keyword': 'warn',
140+
141+
// Require consistent spacing around type annotations
142+
// This rule aims to enforce specific spacing patterns around type annotations and function
143+
// types in type literals.
144+
'typescript/type-annotation-spacing': 'warn',
145+
},
146+
}

β€Žunused.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,5 +230,13 @@ module.exports = {
230230
// Match test descriptions against a pre-configured regular expression
231231
// Unused, too restrictive.
232232
'mocha/valid-test-description': 0,
233+
234+
// Enforces naming conventions for class members by visibility
235+
// Unused, too restrictive.
236+
'typescript/member-naming': 0,
237+
238+
// Disallow the use of type aliases
239+
// Unused, type aliases seem useful.
240+
'typescript/no-type-alias': 0,
233241
},
234242
}

0 commit comments

Comments
Β (0)