bunch of easy ones

pull/8569/head
Simon Holthausen 3 years ago
parent 7c85039471
commit e86fb16402

@ -9,7 +9,7 @@ see: https://github.com/microsoft/TypeScript/tree/main/lib
import http from 'https';
import fs from 'fs';
const GLOBAL_TS_PATH = './src/compiler/utils/globals.ts';
const GLOBAL_TS_PATH = './src/compiler/utils/globals.js';
// MEMO: add additional objects/functions which existed in `src/compiler/utils/names.ts`
// before this script was introduced but could not be retrieved by this process.

@ -0,0 +1,33 @@
/**
* @template T
* @overload
* @param {T[][]} nodes
* @param {T[]} [target]
* @returns {T[]}
*/
/**
* @template T
* @overload
* @param {T[]} nodes
* @param {T[]} [target]
* @returns {T[]}
*/
/**
* @param {any[]} nodes
* @param {any[]} [target]
* @returns {any[]}
*/
export function flatten(nodes, target = []) {
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
if (Array.isArray(node)) {
flatten(node, target);
} else {
target.push(node);
}
}
return target;
}

@ -1,14 +0,0 @@
export function flatten<T>(nodes: T[][], target?: T[]): T[];
export function flatten<T>(nodes: T[], target?: T[]): T[];
export function flatten(nodes: any[], target: any[] = []): any[] {
for (let i = 0; i < nodes.length; i += 1) {
const node = nodes[i];
if (Array.isArray(node)) {
flatten(node, target);
} else {
target.push(node);
}
}
return target;
}

@ -1,7 +1,12 @@
// Adapted from https://github.com/acornjs/acorn/blob/6584815dca7440e00de841d1dad152302fdd7ca5/src/tokenize.js
// Reproduced under MIT License https://github.com/acornjs/acorn/blob/master/LICENSE
export default function full_char_code_at(str: string, i: number): number {
/**
* @param {string} str
* @param {number} i
* @returns {number}
*/
export default function full_char_code_at(str, i) {
const code = str.charCodeAt(i);
if (code <= 0xd7ff || code >= 0xe000) return code;

@ -1,6 +1,6 @@
/** ----------------------------------------------------------------------
This file is automatically generated by `scripts/globals-extractor.mjs`.
Generated At: 2022-09-03T15:22:37.415Z
Generated At: 2023-05-11T15:31:28.406Z
---------------------------------------------------------------------- */
export default new Set([

@ -0,0 +1,9 @@
/**
* @template {{ next?: T; prev?: T }} T
* @param {T} next
* @param {T} prev
*/
export function link(next, prev) {
prev.next = next;
if (next) next.prev = prev;
}

@ -1,4 +0,0 @@
export function link<T extends { next?: T; prev?: T }>(next: T, prev: T) {
prev.next = next;
if (next) next.prev = prev;
}

@ -1,4 +1,8 @@
export default function list(items: string[], conjunction = 'or') {
/**
* @param {string[]} items
* @param {string} [conjunction]
*/
export default function list(items, conjunction = 'or') {
if (items.length === 1) return items[0];
return `${items.slice(0, -1).join(', ')} ${conjunction} ${items[items.length - 1]}`;
}

@ -1,6 +1,6 @@
import { isIdentifierStart, isIdentifierChar } from 'acorn';
import full_char_code_at from './full_char_code_at';
import { regex_starts_with_underscore, regex_ends_with_underscore } from './patterns';
import full_char_code_at from './full_char_code_at.js';
import { regex_starts_with_underscore, regex_ends_with_underscore } from './patterns.js';
export const reserved = new Set([
'arguments',
@ -53,7 +53,11 @@ export const reserved = new Set([
'yield'
]);
export function is_valid(str: string): boolean {
/**
* @param {string} str
* @returns {boolean}
*/
export function is_valid(str) {
let i = 0;
while (i < str.length) {
@ -69,7 +73,10 @@ export function is_valid(str: string): boolean {
const regex_non_standard_characters = /[^a-zA-Z0-9_]+/g;
const regex_starts_with_number = /^[0-9]/;
export function sanitize(name: string) {
/**
* @param {string} name
*/
export function sanitize(name) {
return name
.replace(regex_non_standard_characters, '_')
.replace(regex_starts_with_underscore, '')

@ -25,4 +25,4 @@ export const valid_namespaces = [
xmlns
];
export const namespaces = { foreign, html, mathml, svg, xlink, xml, xmlns } as const;
export const namespaces = /** @type {const} */ ({ foreign, html, mathml, svg, xlink, xml, xmlns });

@ -2,10 +2,11 @@
* Pushes all `items` into `array` using `push`, therefore mutating the array.
* We do this for memory and perf reasons, and because `array.push(...items)` would
* run into a "max call stack size exceeded" error with too many items (~65k).
* @param array
* @param items
* @template T
* @param {T[]} array
* @param {T[]} items
*/
export function push_array<T>(array: T[], items: T[]): void {
export function push_array(array, items) {
for (let i = 0; i < items.length; i++) {
array.push(items[i]);
}

@ -1,9 +1,15 @@
import { regex_starts_with_whitespaces, regex_ends_with_whitespaces } from './patterns';
import { regex_starts_with_whitespaces, regex_ends_with_whitespaces } from './patterns.js';
export function trim_start(str: string) {
/**
* @param {string} str
*/
export function trim_start(str) {
return str.replace(regex_starts_with_whitespaces, '');
}
export function trim_end(str: string) {
/**
* @param {string} str
*/
export function trim_end(str) {
return str.replace(regex_ends_with_whitespaces, '');
}
Loading…
Cancel
Save