Skip to content

Instantly share code, notes, and snippets.

Avatar
🥑
Hangry

Craig Spence phenomnomnominal

🥑
Hangry
View GitHub Profile
View get element selector info
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) {
View ESQuery example
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;
View disable-animations-on-low-battery.js
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;
}
View force-defer.js
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;
};
View noTypeofBrowserGlobalRule.spec.ts
// 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 Aug 18, 2018
TSLint rule to avoid accidental `fdescribe` and `fit` calls in test code
View noFdescribeOrFitRule.ts
// 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 Aug 18, 2018
Example of the basic Rule API from TSLint
View rule-api-example.ts
// 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 Aug 27, 2018
Example of the Rule API from TSLint, using a Walker
View rule-with-walker.ts
// 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));
}
}
View walker-api-example.ts
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 Sep 16, 2018
Walking a TypeScript AST and looking for an Identifier inside a CallExpression
View identifier-inside-a-call-expression-example.ts
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!
}