aboutsummaryrefslogtreecommitdiff
path: root/test/js/deno/scripts/postinstall.ts
blob: 65fd5a04e9a92a48692d7c51e27fdabfd85c5c1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { mkdirSync } from "node:fs";
import { join, dirname } from "node:path";
import { parse, print } from "@swc/core";
import type { ImportDeclaration, ExpressionStatement, CallExpression } from "@swc/core";
import imports from "../resources/imports.json";
import tests from "../resources/tests.json";

// FIXME: https://github.com/oven-sh/bun/issues/2350
// import * as harness from "deno:harness";

for (const test of tests) {
  const path = join(import.meta.dir, "..", test.path);
  const url = new URL(test.remotePath, "https://raw.githubusercontent.com/denoland/deno/main/cli/tests/");
  const response = await fetch(url);
  console.log(response.status, url.toString(), "->", test.path);
  if (!response.ok) {
    continue;
  }
  const src = await response.text();
  const dst = await visit(src, test);
  try {
    mkdirSync(dirname(path));
  } catch {}
  await Bun.write(path.replace(".test.", ".deno."), src);
  await Bun.write(path, dst);
}

async function visit(src: string, test: any): Promise<string> {
  const ast = await parse(src, {
    syntax: "typescript",
    target: "esnext",
    dynamicImport: true,
  });
  for (const item of ast.body) {
    if (item.type === "ImportDeclaration") {
      visitImport(item);
    }
    if (item.type === "ExpressionStatement") {
      visitExpression(item, test);
    }
  }
  const header = `// Copyright 2018+ the Deno authors. All rights reserved. MIT license.
// https://raw.githubusercontent.com/denoland/deno/main/cli/tests/${test.remotePath}
\n`;
  const { code } = await print(ast, {
    isModule: true,
  });
  return header + code;
}

function visitImport(item: ImportDeclaration): void {
  const src = item.source.value;
  let match = false;
  for (const name of imports) {
    if (src.endsWith(name)) {
      match = true;
      break;
    }
  }
  if (!match) {
    console.warn("Unknown import:", src);
    return;
  }
  item.source.raw = '"deno:harness"';
  // FIXME: https://github.com/oven-sh/bun/issues/2350
  /*let missing = [];
  for (const specifier of item.specifiers) {
    const name = specifier.local.value;
    if (!(name in harness)) {
      missing.push(name);
    }
  }
  if (missing.length) {
    console.warn("Harness does not contain exports:", missing);
  }*/
}

function visitExpression(item: ExpressionStatement, test: any): void {
  if (
    item.expression.type === "CallExpression" &&
    item.expression.callee.type === "MemberExpression" &&
    item.expression.callee.object.type === "Identifier" &&
    item.expression.callee.object.value === "Deno"
  ) {
    if (item.expression.callee.property.type === "Identifier" && item.expression.callee.property.value === "test") {
      visitTest(item.expression);
    }
  }
}

function visitTest(item: CallExpression): void {
  for (const argument of item.arguments) {
    if (argument.expression.type === "FunctionExpression") {
      const fn = argument.expression.identifier?.value;
      for (const test of tests) {
        if (test.skip && test.skip.includes(fn)) {
          item.callee.property.value = "test.ignore";
        }
      }
    }
  }
}