Skip to content

Instantly share code, notes, and snippets.

@hitode909
Created April 18, 2018 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hitode909/b3ed32a6abd46b8225a98b95eee7e7ad to your computer and use it in GitHub Desktop.
Save hitode909/b3ed32a6abd46b8225a98b95eee7e7ad to your computer and use it in GitHub Desktop.
import * as ts from "typescript";
import * as Lint from "tslint";
export class Rule extends Lint.Rules.AbstractRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "no-jquery-argument",
description: "Disallows receiving jQuery object in arguments.",
rationale: Lint.Utils.dedent`
Do not receive jQuery Object. You must receive HTMLElement or Array<HTMLElement>.
`,
optionsDescription: "Not configurable.",
options: null,
optionExamples: [true],
type: "maintainability",
typescriptOnly: true,
hasFix: false,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING = "Don't receive jQuery Object.";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new DontReceiveJQueryWalker(sourceFile, this.getOptions()));
}
}
class DontReceiveJQueryWalker extends Lint.RuleWalker {
protected visitTypeReference(node: ts.TypeReferenceNode) {
if (node.getText() === 'JQuery') {
const start = node.getStart();
const width = node.getWidth();
const fix = new Lint.Replacement(start, width, 'HTMLElement');
this.addFailureAtNode(node, 'Treat as HTMLElement instead of JQuery.', fix);
}
super.visitTypeReference(node);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment