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;
if (navigator.getBattery) {
navigator.getBattery().then((battery) => {
if (battery && battery.level < 0.2) {
let disableAnimations = document.createElement('style');
disableAnimations.type = 'text/css';
disableAnimations.innerHTML = `
* {
transition-property: none !important;
animation: none !important;
}
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);
@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!
}