Skip to content

Commit 800a6ad

Browse files
committed
Add explanatory comments for tsgo destructuring workaround
Document why we avoid destructuring from global objects due to a tsgo transpilation bug that causes runtime errors. References issue #3.
1 parent e1fdd0d commit 800a6ad

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

src/decode.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
*/
55
import { PurlError } from './error.js'
66

7-
const { decodeURIComponent: decodeComponent } = globalThis
7+
// IMPORTANT: Do not use destructuring here - use direct assignment instead.
8+
// tsgo has a bug that incorrectly transpiles destructured exports, resulting in
9+
// `exports.decodeComponent = void 0;` which causes runtime errors.
10+
// See: https://github.com/SocketDev/socket-packageurl-js/issues/3
11+
const decodeComponent = globalThis.decodeURIComponent
812

913
function decodePurlComponent(comp: string, encodedComponent: string): string {
1014
try {

src/encode.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
import { isObject } from './objects.js'
1111
import { isNonEmptyString } from './strings.js'
1212

13+
// IMPORTANT: Do not use destructuring here (e.g., const { encodeURIComponent } = globalThis).
14+
// tsgo has a bug that incorrectly transpiles destructured exports, resulting in
15+
// `exports.encodeComponent = void 0;` which causes runtime errors.
16+
// See: https://github.com/SocketDev/socket-packageurl-js/issues/3
1317
const encodeComponent = globalThis.encodeURIComponent
1418

1519
function encodeName(name: unknown): string {

src/normalize.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ function normalizeVersion(rawVersion: unknown): string | undefined {
9393
return typeof rawVersion === 'string' ? rawVersion.trim() : undefined
9494
}
9595

96-
const { apply: ReflectApply } = Reflect
96+
// IMPORTANT: Do not use destructuring here - use direct assignment instead.
97+
// tsgo has a bug that incorrectly transpiles destructured exports, resulting in
98+
// `exports.ReflectApply = void 0;` which causes runtime errors.
99+
// See: https://github.com/SocketDev/socket-packageurl-js/issues/3
100+
const ReflectApply = Reflect.apply
97101

98102
function qualifiersToEntries(
99103
rawQualifiers: unknown,

src/validate.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ import { isNonEmptyString } from './strings.js'
88

99
import type { QualifiersObject } from './purl-component.js'
1010

11-
const { apply: ReflectApply } = Reflect
11+
// IMPORTANT: Do not use destructuring here - use direct assignment instead.
12+
// tsgo has a bug that incorrectly transpiles destructured exports, resulting in
13+
// `exports.ReflectApply = void 0;` which causes runtime errors.
14+
// See: https://github.com/SocketDev/socket-packageurl-js/issues/3
15+
const ReflectApply = Reflect.apply
1216

1317
function validateEmptyByType(
1418
type: string,

0 commit comments

Comments
 (0)