Skip to content

Commit

Permalink
fix(tools-node): allow empty scope in package names (#3311)
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 authored Aug 28, 2024
1 parent 397dd6a commit a6a3149
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changeset/beige-otters-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rnx-kit/metro-plugin-typescript": patch
"@rnx-kit/tools-node": patch
---

Allow empty scope in package names
1 change: 1 addition & 0 deletions packages/test-app/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ function isPnpmMode() {
const config = {
preset: "@rnx-kit/jest-preset/private",
moduleNameMapper: {
"^@/(.*)$": "<rootDir>/src/internal/$1",
"^internal(.*)$": "<rootDir>/src/internal$1",
},
};
Expand Down
1 change: 1 addition & 0 deletions packages/test-app/metro.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const blockList = exclusionList([
module.exports = makeMetroConfig({
resolver: {
extraNodeModules: {
"@/hermes": path.resolve(__dirname, "src", "internal", "hermes.ts"),
internal: path.resolve(__dirname, "src", "internal"),
...(useAuthMock
? {
Expand Down
2 changes: 1 addition & 1 deletion packages/test-app/src/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// Both `internal` imports are used to verify that `metro-resolver-symlinks`
// resolves them correctly when `experimental_retryResolvingFromDisk` is
// enabled.
import { getHermesVersion } from "@/hermes";
import {
getReactNativeVersion,
isBridgeless,
isFabricInstance,
isRemoteDebuggingAvailable,
} from "internal";
import { getHermesVersion } from "internal/hermes";
import type { PropsWithChildren } from "react";
import React, { useCallback, useState } from "react";
import type { NativeSyntheticEvent } from "react-native";
Expand Down
4 changes: 2 additions & 2 deletions packages/test-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"checkJs": false,
"noEmit": true,
"paths": {
"internal": ["./src/internal"],
"internal/*": ["./src/internal/*"]
"@/*": ["./src/internal/*"],
"internal": ["./src/internal"]
}
},
"include": ["src"]
Expand Down
10 changes: 5 additions & 5 deletions packages/tools-node/src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ export function parsePackageRef(r: string): PackageRef {
// If `/` is not found, the reference could be a path alias.
const indexSeparator = r.indexOf("/");
if (indexSeparator >= 0) {
// The separator must start from position >= 2 to ensure that it is '@'
// and at least one other character. Further, the separator must have
// at least 1 character following it, before the end of the string.
if (indexSeparator < 2 || indexSeparator + 2 >= r.length) {
throw new Error(`Invalid package reference: "${r}"`);
// The separator must have at least 1 character following it, before the
// end of the string. Note that the scope may be an empty string.
// TypeScript does not place any restrictions on import re-mappings.
if (indexSeparator + 1 >= r.length) {
throw new Error(`Invalid package reference: ${r}`);
}

return {
Expand Down
8 changes: 4 additions & 4 deletions packages/tools-node/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ describe("Node > Package", () => {
deepEqual(parsePackageRef("@alias"), { name: "@alias" });
});

it("parsePackageRef(@/core) is allowed", () => {
deepEqual(parsePackageRef("@/core"), { scope: "@", name: "core" });
});

it("parsePackageRef(undefined) throws an Error", () => {
// @ts-expect-error Argument of type 'undefined' is not assignable to parameter of type 'string'
throws(() => parsePackageRef(undefined));
Expand All @@ -59,10 +63,6 @@ describe("Node > Package", () => {
throws(() => parsePackageRef("@babel/"));
});

it("parsePackageRef(@/core) throws an Error", () => {
throws(() => parsePackageRef("@/core"));
});

it("readPackage() loads package.json when given its containing directory", () => {
const manifest = readPackage(fixtureDir);
equal(manifest.name, "test-package");
Expand Down

0 comments on commit a6a3149

Please sign in to comment.