Skip to content

Commit 882a148

Browse files
committed
Add support for Windows
- path manipulation and comparison operations were assuming posix-style paths. Adds a bit of normalization so that paths on Windows are always converted to use forward-slashes. This is fine because Windows treats both as path delimeters and forward slashes are not valid in directory or file names.
1 parent facd788 commit 882a148

File tree

6 files changed

+29
-7
lines changed

6 files changed

+29
-7
lines changed

src/DeclarationIndex.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Namespace } from './resources/Namespace';
1212
import { Resource } from './resources/Resource';
1313
import { isExportableDeclaration } from './type-guards/TypescriptHeroGuards';
1414
import { TypescriptParser } from './TypescriptParser';
15-
import { normalizeFilename, normalizePathUri } from './utilities/PathHelpers';
15+
import { normalizeFilename, normalizePathUri, toPosix } from './utilities/PathHelpers';
1616

1717
/**
1818
* Returns the name of the node folder. Is used as the library name for indexing.
@@ -308,7 +308,7 @@ export class DeclarationIndex {
308308
break;
309309
}
310310
if (ex instanceof AllExport || ex instanceof NamedExport) {
311-
const exported = '/' + relative(this.rootPath, normalize(join(resource.parsedPath.dir, ex.from)));
311+
const exported = '/' + toPosix(relative(this.rootPath, normalize(join(resource.parsedPath.dir, ex.from))));
312312
exportsResource = exported === resourcePath;
313313
}
314314
}

src/utilities/PathHelpers.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,16 @@ export function normalizePathUri(uri: string): string {
2525
* @returns {string}
2626
*/
2727
export function normalizeFilename(filepath: string): string {
28-
return filepath.replace(/([.]d)?[.](t|j)sx?$/g, '');
28+
return toPosix(filepath.replace(/([.]d)?[.](t|j)sx?$/g, ''));
29+
}
30+
31+
/**
32+
* On Windows, replaces all backslash delimeters with forward slashes.
33+
* On other OSes, returns the path unmodified.
34+
*/
35+
export function toPosix(path: string): string {
36+
if (platform() === 'win32') {
37+
return path.replace(/\\/g, '/');
38+
}
39+
return path;
2940
}

test/declaration-index/DeclarationIndex.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import mockFs = require('mock-fs');
2-
import { join, resolve } from 'path';
2+
import { join, resolve } from '../testUtilities';
33

44
import { DeclarationIndex } from '../../src/DeclarationIndex';
55
import { ClassDeclaration } from '../../src/declarations';

test/declaration-index/specific-cases/body-parser/DeclarationIndex.body-parser.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { join, resolve } from 'path';
1+
import { join, resolve } from '../../../testUtilities';
22

33
import { DeclarationIndex } from '../../../../src/DeclarationIndex';
44
import { TypescriptParser } from '../../../../src/TypescriptParser';

test/declaration-index/specific-cases/reindex-with-global-module/DeclarationIndex.reindex-with-global-module-export.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FileChanges } from '../../../../src';
2-
import { join, resolve } from 'path';
2+
import { join, resolve } from '../../../testUtilities';
33

44
import { DeclarationIndex } from '../../../../src/DeclarationIndex';
55
import { TypescriptParser } from '../../../../src/TypescriptParser';

test/testUtilities.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
import { resolve } from 'path';
1+
import * as Path from 'path';
2+
import { toPosix } from '../src/utilities/PathHelpers';
23

34
export function getWorkspaceFile(pathFromWorkspace: string): string {
45
return resolve(__dirname, '_workspace', pathFromWorkspace);
56
}
67

8+
// Like path.resolve, but always replaces backslashes with forward slashes on Windows.
9+
export function resolve(...paths: string[]): string {
10+
return toPosix(Path.resolve(...paths));
11+
}
12+
13+
// Like path.join, but always replaces backslashes with forward slashes on Windows.
14+
export function join(...paths: string[]): string {
15+
return toPosix(Path.join(...paths));
16+
}
17+
718
export const rootPath = resolve(__dirname, '_workspace');

0 commit comments

Comments
 (0)