|
15 | 15 | * limitations under the License.
|
16 | 16 | */
|
17 | 17 |
|
| 18 | +import * as tsutils from "tsutils"; |
18 | 19 | import * as ts from "typescript";
|
19 | 20 |
|
20 | 21 | import * as Lint from "../index";
|
21 | 22 |
|
| 23 | +const OPTION_ALLOW_GENERICS = "allow-generics"; |
| 24 | + |
| 25 | +interface Options { |
| 26 | + allowGenerics: boolean | Set<string>; |
| 27 | +} |
| 28 | + |
| 29 | +type RawOptions = |
| 30 | + | undefined |
| 31 | + | { |
| 32 | + [OPTION_ALLOW_GENERICS]?: boolean | Set<string>; |
| 33 | + }; |
| 34 | + |
| 35 | +type GenericReference = ts.NewExpression | ts.TypeReferenceNode; |
| 36 | + |
22 | 37 | export class Rule extends Lint.Rules.AbstractRule {
|
23 | 38 | /* tslint:disable:object-literal-sort-keys */
|
24 | 39 | public static metadata: Lint.IRuleMetadata = {
|
25 | 40 | ruleName: "invalid-void",
|
26 | 41 | description: Lint.Utils.dedent`
|
27 |
| - Disallows usage of \`void\` type outside of return type. |
| 42 | + Disallows usage of \`void\` type outside of generic or return types. |
28 | 43 | If \`void\` is used as return type, it shouldn't be a part of intersection/union type.`,
|
29 | 44 | rationale: Lint.Utils.dedent`
|
30 | 45 | The \`void\` type means "nothing" or that a function does not return any value,
|
31 | 46 | in contra with implicit \`undefined\` type which means that a function returns a value \`undefined\`.
|
32 | 47 | So "nothing" cannot be mixed with any other types.
|
33 | 48 | If you need this - use \`undefined\` type instead.`,
|
34 | 49 | hasFix: false,
|
35 |
| - optionsDescription: "Not configurable.", |
36 |
| - options: null, |
37 |
| - optionExamples: [true], |
| 50 | + optionsDescription: Lint.Utils.dedent` |
| 51 | + If \`${OPTION_ALLOW_GENERICS}\` is specified as \`false\`, then generic types will no longer be allowed to to be \`void\`. |
| 52 | + Alternately, provide an array of strings for \`${OPTION_ALLOW_GENERICS}\` to exclusively allow generic types by those names.`, |
| 53 | + options: { |
| 54 | + type: "object", |
| 55 | + properties: { |
| 56 | + [OPTION_ALLOW_GENERICS]: { |
| 57 | + oneOf: [ |
| 58 | + { type: "boolean" }, |
| 59 | + { type: "array", items: { type: "string" }, minLength: 1 }, |
| 60 | + ], |
| 61 | + }, |
| 62 | + }, |
| 63 | + additionalProperties: false, |
| 64 | + }, |
| 65 | + optionExamples: [ |
| 66 | + true, |
| 67 | + [true, { [OPTION_ALLOW_GENERICS]: false }], |
| 68 | + [true, { [OPTION_ALLOW_GENERICS]: ["Promise", "PromiseLike"] }], |
| 69 | + ], |
38 | 70 | type: "maintainability",
|
39 | 71 | typescriptOnly: true,
|
40 | 72 | };
|
41 | 73 | /* tslint:enable:object-literal-sort-keys */
|
42 | 74 |
|
43 |
| - public static FAILURE_STRING = "void is not a valid type other than return types"; |
| 75 | + public static FAILURE_STRING_ALLOW_GENERICS = |
| 76 | + "void is only valid as a return type or generic type variable"; |
| 77 | + public static FAILURE_STRING_NO_GENERICS = "void is only valid as a return type"; |
| 78 | + public static FAILURE_WRONG_GENERIC = (genericName: string) => |
| 79 | + `${genericName} may not have void as a type variable`; |
44 | 80 |
|
45 | 81 | public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
|
46 |
| - return this.applyWithFunction(sourceFile, walk); |
| 82 | + return this.applyWithFunction(sourceFile, walk, { |
| 83 | + // tslint:disable-next-line:no-object-literal-type-assertion |
| 84 | + allowGenerics: this.getAllowGenerics(this.ruleArguments[0] as RawOptions), |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + private getAllowGenerics(rawArgument: RawOptions) { |
| 89 | + if (rawArgument == undefined) { |
| 90 | + return true; |
| 91 | + } |
| 92 | + |
| 93 | + const allowGenerics = rawArgument[OPTION_ALLOW_GENERICS]; |
| 94 | + |
| 95 | + return allowGenerics instanceof Array ? new Set(allowGenerics) : Boolean(allowGenerics); |
47 | 96 | }
|
48 | 97 | }
|
49 | 98 |
|
@@ -75,10 +124,60 @@ const failedKinds = new Set([
|
75 | 124 | ts.SyntaxKind.CallExpression,
|
76 | 125 | ]);
|
77 | 126 |
|
78 |
| -function walk(ctx: Lint.WalkContext): void { |
| 127 | +function walk(ctx: Lint.WalkContext<Options>): void { |
| 128 | + const defaultFailureString = ctx.options.allowGenerics |
| 129 | + ? Rule.FAILURE_STRING_ALLOW_GENERICS |
| 130 | + : Rule.FAILURE_STRING_NO_GENERICS; |
| 131 | + |
| 132 | + const getGenericReferenceName = (node: GenericReference) => { |
| 133 | + const rawName = tsutils.isNewExpression(node) ? node.expression : node.typeName; |
| 134 | + |
| 135 | + return tsutils.isIdentifier(rawName) ? rawName.text : rawName.getText(ctx.sourceFile); |
| 136 | + }; |
| 137 | + |
| 138 | + const getTypeReferenceFailure = (node: GenericReference) => { |
| 139 | + if (!(ctx.options.allowGenerics instanceof Set)) { |
| 140 | + return ctx.options.allowGenerics ? undefined : defaultFailureString; |
| 141 | + } |
| 142 | + |
| 143 | + const genericName = getGenericReferenceName(node); |
| 144 | + |
| 145 | + return ctx.options.allowGenerics.has(genericName) |
| 146 | + ? undefined |
| 147 | + : Rule.FAILURE_WRONG_GENERIC(genericName); |
| 148 | + }; |
| 149 | + |
| 150 | + const checkTypeReference = (parent: GenericReference, node: ts.Node) => { |
| 151 | + const failure = getTypeReferenceFailure(parent); |
| 152 | + |
| 153 | + if (failure !== undefined) { |
| 154 | + ctx.addFailureAtNode(node, failure); |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + const isParentGenericReference = ( |
| 159 | + parent: ts.Node, |
| 160 | + node: ts.Node, |
| 161 | + ): parent is GenericReference => { |
| 162 | + if (tsutils.isTypeReferenceNode(parent)) { |
| 163 | + return true; |
| 164 | + } |
| 165 | + |
| 166 | + return ( |
| 167 | + tsutils.isNewExpression(parent) && |
| 168 | + parent.typeArguments !== undefined && |
| 169 | + ts.isTypeNode(node) && |
| 170 | + parent.typeArguments.indexOf(node) !== -1 |
| 171 | + ); |
| 172 | + }; |
| 173 | + |
79 | 174 | ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node) {
|
80 | 175 | if (node.kind === ts.SyntaxKind.VoidKeyword && failedKinds.has(node.parent.kind)) {
|
81 |
| - ctx.addFailureAtNode(node, Rule.FAILURE_STRING); |
| 176 | + if (isParentGenericReference(node.parent, node)) { |
| 177 | + checkTypeReference(node.parent, node); |
| 178 | + } else { |
| 179 | + ctx.addFailureAtNode(node, defaultFailureString); |
| 180 | + } |
82 | 181 | }
|
83 | 182 |
|
84 | 183 | ts.forEachChild(node, cb);
|
|
0 commit comments