Skip to content

Instantly share code, notes, and snippets.

View rbuckton's full-sized avatar

Ron Buckton rbuckton

View GitHub Profile

ECMAScript Regular Expression Language Additions

This is a strawperson for the addition of multiple Regular Expression features popular in various languages and parsers. The primary influences for this proposal come from prior art in the following languages and regular expression engines:

@rbuckton
rbuckton / ts-proposal-range-types.md
Last active November 7, 2023 19:10
Proposal: Range Types for TypeScript

Range Types (T[X:Y]) for TypeScript

A Range Type is a type that produces a new tuple or array type that derives from T by dropping the excess ordered elements of T outside of the range starting at offset X (inclusive) through offset Y (exclusive).

A Range Type is denoted using a : operator in what would otherwise be an Indexed Access Type:

type T = [1, 2, 3][0:2];
@rbuckton
rbuckton / ts-proposal-inverse-offset-types.md
Last active February 11, 2020 20:12
Proposal: Inverse Offset Types for TypeScript

Proposal: Inverse Offset Types (^N) for TypeScript

This proposal adds an Inverse Offset Type to the TypeScript type-space. An Inverse Offset Type is a type notation used to indicate that Type should be considered as an offset from the end of an Array Type or Tuple Type.

An Inverse Offset Type is denoted using a "hat" operator (^) preceding a type:

type T = ^0;
@rbuckton
rbuckton / README.md
Created January 28, 2020 20:07
ECMAScript Strawperson: nameof operator

Purpose:

  • Primarily for developer tooling, such as for "rename" refactoring.

Syntax:

nameof IdentifierName

Examples:

@rbuckton
rbuckton / README.md
Created January 28, 2020 19:57
ECMAScript Strawperson: Concise Method Bodies

Syntax:

// classes
class C {
  #x = 1;
  get x() => this.#x;
  set x(value) => this.#x = value;
  toString() => `C(${this.#x})`;
  static create() => new C();
}
@rbuckton
rbuckton / indexAndInterval.js
Last active August 23, 2019 16:47
Index and Interval types and syntax
// Symbols:
// @@geti - An inverted-get. When present on an object used in an element access, calls this method rather than coercing
// the object to a String.
// ex: `a[b]` --> `b[@@geti](a)`
// @@seti - An inverted-set. When present on an object used in an element access assignment, calls this method rather
// than coercing the object to a String.
// ex: `a[b] = 1` --> `b[@@seti](a, 1)`
// @@indexedGet - Gets a value from an object in relation to a given `Index` instance.
// @@indexedSet - Sets a value on an object in relation to a given `Index` instance.
// @@slice - Gets values from an object in relation to a given `Interval` instance.
export {};
/** A non-existent symbol used to define the types of events defined on an object */
declare const kEvents: unique symbol;
/** Gets the events defined on an object. */
type Events<T> = T extends { readonly [kEvents]: infer Events } ? Events : {};
/** Get the names of events defined on an object. */
type EventNames<T> = keyof Events<T>;
@rbuckton
rbuckton / async-try-using.js
Created July 24, 2019 17:17
try/using semantics
Symbol.asyncDispose = Symbol.for("Symbol.asyncDispose");
function __asyncUsing(obj) {
let state = obj !== null && obj !== undefined ? 0 : 2;
return {
[Symbol.asyncIterator]() { return this; },
async next() {
if (state) return this.return();
state = 1;
return { value: obj };
},
//
// Lexical Grammar
//
SourceCharacter::
> Any unicode code point
WhiteSpace::
<TAB>
<VT>

Decorators, Take 4

A decorator is a function that returns a series of "hooks":

function dec() {
  return [
    { register(klass) { } },
    { register(target, key) { } },
    { wrap(f) { return f; } },
 { initialize(target, key, value) { } },