Skip to content

Commit 83643fe

Browse files
committed
fix(path): replace win32 usages with custom functions, prettier
1 parent 5524b9d commit 83643fe

File tree

6 files changed

+33
-7
lines changed

6 files changed

+33
-7
lines changed

CLAUDE.md

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
55
## Development Commands
66

77
### Building
8+
89
```bash
910
yarn build # Compile TypeScript to dist/
1011
yarn typecheck # Run TypeScript type checking without emitting files
1112
```
1213

1314
### Testing
15+
1416
```bash
1517
yarn test # Run all tests with coverage (Vitest)
1618
yarn test:node # Run tests in Node.js environment
@@ -20,26 +22,32 @@ yarn test:update # Update test snapshots
2022
```
2123

2224
### Code Quality
25+
2326
```bash
2427
yarn lint # Run ESLint on lib/ directory
2528
yarn prettier # Format all code files
2629
```
2730

2831
### Running Individual Tests
32+
2933
To run a single test file:
34+
3035
```bash
3136
npx vitest test/specs/circular/circular.spec.ts
3237
```
3338

3439
To run tests matching a pattern:
40+
3541
```bash
3642
npx vitest --grep "circular"
3743
```
3844

3945
## Architecture Overview
4046

4147
### Core Purpose
48+
4249
This library parses, resolves, and dereferences JSON Schema `$ref` pointers. It handles references to:
50+
4351
- External files (local filesystem)
4452
- HTTP/HTTPS URLs
4553
- Internal JSON pointers within schemas
@@ -49,7 +57,9 @@ This library parses, resolves, and dereferences JSON Schema `$ref` pointers. It
4957
### Key Architecture Components
5058

5159
#### 1. $RefParser (lib/index.ts)
60+
5261
The main entry point and orchestrator class. Provides four primary operations:
62+
5363
- **parse()**: Reads a single schema file (JSON/YAML) without resolving references
5464
- **resolve()**: Parses and resolves all `$ref` pointers, returns a `$Refs` object mapping references to values
5565
- **bundle()**: Converts external `$ref` pointers to internal ones (single file output)
@@ -58,35 +68,44 @@ The main entry point and orchestrator class. Provides four primary operations:
5868
All methods support both callback and Promise-based APIs, with multiple overload signatures.
5969

6070
#### 2. $Refs (lib/refs.ts)
71+
6172
A map/registry of all resolved JSON references and their values. Tracks:
73+
6274
- All file paths/URLs encountered
6375
- Circular reference detection
6476
- Helper methods to query references by type (file, http, etc.)
6577

6678
#### 3. Pointer (lib/pointer.ts)
79+
6780
Represents a single JSON pointer (`#/definitions/person`) and implements JSON Pointer RFC 6901 spec:
81+
6882
- Parses JSON pointer syntax (`/`, `~0`, `~1` escaping)
6983
- Resolves pointers to actual values within objects
7084
- Handles edge cases (null values, missing properties)
7185

7286
#### 4. $Ref (lib/ref.ts)
87+
7388
Wraps a single reference with metadata:
89+
7490
- The reference path/URL
7591
- The resolved value
7692
- Path type (file, http, etc.)
7793
- Error information (when continueOnError is enabled)
7894

7995
#### 5. Plugin System
96+
8097
Two types of plugins, both configurable via options:
8198

8299
**Parsers** (lib/parsers/):
100+
83101
- JSON parser (json.ts)
84102
- YAML parser (yaml.ts) - uses js-yaml
85103
- Text parser (text.ts)
86104
- Binary parser (binary.ts)
87105
- Execute in order based on `order` property and `canParse()` matching
88106

89107
**Resolvers** (lib/resolvers/):
108+
90109
- File resolver (file.ts) - reads from filesystem (Node.js only)
91110
- HTTP resolver (http.ts) - fetches from URLs using native fetch
92111
- Custom resolvers can be added via options
@@ -100,7 +119,9 @@ Two types of plugins, both configurable via options:
100119
**lib/dereference.ts**: Replaces all `$ref` pointers with actual values, handles circular references
101120

102121
#### 7. Options System (lib/options.ts)
122+
103123
Hierarchical configuration with defaults for:
124+
104125
- Which parsers/resolvers to enable
105126
- Circular reference handling (boolean or "ignore")
106127
- External reference resolution (relative vs root)
@@ -132,12 +153,14 @@ Hierarchical configuration with defaults for:
132153
## Testing Strategy
133154

134155
Tests are organized in test/specs/ by scenario:
135-
- Each scenario has test files (*.spec.ts) and fixture data
156+
157+
- Each scenario has test files (\*.spec.ts) and fixture data
136158
- Tests validate parse, resolve, bundle, and dereference operations
137159
- Extensive coverage of edge cases: circular refs, deep nesting, special characters in paths
138160
- Browser-specific tests use test/fixtures/server.ts for HTTP mocking
139161

140162
Test utilities:
163+
141164
- test/utils/helper.js: Common test patterns
142165
- test/utils/path.js: Path handling for cross-platform tests
143166
- test/utils/serializeJson.ts: Custom snapshot serializer

lib/util/convert-path-to-posix.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import path from "path";
22

3+
const win32Sep = "\\";
34
export default function convertPathToPosix(filePath: string) {
45
const isExtendedLengthPath = filePath.startsWith("\\\\?\\");
56

67
if (isExtendedLengthPath) {
78
return filePath;
89
}
910

10-
return filePath.split(path?.win32?.sep).join(path?.posix?.sep ?? "/");
11+
return filePath.split(win32Sep).join(path?.posix?.sep ?? "/");
1112
}

lib/util/url.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import convertPathToPosix from "./convert-path-to-posix.js";
2-
import path, { win32 } from "path";
2+
import path from "path";
33

44
const forwardSlashPattern = /\//g;
55
const protocolPattern = /^(\w{2,}):\/\//i;
@@ -9,6 +9,8 @@ const jsonPointerTilde = /~0/g;
99
import { join } from "path";
1010
import { isWindows } from "./is-windows.js";
1111

12+
const isAbsoluteWin32Path = /^[a-zA-Z]:\\/;
13+
1214
// RegExp patterns to URL-encode special characters in local filesystem paths
1315
const urlEncodePatterns = [
1416
[/\?/g, "%3F"],
@@ -391,7 +393,7 @@ export function fromFileSystemPath(path: string) {
391393
const hasProjectDir = upperPath.includes(posixUpper);
392394
const hasProjectUri = upperPath.includes(posixUpper);
393395
const isAbsolutePath =
394-
win32?.isAbsolute(path) ||
396+
isAbsoluteWin32Path.test(path) ||
395397
path.startsWith("http://") ||
396398
path.startsWith("https://") ||
397399
path.startsWith("file://");

test/specs/bundle-defs-encoding/bundle-defs-encoding.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe("Bundle $defs encoding", () => {
1010
const bundled = await parser.bundle(schema);
1111

1212
// Debug: log the bundled schema
13-
console.log('Bundled schema:', JSON.stringify(bundled, null, 2));
13+
console.log("Bundled schema:", JSON.stringify(bundled, null, 2));
1414

1515
// The bundled schema should have $defs section with myschema that references otherschema
1616
expect(bundled).toHaveProperty("$defs");

test/specs/defs/dereferencedA.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@
116116
]
117117
}
118118
}
119-
}
119+
}

test/specs/defs/dereferencedB.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,4 @@
116116
]
117117
}
118118
}
119-
}
119+
}

0 commit comments

Comments
 (0)