Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Changed node kind checks to tsutils function calls, changed option ty…
Browse files Browse the repository at this point in the history
…pe from array to object.
  • Loading branch information
vmk1vmk committed Mar 25, 2019
1 parent d91e52b commit 108d04d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/rules/code-examples/noAny.examples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const codeExamples = [
description:
"Disallows usages of `any` as a type declaration except rest spread parameters.",
config: Lint.Utils.dedent`
"rules": { "no-any": [true, "ignore-rest-args"] }
"rules": { "no-any": [true, { "ignore-rest-args": true }] }
`,
pass: Lint.Utils.dedent`
function foo(a: number, ...rest: any[]): void {
Expand Down
31 changes: 19 additions & 12 deletions src/rules/noAnyRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

import { isArrayTypeNode, isParameterDeclaration } from "tsutils";
import * as ts from "typescript";

import * as Lint from "../index";
Expand All @@ -41,13 +42,15 @@ export class Rule extends Lint.Rules.AbstractRule {
Also see the \`no-unsafe-any\` rule.
`,
optionsDescription: Lint.Utils.dedent`
If \`"${OPTION_IGNORE_REST_ARGS}"\` is provided rest arguments will be ignored.
If \`"${OPTION_IGNORE_REST_ARGS}": true\` is provided rest arguments will be ignored.
`,
options: {
type: "string",
enum: [OPTION_IGNORE_REST_ARGS],
type: "object",
properties: {
[OPTION_IGNORE_REST_ARGS]: { type: "boolean" },
},
},
optionExamples: [true, [true, OPTION_IGNORE_REST_ARGS]],
optionExamples: [true, [true, { [OPTION_IGNORE_REST_ARGS]: true }]],
type: "typescript",
typescriptOnly: true,
codeExamples,
Expand All @@ -58,22 +61,26 @@ export class Rule extends Lint.Rules.AbstractRule {
"Type declaration of 'any' loses type-safety. Consider replacing it with a more precise type.";

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
const options: Options = {
ignoreRestArguments: this.ruleArguments.indexOf(OPTION_IGNORE_REST_ARGS) >= 0,
};

const options: Options = getOptions(this.ruleArguments[0] as Partial<Options> | undefined);
return this.applyWithFunction(sourceFile, walk, options);
}
}

interface Options {
ignoreRestArguments: boolean;
[OPTION_IGNORE_REST_ARGS]: boolean;
}

function getOptions(options: Partial<Options> | undefined): Options {
return {
[OPTION_IGNORE_REST_ARGS]: false,
...options,
};
}

function walk(ctx: Lint.WalkContext<Options>) {
return ts.forEachChild(ctx.sourceFile, function cb(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.AnyKeyword) {
if (ctx.options.ignoreRestArguments && isRestParameterArrayType(node)) {
if (ctx.options[OPTION_IGNORE_REST_ARGS] && isRestParameterArrayType(node)) {
return;
}

Expand All @@ -87,8 +94,8 @@ function walk(ctx: Lint.WalkContext<Options>) {

function isRestParameterArrayType(anyTypeNode: ts.Node) {
return (
anyTypeNode.parent.kind === ts.SyntaxKind.ArrayType &&
anyTypeNode.parent.parent.kind === ts.SyntaxKind.Parameter &&
isArrayTypeNode(anyTypeNode.parent) &&
isParameterDeclaration(anyTypeNode.parent.parent) &&
anyTypeNode.parent.parent.getChildAt(0).kind === ts.SyntaxKind.DotDotDotToken
);
}
2 changes: 1 addition & 1 deletion test/rules/no-any/ignore-rest-args/tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"rules": {
"no-any": [true, "ignore-rest-args"]
"no-any": [true, { "ignore-rest-args": true }]
}
}

0 comments on commit 108d04d

Please sign in to comment.