Skip to content

Commit 1dfc2bd

Browse files
bradzacherJamesHenry
authored andcommitted
[BREAKING] [no-type-alias] simplify config (typescript-eslint#259)
In standardising our config, we should *either* use booleans, or strings. Offering the ability to configure the rule via `true`/`false` as well as `"always"`/`"never"` makes the docs harder to understand. - [breaking] remove true/false options - cleanup tests/docs - switch to messageId
1 parent 0a3fae0 commit 1dfc2bd

File tree

4 files changed

+1036
-920
lines changed

4 files changed

+1036
-920
lines changed

Diff for: packages/eslint-plugin-typescript/docs/rules/no-type-alias.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -82,23 +82,23 @@ and simplified types (primitives, tuples, unions, intersections, etc).
8282
This rule, in its default state, does not require any argument. If you would like to enable one
8383
or more of the following you may pass an object with the options set as follows:
8484

85-
- `allowAliases` set to `true` or `"always"` will allow you to do aliasing (Defaults to `false`/`"never"`).
86-
- `allowCallbacks` set to `true` or `"always"` will allow you to use type aliases with callbacks (Defaults to `false`/`"never"`)
87-
- `allowLiterals` set to `true` or `"always"` will allow you to use type aliases with literal objects (Defaults to `false`/`"never"`)
88-
- `allowMappedTypes` set to `true` or `"always"` will allow you to use type aliases as mapping tools (Defaults to `false`/`"never"`)
85+
- `allowAliases` set to `"always"` will allow you to do aliasing (Defaults to `"never"`).
86+
- `allowCallbacks` set to `"always"` will allow you to use type aliases with callbacks (Defaults to `"never"`)
87+
- `allowLiterals` set to `"always"` will allow you to use type aliases with literal objects (Defaults to `"never"`)
88+
- `allowMappedTypes` set to `"always"` will allow you to use type aliases as mapping tools (Defaults to `"never"`)
8989

9090
### allowAliases
9191

9292
This applies to primitive types and reference types.
9393

9494
The setting accepts the following values:
9595

96-
- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature.
96+
- `"always"` or `"never"` to active or deactivate the feature.
9797
- `"in-unions"`, allows aliasing in union statements, e.g. `type Foo = string | string[];`
9898
- `"in-intersections"`, allows aliasing in intersection statements, e.g. `type Foo = string & string[];`
9999
- `"in-unions-and-intersections"`, allows aliasing in union and/or intersection statements.
100100

101-
Examples of **correct** code for the `{ "allowAliases": true }` or `{ "allowAliases": "always" }` options:
101+
Examples of **correct** code for the `{ "allowAliases": "always" }` options:
102102

103103
```ts
104104
// primitives
@@ -231,9 +231,9 @@ This applies to function types.
231231

232232
The setting accepts the following values:
233233

234-
- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature.
234+
- `"always"` or `"never"` to active or deactivate the feature.
235235

236-
Examples of **correct** code for the `{ "allowCallbacks": true }` or `{ "allowCallbacks": "always" }` option:
236+
Examples of **correct** code for the `{ "allowCallbacks": "always" }` option:
237237

238238
```ts
239239
type Foo = () => void;
@@ -253,12 +253,12 @@ This applies to literal types (`type Foo = { ... }`).
253253

254254
The setting accepts the following options:
255255

256-
- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature.
256+
- `"always"` or `"never"` to active or deactivate the feature.
257257
- `"in-unions"`, allows literals in union statements, e.g. `type Foo = string | string[];`
258258
- `"in-intersections"`, allows literals in intersection statements, e.g. `type Foo = string & string[];`
259259
- `"in-unions-and-intersections"`, allows literals in union and/or intersection statements.
260260

261-
Examples of **correct** code for the `{ "allowLiterals": true }` or `{ "allowLiterals": "always" }` options:
261+
Examples of **correct** code for the `{ "allowLiterals": "always" }` options:
262262

263263
```ts
264264
type Foo = {};
@@ -360,12 +360,12 @@ This applies to literal types.
360360

361361
The setting accepts the following values:
362362

363-
- `true` or `false` (`"always"` or `"never"`) to active or deactivate the feature.
363+
- `"always"` or `"never"` to active or deactivate the feature.
364364
- `"in-unions"`, allows aliasing in union statements, e.g. `type Foo = string | string[];`
365365
- `"in-intersections"`, allows aliasing in intersection statements, e.g. `type Foo = string & string[];`
366366
- `"in-unions-and-intersections"`, allows aliasing in union and/or intersection statements.
367367

368-
Examples of **correct** code for the `{ "allowMappedTypes": true }` or `{ "allowMappedTypes": "always" }` options:
368+
Examples of **correct** code for the `{ "allowMappedTypes": "always" }` options:
369369

370370
```ts
371371
type Foo<T> = { readonly [P in keyof T]: T[P] };

Diff for: packages/eslint-plugin-typescript/lib/rules/no-type-alias.js

+51-58
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@ module.exports = {
1919
category: "TypeScript",
2020
url: util.metaDocsUrl("no-type-alias"),
2121
},
22+
messages: {
23+
noTypeAlias: "Type {{alias}} are not allowed.",
24+
noCompositionAlias:
25+
"{{typeName}} in {{compositionType}} types are not allowed.",
26+
},
2227
schema: [
2328
{
2429
type: "object",
2530
properties: {
2631
allowAliases: {
2732
enum: [
28-
true,
29-
false,
3033
"always",
3134
"never",
3235
"in-unions",
@@ -35,12 +38,10 @@ module.exports = {
3538
],
3639
},
3740
allowCallbacks: {
38-
enum: [true, false, "always", "never"],
41+
enum: ["always", "never"],
3942
},
4043
allowLiterals: {
4144
enum: [
42-
true,
43-
false,
4445
"always",
4546
"never",
4647
"in-unions",
@@ -50,8 +51,6 @@ module.exports = {
5051
},
5152
allowMappedTypes: {
5253
enum: [
53-
true,
54-
false,
5554
"always",
5655
"never",
5756
"in-unions",
@@ -66,22 +65,15 @@ module.exports = {
6665
},
6766

6867
create(context) {
69-
const options = context.options[0];
68+
const options = context.options[0] || {};
7069

71-
const allowAliases = (options && options.allowAliases) || "never";
72-
const allowCallbacks = (options && options.allowCallbacks) || "never";
73-
const allowLiterals = (options && options.allowLiterals) || "never";
74-
const allowMappedTypes =
75-
(options && options.allowMappedTypes) || "never";
70+
const allowAliases = options.allowAliases || "never";
71+
const allowCallbacks = options.allowCallbacks || "never";
72+
const allowLiterals = options.allowLiterals || "never";
73+
const allowMappedTypes = options.allowMappedTypes || "never";
7674

77-
const unions = [
78-
true,
79-
"always",
80-
"in-unions",
81-
"in-unions-and-intersections",
82-
];
75+
const unions = ["always", "in-unions", "in-unions-and-intersections"];
8376
const intersections = [
84-
true,
8577
"always",
8678
"in-intersections",
8779
"in-unions-and-intersections",
@@ -181,27 +173,36 @@ module.exports = {
181173

182174
/**
183175
* Gets the message to be displayed based on the node type and whether the node is a top level declaration.
176+
* @param {Object} node the location
184177
* @param {string} compositionType the type of composition this alias is part of (undefined if not
185178
* part of a composition)
186179
* @param {boolean} isRoot a flag indicating we are dealing with the top level declaration.
187180
* @param {string} type the kind of type alias being validated.
188181
* @returns {string} the message to be displayed.
189182
* @private
190183
*/
191-
function getMessage(compositionType, isRoot, type) {
184+
function getMessage(node, compositionType, isRoot, type) {
192185
if (isRoot) {
193-
return type
194-
? `Type ${type} are not allowed.`
195-
: "Type aliases are not allowed.";
186+
return {
187+
node,
188+
messageId: "noTypeAlias",
189+
data: {
190+
alias: type || "aliases",
191+
},
192+
};
196193
}
197194

198-
return compositionType === "TSUnionType"
199-
? `${type[0].toUpperCase()}${type.slice(
200-
1
201-
)} in union types are not allowed.`
202-
: `${type[0].toUpperCase()}${type.slice(
203-
1
204-
)} in intersection types are not allowed.`;
195+
return {
196+
node,
197+
messageId: "noCompositionAlias",
198+
data: {
199+
compositionType:
200+
compositionType === "TSUnionType"
201+
? "union"
202+
: "intersection",
203+
typeName: util.upperCaseFirst(type),
204+
},
205+
};
205206
}
206207

207208
/**
@@ -223,25 +224,20 @@ module.exports = {
223224
allowAliases
224225
)
225226
) {
226-
context.report({
227-
node,
228-
message: getMessage(
229-
compositionType,
230-
isTopLevel,
231-
"aliases"
232-
),
233-
});
227+
context.report(
228+
getMessage(node, compositionType, isTopLevel, "aliases")
229+
);
234230
}
235231
} else if (isCallback(node)) {
236232
if (allowCallbacks === "never") {
237-
context.report({
238-
node,
239-
message: getMessage(
233+
context.report(
234+
getMessage(
235+
node,
240236
compositionType,
241237
isTopLevel,
242238
"callbacks"
243-
),
244-
});
239+
)
240+
);
245241
}
246242
} else if (isLiteral(node)) {
247243
if (
@@ -252,14 +248,14 @@ module.exports = {
252248
allowLiterals
253249
)
254250
) {
255-
context.report({
256-
node,
257-
message: getMessage(
251+
context.report(
252+
getMessage(
253+
node,
258254
compositionType,
259255
isTopLevel,
260256
"literals"
261-
),
262-
});
257+
)
258+
);
263259
}
264260
} else if (isMappedType(node)) {
265261
if (
@@ -270,20 +266,17 @@ module.exports = {
270266
allowMappedTypes
271267
)
272268
) {
273-
context.report({
274-
node,
275-
message: getMessage(
269+
context.report(
270+
getMessage(
271+
node,
276272
compositionType,
277273
isTopLevel,
278274
"mapped types"
279-
),
280-
});
275+
)
276+
);
281277
}
282278
} else {
283-
context.report({
284-
node,
285-
message: getMessage(compositionType, isTopLevel),
286-
});
279+
context.report(getMessage(node, compositionType, isTopLevel));
287280
}
288281
}
289282

Diff for: packages/eslint-plugin-typescript/lib/util.js

+10
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,13 @@ function deepMerge(first = {}, second = {}) {
6262
}, {});
6363
}
6464
exports.deepMerge = deepMerge;
65+
66+
/**
67+
* Upper cases the first character or the string
68+
* @param {string} str a string
69+
* @returns {string} upper case first
70+
*/
71+
function upperCaseFirst(str) {
72+
return str[0].toUpperCase() + str.slice(1);
73+
}
74+
exports.upperCaseFirst = upperCaseFirst;

0 commit comments

Comments
 (0)