Skip to content

Commit 2706d1d

Browse files
committed
Deduplicate lsif npm identifiers.
1 parent b0c81c4 commit 2706d1d

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

npm/bin/lsif-npm

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#!/usr/bin/env node
1+
#!/usr/bin/env node
22
require('../lib/main.js').main();

npm/src/main.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,23 @@ function makeAbsolute(p: string, root?: string): string {
8686
}
8787
}
8888

89+
const makeIntIdGenerator = () => {
90+
let counter = Number.MAX_SAFE_INTEGER;
91+
92+
return () => {
93+
return counter--;
94+
}
95+
}
96+
97+
const uuidIdGenerator = () => {
98+
return uuid.v4()
99+
}
100+
89101
class Linker {
90102

91103
private _idGenerator: (() => Id) | undefined;
92104

93-
constructor() {
105+
constructor(private intIdGenerator: () => Id) {
94106
}
95107

96108
protected get idGenerator(): () => Id {
@@ -105,14 +117,9 @@ class Linker {
105117
return;
106118
}
107119
if (typeof id === 'number') {
108-
let counter = Number.MAX_SAFE_INTEGER;
109-
this._idGenerator = () => {
110-
return counter--;
111-
};
120+
this._idGenerator = this.intIdGenerator
112121
} else {
113-
this._idGenerator = () => {
114-
return uuid.v4();
115-
};
122+
this._idGenerator = uuidIdGenerator
116123
}
117124
}
118125

@@ -167,8 +174,8 @@ class ExportLinker extends Linker {
167174

168175
private packageInformation: PackageInformation | undefined;
169176

170-
constructor(private projectRoot: string, private packageJson: PackageJson) {
171-
super();
177+
constructor(private projectRoot: string, private packageJson: PackageJson, intIdGenerator: () => Id) {
178+
super(intIdGenerator);
172179
}
173180

174181
public handleMoniker(moniker: Moniker): void {
@@ -212,8 +219,8 @@ class ImportLinker extends Linker {
212219

213220
private packageData: Map<string, { packageInfo: PackageInformation, packageJson: PackageJson } | null>;
214221

215-
constructor(private projectRoot: string) {
216-
super();
222+
constructor(private projectRoot: string, intIdGenerator: () => Id) {
223+
super(intIdGenerator);
217224
this.packageData = new Map();
218225
}
219226

@@ -354,11 +361,12 @@ export function main(): void {
354361
return;
355362
}
356363

364+
const intIdGenerator = makeIntIdGenerator()
357365
let exportLinker: ExportLinker | undefined;
358366
if (packageJson !== undefined) {
359-
exportLinker = new ExportLinker(projectRoot, packageJson);
367+
exportLinker = new ExportLinker(projectRoot, packageJson, intIdGenerator);
360368
}
361-
const importLinker: ImportLinker = new ImportLinker(projectRoot);
369+
const importLinker: ImportLinker = new ImportLinker(projectRoot, intIdGenerator);
362370
let input: NodeJS.ReadStream | fs.ReadStream = process.stdin;
363371
if (options.in !== undefined && fs.existsSync(options.in)) {
364372
input = fs.createReadStream(options.in, { encoding: 'utf8'});

0 commit comments

Comments
 (0)