Skip to content

Commit 2d45dd5

Browse files
committed
isLocalImport doesn’t need root
1 parent 03016be commit 2d45dd5

File tree

4 files changed

+20
-22
lines changed

4 files changed

+20
-22
lines changed

src/javascript/features.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function findFeatures(node, root, sourcePath, references, input) {
1616
} = node;
1717

1818
// Promote fetches with static literals to file attachment references.
19-
if (isLocalFetch(node, references, root, sourcePath)) {
19+
if (isLocalFetch(node, references, sourcePath)) {
2020
features.push({type: "FileAttachment", name: getStringLiteralValue(arg)});
2121
return;
2222
}
@@ -50,7 +50,7 @@ export function findFeatures(node, root, sourcePath, references, input) {
5050
return features;
5151
}
5252

53-
export function isLocalFetch(node, references, root, sourcePath) {
53+
export function isLocalFetch(node, references, sourcePath) {
5454
if (node.type !== "CallExpression") return false;
5555
const {
5656
callee,
@@ -61,7 +61,7 @@ export function isLocalFetch(node, references, root, sourcePath) {
6161
callee.name === "fetch" &&
6262
!references.includes(callee) &&
6363
isStringLiteral(arg) &&
64-
isLocalImport(getStringLiteralValue(arg), root, sourcePath)
64+
isLocalImport(getStringLiteralValue(arg), sourcePath)
6565
);
6666
}
6767

src/javascript/fetches.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {isLocalFetch} from "./features.js";
44
export function rewriteFetches(output, rootNode, root, sourcePath) {
55
simple(rootNode.body, {
66
CallExpression(node) {
7-
if (isLocalFetch(node, rootNode.references, root, sourcePath)) {
7+
if (isLocalFetch(node, rootNode.references, sourcePath)) {
88
output.insertLeft(node.arguments[0].start + 3, "_file/");
99
}
1010
}

src/javascript/imports.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function findImports(body: Node, root: string, sourcePath: string) {
3232
function findImport(node) {
3333
if (isStringLiteral(node.source)) {
3434
const value = getStringLiteralValue(node.source);
35-
if (isLocalImport(value, root, sourcePath)) {
35+
if (isLocalImport(value, sourcePath)) {
3636
findLocalImports(normalize(value));
3737
} else {
3838
imports.push({name: value, type: "global"});
@@ -62,7 +62,7 @@ export function findImports(body: Node, root: string, sourcePath: string) {
6262
function findLocalImport(node) {
6363
if (isStringLiteral(node.source)) {
6464
const value = getStringLiteralValue(node.source);
65-
if (isLocalImport(value, root, sourcePath)) {
65+
if (isLocalImport(value, sourcePath)) {
6666
findLocalImports(join(dirname(path), value));
6767
} else {
6868
imports.push({name: value, type: "global"});
@@ -78,21 +78,21 @@ export function findImports(body: Node, root: string, sourcePath: string) {
7878
// TODO parallelize multiple static imports
7979
export function rewriteImports(output: any, rootNode: JavaScriptNode, root: string, sourcePath: string) {
8080
simple(rootNode.body, {
81-
ImportExpression(node: any) {
81+
ImportExpression(node) {
8282
if (isStringLiteral(node.source)) {
8383
const value = getStringLiteralValue(node.source);
8484
output.replaceLeft(
8585
node.source.start,
8686
node.source.end,
8787
JSON.stringify(
88-
isLocalImport(value, root, sourcePath)
88+
isLocalImport(value, sourcePath)
8989
? join("/_import/", join(dirname(sourcePath), value))
9090
: resolveImport(value)
9191
)
9292
);
9393
}
9494
},
95-
ImportDeclaration(node: any) {
95+
ImportDeclaration(node) {
9696
if (isStringLiteral(node.source)) {
9797
const value = getStringLiteralValue(node.source);
9898
rootNode.async = true;
@@ -102,11 +102,9 @@ export function rewriteImports(output: any, rootNode: JavaScriptNode, root: stri
102102
`const ${
103103
node.specifiers.some(isNotNamespaceSpecifier)
104104
? `{${node.specifiers.filter(isNotNamespaceSpecifier).map(rewriteImportSpecifier).join(", ")}}`
105-
: node.specifiers.some(isNamespaceSpecifier)
106-
? node.specifiers.find(isNamespaceSpecifier).local.name
107-
: "{}"
105+
: node.specifiers.find(isNamespaceSpecifier)?.local.name ?? "{}"
108106
} = await import(${JSON.stringify(
109-
isLocalImport(value, root, sourcePath)
107+
isLocalImport(value, sourcePath)
110108
? join("/_import/", join(dirname(sourcePath), value))
111109
: resolveImport(value)
112110
)});`
@@ -124,10 +122,10 @@ function rewriteImportSpecifier(node) {
124122
: `${node.imported.name}: ${node.local.name}`;
125123
}
126124

127-
export function isLocalImport(value: string, root: string, sourcePath: string): boolean {
125+
export function isLocalImport(value: string, sourcePath: string): boolean {
128126
return (
129127
["./", "../", "/"].some((prefix) => value.startsWith(prefix)) &&
130-
join(root + "/", dirname(sourcePath), value).startsWith(root)
128+
!join(".", dirname(sourcePath), value).startsWith("../")
131129
);
132130
}
133131

test/isLocalImport-test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ describe("isLocalImport", () => {
1212
const sourcePath = "/hello.md";
1313
const importValue = "./helpers.js";
1414
assert.equal(pathFromRoot(importValue, root, sourcePath), "docs/helpers.js");
15-
assert(isLocalImport(importValue, root, sourcePath));
15+
assert(isLocalImport(importValue, sourcePath));
1616
});
1717

1818
it("relative paths are correctly handled", async () => {
1919
const root = "docs";
2020
const sourcePath = "/subDocs/hello.md";
2121
const importValue = "./helpers.js";
2222
assert.equal(pathFromRoot(importValue, root, sourcePath), "docs/subDocs/helpers.js");
23-
assert(isLocalImport(importValue, root, sourcePath));
23+
assert(isLocalImport(importValue, sourcePath));
2424
});
2525

2626
it("root and sourcePath arguments can correctly handle slashes", async () => {
2727
const root = "docs/";
2828
const sourcePath = "/hello.md/";
2929
const importValue = "./helpers.js";
3030
assert.equal(pathFromRoot(importValue, root, sourcePath), "docs/helpers.js");
31-
assert(isLocalImport(importValue, root, sourcePath));
31+
assert(isLocalImport(importValue, sourcePath));
3232
});
3333

3434
it("identifies a local import from a nested sourcePath", async () => {
3535
const root = "docs";
3636
const sourcePath = "/subDocs/subDocs2/hello.md";
3737
const importValue = "../../random.js";
3838
assert.equal(pathFromRoot(importValue, root, sourcePath), "docs/random.js");
39-
assert(isLocalImport(importValue, root, sourcePath));
39+
assert(isLocalImport(importValue, sourcePath));
4040
});
4141

4242
it("cannot go to an ancestor directory beyond the root", async () => {
@@ -45,14 +45,14 @@ describe("isLocalImport", () => {
4545

4646
const importValue1 = "../../../random.js";
4747
assert.equal(pathFromRoot(importValue1, root, sourcePath), "../../random.js");
48-
assert.equal(isLocalImport(importValue1, root, sourcePath), false);
48+
assert.equal(isLocalImport(importValue1, sourcePath), false);
4949

5050
const importValue2 = "./../../random.js";
5151
assert.equal(pathFromRoot(importValue2, root, sourcePath), "../random.js");
52-
assert.equal(isLocalImport(importValue2, root, sourcePath), false);
52+
assert.equal(isLocalImport(importValue2, sourcePath), false);
5353

5454
const importValue3 = "/../../random.js";
5555
assert.equal(pathFromRoot(importValue3, root, sourcePath), "../random.js");
56-
assert.equal(isLocalImport(importValue3, root, sourcePath), false);
56+
assert.equal(isLocalImport(importValue3, sourcePath), false);
5757
});
5858
});

0 commit comments

Comments
 (0)