Skip to content

Instantly share code, notes, and snippets.

View phenomnomnominal's full-sized avatar
🥑
Hangry

Craig Spence phenomnomnominal

🥑
Hangry
View GitHub Profile
var getCSSSelector = function () {
var path, node = this;
while (node.length) {
var realNode = node[0], name = realNode.localName;
if (!name) break;
name = name.toLowerCase();
var parent = node.parent();
var sameTagSiblings = parent.children(name);
if (sameTagSiblings.length > 1) {
var esprima = require('esprima');
var esquery = require('esquery');
var ast = esprima.parse('function hello () { }');
var func = esquery(ast, 'FunctionDeclaration');
console.log(ast.body[0] === func); // false;
var oldCreateElement = document.createElement;
document.createElement = function () {
var tagName = arguments[0];
var element = oldCreateElement.apply(document, arguments);
if (tagName === 'script') {
element.setAttribute('defer', '');
}
return element;
};
// 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';
@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) => `
@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 / 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));
}
}
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);
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 => {
// 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.