-
-
Notifications
You must be signed in to change notification settings - Fork 139
/
dts_walker_test.go
77 lines (64 loc) · 2.19 KB
/
dts_walker_test.go
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
package server
import (
"bytes"
"fmt"
"testing"
)
func TestDtsWalker(t *testing.T) {
const rawDts = `
/*
* This is a multi-line comment.
* @example: import("react")
*/
// This is a single-line comment. import("react")
// import React from "react";
/// <reference path="global.d.ts" />
import * as CSS from 'csstype';
import * as PropTypes from 'prop-types';
import type { Interaction as SchedulerInteraction } /* bad comment */ from "scheduler/tracing";
import DefaultExport, { AndNamed } from "scheduler/tracing";
export * from "react";
export = React;
// todo: support: export const weird: "import('react')";
import React = import('react');
import React = require("react");
`
const expectedDts = `
/*
* This is a multi-line comment.
* @example: import("react")
*/
// This is a single-line comment. import("react")
// import React from "react";
/// <reference path="./global.d.ts" />
import * as CSS from 'https://esm.sh/csstype@1.0.0/index.d.ts';
import * as PropTypes from 'https://esm.sh/prop-types@1.0.0/index.d.ts';
import type { Interaction as SchedulerInteraction } /* bad comment */ from "https://esm.sh/scheduler@1.0.0/tracing.d.ts";
import DefaultExport, { AndNamed } from "https://esm.sh/scheduler@1.0.0/tracing.d.ts";
export * from "https://esm.sh/@types/react@1.0.0/index.d.ts";
export = React;
// todo: support: export const weird: "import('react')";
import React = import('https://esm.sh/@types/react@1.0.0/index.d.ts');
import React = require("https://esm.sh/@types/react@1.0.0/index.d.ts");
`
buf := bytes.NewBuffer(nil)
err := walkDts(bytes.NewReader([]byte(rawDts)), buf, func(name string, kind string, position int) string {
if kind == "importExpr" || kind == "importCall" {
if name == "react" || name == "react-dom" {
return fmt.Sprintf("https://esm.sh/@types/%s@1.0.0/index.d.ts", name)
}
pkgName, _, subPath := splitPkgPath(name)
if subPath != "" {
return fmt.Sprintf("https://esm.sh/%s@1.0.0/%s.d.ts", pkgName, subPath)
}
return fmt.Sprintf("https://esm.sh/%s@1.0.0/index.d.ts", pkgName)
}
return name
})
if err != nil {
t.Fatal(err)
}
if buf.String() != expectedDts {
t.Fatal("transformed dts not match, want:", expectedDts, "got:", buf.String())
}
}