|
| 1 | +// @ts-ignore -- ignore |
| 2 | +import TestStream from "test262-stream" |
| 3 | +import path from "path" |
| 4 | +import { promises as fs } from "fs" |
| 5 | +import { parseRegExpLiteral } from "../src/index" |
| 6 | +import jsTokens from "js-tokens" |
| 7 | +import { cloneWithoutCircular } from "./clone-without-circular" |
| 8 | +import type { RegExpSyntaxError } from "../src/regexp-syntax-error" |
| 9 | +import { fixturesData } from "../test/fixtures/parser/literal" |
| 10 | +import type { Readable } from "stream" |
| 11 | + |
| 12 | +const fixturesRoot = path.join( |
| 13 | + __dirname, |
| 14 | + "../test/fixtures/parser/literal/test262", |
| 15 | +) |
| 16 | + |
| 17 | +const test262Root = path.dirname(require.resolve("test262/package.json")) |
| 18 | + |
| 19 | +const stream: Readable = new TestStream(test262Root, { omitRuntime: true }) |
| 20 | + |
| 21 | +type Test = { |
| 22 | + file: string |
| 23 | + contents: string |
| 24 | + attrs: { |
| 25 | + features?: string[] |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +const testObjects: Test[] = [] |
| 30 | + |
| 31 | +stream.on("data", (test: Test) => { |
| 32 | + if (!test.file.toLocaleLowerCase().includes("regexp")) { |
| 33 | + return |
| 34 | + } |
| 35 | + testObjects.push(test) |
| 36 | +}) |
| 37 | +stream.on("end", () => { |
| 38 | + // eslint-disable-next-line no-void |
| 39 | + void extractMain() |
| 40 | +}) |
| 41 | + |
| 42 | +async function extractMain() { |
| 43 | + const usedPatterns = new Set<string>() |
| 44 | + for (const fixture of Object.values(fixturesData)) { |
| 45 | + for (const pattern of Object.keys(fixture.patterns)) { |
| 46 | + usedPatterns.add(pattern) |
| 47 | + } |
| 48 | + } |
| 49 | + const extractedFixtures = new Map< |
| 50 | + string, |
| 51 | + { |
| 52 | + _test262FileNames: string[] |
| 53 | + options: { |
| 54 | + strict?: boolean |
| 55 | + } |
| 56 | + patterns: Record<string, any> |
| 57 | + } |
| 58 | + >() |
| 59 | + for (const test of testObjects.sort((a, b) => { |
| 60 | + const lengthA = a.attrs.features?.length ?? 999 |
| 61 | + const lengthB = b.attrs.features?.length ?? 999 |
| 62 | + return lengthA - lengthB || compareStr(a.file, b.file) |
| 63 | + })) { |
| 64 | + let filePath: string | undefined = undefined |
| 65 | + if (test.attrs.features && test.attrs.features.length > 0) { |
| 66 | + filePath = path.join( |
| 67 | + fixturesRoot, |
| 68 | + `${[...test.attrs.features] |
| 69 | + .sort(compareStr) |
| 70 | + .join("-and-")}.json`, |
| 71 | + ) |
| 72 | + } else { |
| 73 | + filePath = path.join(fixturesRoot, "not-categorized.json") |
| 74 | + } |
| 75 | + let fixture = extractedFixtures.get(filePath) |
| 76 | + |
| 77 | + if (!fixture) { |
| 78 | + if (await fileExists(filePath)) { |
| 79 | + fixture = JSON.parse(await fs.readFile(filePath, "utf8")) |
| 80 | + } |
| 81 | + if (!fixture) { |
| 82 | + fixture = { |
| 83 | + _test262FileNames: [], |
| 84 | + options: {}, |
| 85 | + patterns: {}, |
| 86 | + } |
| 87 | + extractedFixtures.set(filePath, fixture) |
| 88 | + } |
| 89 | + } |
| 90 | + let has = false |
| 91 | + for (const pattern of extractRegExp(test.contents)) { |
| 92 | + if (usedPatterns.has(pattern)) { |
| 93 | + continue |
| 94 | + } |
| 95 | + has = true |
| 96 | + usedPatterns.add(pattern) |
| 97 | + try { |
| 98 | + const ast = parseRegExpLiteral(pattern, fixture.options) |
| 99 | + fixture.patterns[pattern] = { ast: cloneWithoutCircular(ast) } |
| 100 | + } catch (err) { |
| 101 | + const error = err as RegExpSyntaxError |
| 102 | + fixture.patterns[pattern] = { |
| 103 | + error: { message: error.message, index: error.index }, |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + if (has) { |
| 108 | + fixture._test262FileNames = [ |
| 109 | + ...fixture._test262FileNames, |
| 110 | + test.file, |
| 111 | + ] |
| 112 | + } |
| 113 | + } |
| 114 | + await fs.copyFile( |
| 115 | + path.join(test262Root, "LICENSE"), |
| 116 | + path.join(fixturesRoot, "LICENSE"), |
| 117 | + ) |
| 118 | + for (const [filePath, fixture] of extractedFixtures) { |
| 119 | + if (Object.keys(fixture.patterns).length === 0) { |
| 120 | + continue |
| 121 | + } |
| 122 | + fixture._test262FileNames = [ |
| 123 | + ...new Set(fixture._test262FileNames), |
| 124 | + ].sort(compareStr) |
| 125 | + // @ts-ignore -- ignore |
| 126 | + fixture.patterns = Object.fromEntries( |
| 127 | + Object.entries(fixture.patterns).sort((a, b) => |
| 128 | + compareStr(a[0], b[0]), |
| 129 | + ), |
| 130 | + ) |
| 131 | + await fs.mkdir(path.dirname(filePath), { recursive: true }) |
| 132 | + await fs.writeFile( |
| 133 | + filePath, |
| 134 | + JSON.stringify( |
| 135 | + fixture, |
| 136 | + (_, v: unknown) => (v === Infinity ? "$$Infinity" : v), |
| 137 | + 2, |
| 138 | + ), |
| 139 | + ) |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +function* extractRegExp(content: string) { |
| 144 | + for (const token of jsTokens(content)) { |
| 145 | + if (token.type === "RegularExpressionLiteral") { |
| 146 | + yield token.value |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +async function fileExists(filepath: string) { |
| 152 | + try { |
| 153 | + return (await fs.lstat(filepath)).isFile() |
| 154 | + } catch (e) { |
| 155 | + return false |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +function compareStr(a: string, b: string) { |
| 160 | + return a > b ? 1 : a < b ? -1 : 0 |
| 161 | +} |
0 commit comments