Skip to content

Instantly share code, notes, and snippets.

View OliverJAsh's full-sized avatar

Oliver Joseph Ash OliverJAsh

View GitHub Profile
import { TSESLint, TSESTree } from '@typescript-eslint/utils';
import * as tsutils from 'tsutils';
import * as ts from 'typescript';
import { getChecker, getParserSvc, ruleCreator } from '../utils';
export const RequireObjectTypeAnnotations = ruleCreator({
defaultOptions: [],
meta: {
docs: {

Require type annotations for objects

For object literals without type annotations, many TypeScript features don't work. Contrived example:

type User = { name: string; age: number };

const makeUser = () => ({ name: 'bob', age: 123 });
@OliverJAsh
OliverJAsh / example.md
Created May 4, 2022 06:37
`intlc` example

Given this JSON + ICU Message:

{
  "uploaded": {
    "message": "Your <photoLink>photo</photoLink> has been uploaded.",
    "backend": "tsx"
  }
}
@OliverJAsh
OliverJAsh / foo.ts
Created April 26, 2022 07:39
`no-observable-pipe` ESLint rule
const { getTypeServices } = require('eslint-etc');
const {
ESLintUtils,
TSESTree,
TSESLint,
} = require('@typescript-eslint/experimental-utils');
const ruleCreator = ESLintUtils.RuleCreator(
(_name) =>
// This should be the URL of the docs for this rule, but since this is a custom/local rule, we
@OliverJAsh
OliverJAsh / prettier-diff.sh
Created January 14, 2022 08:59
prettier-diff
#!/bin/bash
# Exit immediately if a command exits with a non-zero status, amongst other improvements
# https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -eux
#
# Example usage:
# ./prettier-diff.sh --no-index my-minified-file-before.js my-minified-file-after.js
#
@OliverJAsh
OliverJAsh / new-jsx-transform.js
Created January 14, 2022 08:51
Old vs new JSX transform
return (0, a.jsxs)(
ae.im,
{
id: s,
style: { marginLeft: -ve },
popoverProps: c,
children: [
(0, a.jsx)(
ae.xz,
{ showDropdownSymbol: !0, className: (0, ne.Bt)({}), children: t(d) },
import { pipe } from 'fp-ts/function';
import * as NonEmptyArray from 'fp-ts/NonEmptyArray';
import * as O from 'fp-ts/Option';
const runMain = () => {
require('./main');
};
const createTimeoutPromise = (timeout: number): Promise<void> =>
new Promise<void>((resolve) =>
@OliverJAsh
OliverJAsh / doc.md
Created June 22, 2021 11:32
Unsplash: Architectural Decision Record (ADR): Mocking API requests in E2E (Cypress) tests

Mocking API requests in E2E (Cypress) tests

Problem

Testing at the network level

In our E2E tests, when the test runner navigates through the site, we don't want to make real API requests but rather we want to mock these requests so that we can easily test many different scenarios, and so that our tests are more resilient and less likely to flake if API is suffering any downtime.

Up until now, this has been achieved by "mocking the fetch function". For example:

function reverseFormatNumber(val,locale){
var parts = new Intl.NumberFormat(locale).formatToParts(1111.1);
var group = parts.find(part => part.type === 'group').value;
var decimal = parts.find(part => part.type === 'decimal').value;
var reversedVal = val.replace(new RegExp('\\' + group, 'g'), '');
reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.');
return Number.isNaN(reversedVal)?0:+reversedVal;
}
console.log(reverseFormatNumber('1,234.56','en'));
@OliverJAsh
OliverJAsh / script.js
Last active January 14, 2022 11:39
webpack: list reasons why a given module was included (dependents)
// @ts-check
const treeify = require('treeify');
/**
* @param {import('webpack').StatsCompilation} stats
* @param {string | number} id
*/
const getModuleById = (stats, id) => stats.modules.find((module) => module.id === id);