Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

convert prefer-array-literal rule to use a walk function #775

Merged
merged 1 commit into from
Jan 7, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 43 additions & 21 deletions src/preferArrayLiteralRule.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

import { AstUtils } from './utils/AstUtils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
import { isObject } from './utils/TypeGuard';

interface Options {
allowTypeParameters: boolean;
}
export class Rule extends Lint.Rules.AbstractRule {
public static metadata: ExtendedMetadata = {
ruleName: 'prefer-array-literal',
Expand All @@ -25,38 +29,56 @@ export class Rule extends Lint.Rules.AbstractRule {
public static CONSTRUCTOR_FAILURE_STRING: string = 'Replace Array constructor with an array literal: ';

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new NoGenericArrayWalker(sourceFile, this.getOptions()));
return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
}
}

class NoGenericArrayWalker extends Lint.RuleWalker {
private allowTypeParameters: boolean = false;
private parseOptions(options: Lint.IOptions): Options {
let value: boolean = false;
let ruleOptions: any[] = [];

if (options.ruleArguments instanceof Array) {
ruleOptions = options.ruleArguments;
}

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
this.getOptions().forEach((opt: unknown) => {
if (options instanceof Array) {
ruleOptions = options;
}

ruleOptions.forEach((opt: unknown) => {
if (isObject(opt)) {
this.allowTypeParameters = opt['allow-type-parameters'] === true;
value = opt['allow-type-parameters'] === true;
}
});

return {
allowTypeParameters: value
};
}
}

function walk(ctx: Lint.WalkContext<Options>) {
const { allowTypeParameters } = ctx.options;

protected visitTypeReference(node: ts.TypeReferenceNode): void {
if (this.allowTypeParameters === false) {
if ((<ts.Identifier>node.typeName).text === 'Array') {
const failureString = Rule.GENERICS_FAILURE_STRING + node.getText();
this.addFailureAt(node.getStart(), node.getWidth(), failureString);
function cb(node: ts.Node): void {
if (tsutils.isTypeReferenceNode(node)) {
if (!allowTypeParameters) {
if ((<ts.Identifier>node.typeName).text === 'Array') {
const failureString = Rule.GENERICS_FAILURE_STRING + node.getText();
ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
}
}
}
super.visitTypeReference(node);
}

protected visitNewExpression(node: ts.NewExpression): void {
const functionName = AstUtils.getFunctionName(node);
if (functionName === 'Array') {
const failureString = Rule.CONSTRUCTOR_FAILURE_STRING + node.getText();
this.addFailureAt(node.getStart(), node.getWidth(), failureString);
if (tsutils.isNewExpression(node)) {
const functionName = AstUtils.getFunctionName(node);
if (functionName === 'Array') {
const failureString = Rule.CONSTRUCTOR_FAILURE_STRING + node.getText();
ctx.addFailureAt(node.getStart(), node.getWidth(), failureString);
}
}
super.visitNewExpression(node);

return ts.forEachChild(node, cb);
}

return ts.forEachChild(ctx.sourceFile, cb);
}