Skip to content

Instantly share code, notes, and snippets.

View phenomnomnominal's full-sized avatar
🥑
Hangry

Craig Spence phenomnomnominal

🥑
Hangry
View GitHub Profile
it('should not create a lint error if "describe()" is used', () => {
const sourceFile = tsquery.ast(`
describe();
`);
const rule = new Rule({ ruleArguments: [] });
const errors = rule.apply(sourceFile);
expect(errors.length).to.equal(0);
import { tsquery } from '@phenomnomnominal/tsquery';
import { IOptions, Replacement } from 'tslint';
import { expect } from 'chai';
import { Rule } from './noFdescribeOrFitRule';
describe('noFdescribeOrFitRule', () => {
it('should create a lint error if "fdescribe()" is used', () => {
const sourceFile = tsquery.ast(`
fdescribe();
// Dependencies:
import { tsquery } from '@phenomnomnominal/tsquery';
import { Fix, Replacement, RuleFailure, Rules } from 'tslint';
import { SourceFile } from 'typescript';
// Constants:
const FDESCRIBE_FIT_QUERY = 'CallExpression > Identifier[name=/^f(describe|it)$/]';
const FAILURE_MESSAGE = (filter: string) => `
Remember to remove "${filter}" once you have finished working on tests.
import { AbstractWalker } from 'tslint';
import { forEachChild, Node, SourceFile, SyntaxKind } from 'typescript';
const FAILURE_MESSAGE = (filter: string) => `
Remember to remove "${filter}" once you have finished working on tests.
`;
class NoFDescribeOrFItWalker extends AbstractWalker {
public walk (sourceFile: SourceFile) {
const walkNode = (node: Node): void => {
@phenomnomnominal
phenomnomnominal / identifier-inside-a-call-expression-example.ts
Created September 16, 2018 05:41
Walking a TypeScript AST and looking for an Identifier inside a CallExpression
import { AbstractWalker } from 'tslint';
import { forEachChild, Node, SourceFile, SyntaxKind } from 'typescript';
class NoFDescribeOrFItWalker extends AbstractWalker {
public walk (sourceFile: SourceFile) {
const walkNode = (node: Node): void => {
if (node.kind === SyntaxKind.CallExpression) {
if (node.expression.kind === SyntaxKind.Identifier) {
// possible rule match!
}
import { AbstractWalker } from 'tslint';
import { forEachChild, Node, SourceFile } from 'typescript';
class NoFDescribeOrFItWalker extends AbstractWalker {
public walk (sourceFile: SourceFile) {
const walkNode = (node: Node): void => {
// ...
// evaluate rule
// ...
return forEachChild(node, walkNode);
@phenomnomnominal
phenomnomnominal / rule-with-walker.ts
Created August 27, 2018 21:37
Example of the Rule API from TSLint, using a Walker
// Dependencies:
import { RuleFailure, Rules } from 'tslint';
import { SourceFile } from 'typescript';
export class Rule extends Rules.AbstractRule {
public apply (sourceFile: SourceFile): Array<RuleFailure> {
return this.applyWithWalker(new MyCustomRuleWalker(sourceFile));
}
}
@phenomnomnominal
phenomnomnominal / rule-api-example.ts
Last active August 18, 2018 06:43
Example of the basic Rule API from TSLint
// Dependencies:
import { RuleFailure, Rules } from 'tslint';
import { SourceFile } from 'typescript';
export class Rule extends Rules.AbstractRule {
public apply (sourceFile: SourceFile): Array<RuleFailure> {
}
}
@phenomnomnominal
phenomnomnominal / noFdescribeOrFitRule.ts
Last active August 18, 2018 06:41
TSLint rule to avoid accidental `fdescribe` and `fit` calls in test code
// Dependencies:
import { tsquery } from '@phenomnomnominal/tsquery';
import { Fix, Replacement, RuleFailure, Rules } from 'tslint';
import { SourceFile } from 'typescript';
// Constants:
const FDESCRIBE_FIT_QUERY = 'CallExpression > Identifier[name=/^f(describe|it)$/]';
const FAILURE_MESSAGE = (filter: string) => `
// Test Utilities:
import { ast, expect } from './index';
// Under test:
import { Rule } from '../src/noTypeofBrowserGlobalRule';
describe('no-typeof-browser-global', () => {
it('should fail when `typeof` is used to check for "document"', () => {
const sourceFile = ast(`
typeof document === 'undefined';