Skip to content

Commit

Permalink
[BREAKING] [no-type-alias] simplify config (typescript-eslint#259)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bradzacher authored and JamesHenry committed Jan 18, 2019
1 parent 0a3fae0 commit 1dfc2bd
Show file tree
Hide file tree
Showing 4 changed files with 1,036 additions and 920 deletions.
24 changes: 12 additions & 12 deletions packages/eslint-plugin-typescript/docs/rules/no-type-alias.md
Expand Up @@ -82,23 +82,23 @@ and simplified types (primitives, tuples, unions, intersections, etc).
This rule, in its default state, does not require any argument. If you would like to enable one
or more of the following you may pass an object with the options set as follows:

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

### allowAliases

This applies to primitive types and reference types.

The setting accepts the following values:

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

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

```ts
// primitives
Expand Down Expand Up @@ -231,9 +231,9 @@ This applies to function types.

The setting accepts the following values:

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

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

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

The setting accepts the following options:

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

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

```ts
type Foo = {};
Expand Down Expand Up @@ -360,12 +360,12 @@ This applies to literal types.

The setting accepts the following values:

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

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

```ts
type Foo<T> = { readonly [P in keyof T]: T[P] };
Expand Down
109 changes: 51 additions & 58 deletions packages/eslint-plugin-typescript/lib/rules/no-type-alias.js
Expand Up @@ -19,14 +19,17 @@ module.exports = {
category: "TypeScript",
url: util.metaDocsUrl("no-type-alias"),
},
messages: {
noTypeAlias: "Type {{alias}} are not allowed.",
noCompositionAlias:
"{{typeName}} in {{compositionType}} types are not allowed.",
},
schema: [
{
type: "object",
properties: {
allowAliases: {
enum: [
true,
false,
"always",
"never",
"in-unions",
Expand All @@ -35,12 +38,10 @@ module.exports = {
],
},
allowCallbacks: {
enum: [true, false, "always", "never"],
enum: ["always", "never"],
},
allowLiterals: {
enum: [
true,
false,
"always",
"never",
"in-unions",
Expand All @@ -50,8 +51,6 @@ module.exports = {
},
allowMappedTypes: {
enum: [
true,
false,
"always",
"never",
"in-unions",
Expand All @@ -66,22 +65,15 @@ module.exports = {
},

create(context) {
const options = context.options[0];
const options = context.options[0] || {};

const allowAliases = (options && options.allowAliases) || "never";
const allowCallbacks = (options && options.allowCallbacks) || "never";
const allowLiterals = (options && options.allowLiterals) || "never";
const allowMappedTypes =
(options && options.allowMappedTypes) || "never";
const allowAliases = options.allowAliases || "never";
const allowCallbacks = options.allowCallbacks || "never";
const allowLiterals = options.allowLiterals || "never";
const allowMappedTypes = options.allowMappedTypes || "never";

const unions = [
true,
"always",
"in-unions",
"in-unions-and-intersections",
];
const unions = ["always", "in-unions", "in-unions-and-intersections"];
const intersections = [
true,
"always",
"in-intersections",
"in-unions-and-intersections",
Expand Down Expand Up @@ -181,27 +173,36 @@ module.exports = {

/**
* Gets the message to be displayed based on the node type and whether the node is a top level declaration.
* @param {Object} node the location
* @param {string} compositionType the type of composition this alias is part of (undefined if not
* part of a composition)
* @param {boolean} isRoot a flag indicating we are dealing with the top level declaration.
* @param {string} type the kind of type alias being validated.
* @returns {string} the message to be displayed.
* @private
*/
function getMessage(compositionType, isRoot, type) {
function getMessage(node, compositionType, isRoot, type) {
if (isRoot) {
return type
? `Type ${type} are not allowed.`
: "Type aliases are not allowed.";
return {
node,
messageId: "noTypeAlias",
data: {
alias: type || "aliases",
},
};
}

return compositionType === "TSUnionType"
? `${type[0].toUpperCase()}${type.slice(
1
)} in union types are not allowed.`
: `${type[0].toUpperCase()}${type.slice(
1
)} in intersection types are not allowed.`;
return {
node,
messageId: "noCompositionAlias",
data: {
compositionType:
compositionType === "TSUnionType"
? "union"
: "intersection",
typeName: util.upperCaseFirst(type),
},
};
}

/**
Expand All @@ -223,25 +224,20 @@ module.exports = {
allowAliases
)
) {
context.report({
node,
message: getMessage(
compositionType,
isTopLevel,
"aliases"
),
});
context.report(
getMessage(node, compositionType, isTopLevel, "aliases")
);
}
} else if (isCallback(node)) {
if (allowCallbacks === "never") {
context.report({
node,
message: getMessage(
context.report(
getMessage(
node,
compositionType,
isTopLevel,
"callbacks"
),
});
)
);
}
} else if (isLiteral(node)) {
if (
Expand All @@ -252,14 +248,14 @@ module.exports = {
allowLiterals
)
) {
context.report({
node,
message: getMessage(
context.report(
getMessage(
node,
compositionType,
isTopLevel,
"literals"
),
});
)
);
}
} else if (isMappedType(node)) {
if (
Expand All @@ -270,20 +266,17 @@ module.exports = {
allowMappedTypes
)
) {
context.report({
node,
message: getMessage(
context.report(
getMessage(
node,
compositionType,
isTopLevel,
"mapped types"
),
});
)
);
}
} else {
context.report({
node,
message: getMessage(compositionType, isTopLevel),
});
context.report(getMessage(node, compositionType, isTopLevel));
}
}

Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin-typescript/lib/util.js
Expand Up @@ -62,3 +62,13 @@ function deepMerge(first = {}, second = {}) {
}, {});
}
exports.deepMerge = deepMerge;

/**
* Upper cases the first character or the string
* @param {string} str a string
* @returns {string} upper case first
*/
function upperCaseFirst(str) {
return str[0].toUpperCase() + str.slice(1);
}
exports.upperCaseFirst = upperCaseFirst;

0 comments on commit 1dfc2bd

Please sign in to comment.