From e86fb16402da2519861d3fced483e60e2630453e Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Thu, 11 May 2023 17:38:20 +0200 Subject: [PATCH] bunch of easy ones --- scripts/globals-extractor.mjs | 2 +- src/compiler/utils/{clone.ts => clone.js} | 0 src/compiler/utils/flatten.js | 33 +++++++++++++++++++ src/compiler/utils/flatten.ts | 14 -------- ...l_char_code_at.ts => full_char_code_at.js} | 7 +++- src/compiler/utils/{globals.ts => globals.js} | 2 +- src/compiler/utils/link.js | 9 +++++ src/compiler/utils/link.ts | 4 --- src/compiler/utils/{list.ts => list.js} | 6 +++- src/compiler/utils/{names.ts => names.js} | 15 ++++++--- .../utils/{namespaces.ts => namespaces.js} | 2 +- .../utils/{nodes_match.ts => nodes_match.js} | 0 .../utils/{patterns.ts => patterns.js} | 0 .../utils/{push_array.ts => push_array.js} | 7 ++-- src/compiler/utils/{trim.ts => trim.js} | 12 +++++-- 15 files changed, 80 insertions(+), 33 deletions(-) rename src/compiler/utils/{clone.ts => clone.js} (100%) create mode 100644 src/compiler/utils/flatten.js delete mode 100644 src/compiler/utils/flatten.ts rename src/compiler/utils/{full_char_code_at.ts => full_char_code_at.js} (74%) rename src/compiler/utils/{globals.ts => globals.js} (99%) create mode 100644 src/compiler/utils/link.js delete mode 100644 src/compiler/utils/link.ts rename src/compiler/utils/{list.ts => list.js} (50%) rename src/compiler/utils/{names.ts => names.js} (83%) rename src/compiler/utils/{namespaces.ts => namespaces.js} (87%) rename src/compiler/utils/{nodes_match.ts => nodes_match.js} (100%) rename src/compiler/utils/{patterns.ts => patterns.js} (100%) rename src/compiler/utils/{push_array.ts => push_array.js} (75%) rename src/compiler/utils/{trim.ts => trim.js} (53%) diff --git a/scripts/globals-extractor.mjs b/scripts/globals-extractor.mjs index 47dbc240ff..b9706dfb92 100644 --- a/scripts/globals-extractor.mjs +++ b/scripts/globals-extractor.mjs @@ -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. diff --git a/src/compiler/utils/clone.ts b/src/compiler/utils/clone.js similarity index 100% rename from src/compiler/utils/clone.ts rename to src/compiler/utils/clone.js diff --git a/src/compiler/utils/flatten.js b/src/compiler/utils/flatten.js new file mode 100644 index 0000000000..b4af1246d7 --- /dev/null +++ b/src/compiler/utils/flatten.js @@ -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; +} diff --git a/src/compiler/utils/flatten.ts b/src/compiler/utils/flatten.ts deleted file mode 100644 index 53557a2eee..0000000000 --- a/src/compiler/utils/flatten.ts +++ /dev/null @@ -1,14 +0,0 @@ -export function flatten(nodes: T[][], target?: T[]): T[]; -export function flatten(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; -} diff --git a/src/compiler/utils/full_char_code_at.ts b/src/compiler/utils/full_char_code_at.js similarity index 74% rename from src/compiler/utils/full_char_code_at.ts rename to src/compiler/utils/full_char_code_at.js index 0469b537fc..90e6b8def3 100644 --- a/src/compiler/utils/full_char_code_at.ts +++ b/src/compiler/utils/full_char_code_at.js @@ -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; diff --git a/src/compiler/utils/globals.ts b/src/compiler/utils/globals.js similarity index 99% rename from src/compiler/utils/globals.ts rename to src/compiler/utils/globals.js index ee43af3b91..020a0738e1 100644 --- a/src/compiler/utils/globals.ts +++ b/src/compiler/utils/globals.js @@ -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([ diff --git a/src/compiler/utils/link.js b/src/compiler/utils/link.js new file mode 100644 index 0000000000..680fd4fdc4 --- /dev/null +++ b/src/compiler/utils/link.js @@ -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; +} diff --git a/src/compiler/utils/link.ts b/src/compiler/utils/link.ts deleted file mode 100644 index 7cdef75d94..0000000000 --- a/src/compiler/utils/link.ts +++ /dev/null @@ -1,4 +0,0 @@ -export function link(next: T, prev: T) { - prev.next = next; - if (next) next.prev = prev; -} diff --git a/src/compiler/utils/list.ts b/src/compiler/utils/list.js similarity index 50% rename from src/compiler/utils/list.ts rename to src/compiler/utils/list.js index ea47c7d497..dd491648e8 100644 --- a/src/compiler/utils/list.ts +++ b/src/compiler/utils/list.js @@ -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]}`; } diff --git a/src/compiler/utils/names.ts b/src/compiler/utils/names.js similarity index 83% rename from src/compiler/utils/names.ts rename to src/compiler/utils/names.js index 303c6297b0..5f54c153d0 100644 --- a/src/compiler/utils/names.ts +++ b/src/compiler/utils/names.js @@ -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, '') diff --git a/src/compiler/utils/namespaces.ts b/src/compiler/utils/namespaces.js similarity index 87% rename from src/compiler/utils/namespaces.ts rename to src/compiler/utils/namespaces.js index d51b303fe1..1709693483 100644 --- a/src/compiler/utils/namespaces.ts +++ b/src/compiler/utils/namespaces.js @@ -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 }); diff --git a/src/compiler/utils/nodes_match.ts b/src/compiler/utils/nodes_match.js similarity index 100% rename from src/compiler/utils/nodes_match.ts rename to src/compiler/utils/nodes_match.js diff --git a/src/compiler/utils/patterns.ts b/src/compiler/utils/patterns.js similarity index 100% rename from src/compiler/utils/patterns.ts rename to src/compiler/utils/patterns.js diff --git a/src/compiler/utils/push_array.ts b/src/compiler/utils/push_array.js similarity index 75% rename from src/compiler/utils/push_array.ts rename to src/compiler/utils/push_array.js index 7cb77f84a4..ee6a9f7c26 100644 --- a/src/compiler/utils/push_array.ts +++ b/src/compiler/utils/push_array.js @@ -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(array: T[], items: T[]): void { +export function push_array(array, items) { for (let i = 0; i < items.length; i++) { array.push(items[i]); } diff --git a/src/compiler/utils/trim.ts b/src/compiler/utils/trim.js similarity index 53% rename from src/compiler/utils/trim.ts rename to src/compiler/utils/trim.js index 919a423d7f..ee962e5ea4 100644 --- a/src/compiler/utils/trim.ts +++ b/src/compiler/utils/trim.js @@ -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, ''); }