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

New Rule: Disallows usages of <> type assertions before a object literal expression #1213

Closed
Fank opened this issue May 6, 2016 · 0 comments

Comments

@Fank
Copy link

Fank commented May 6, 2016

In typescript you can do following:

interface INxGridColumnConf {
    name: string;
    width: number;
}

let conf: INxGridColumnConf = <INxGridColumnConf>{}; // This type if syntax should be forbidden
conf.name = column.name;
conf.width = column.width;

because the interface INxGridColumnConf requires the key name and width

Here is an draft example how it maybe can done:

import * as ts from 'typescript';
import * as Lint from 'tslint/lib/lint';


export class Rule extends Lint.Rules.AbstractRule {
    public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
        return this.applyWithWalker(new NegSoftDereferencingWalker(sourceFile, this.getOptions()));
    }
}

class NegSoftDereferencingWalker extends Lint.RuleWalker {
    public visitObjectLiteralExpression(node: ts.ObjectLiteralExpression) {
        const parent = <ts.TypeAssertion>node.parent;

        if (
            parent.kind === ts.SyntaxKind.TypeAssertionExpression
            // && parent.type.kind === ts.SyntaxKind.TypeReference // custom type only
        ) {
            this.addFailure(this.createFailure(parent.getStart(), parent.type.getWidth() + 2, `type assertion is forbidden before object literals`));
        }

        super.visitObjectLiteralExpression(node);
    }
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants