Comparing version 0.3.0 to 0.4.0
291
lib/main.js
@@ -13,3 +13,2 @@ "use strict"; | ||
const uuid = require("uuid"); | ||
const vscode_uri_1 = require("vscode-uri"); | ||
const package_1 = require("./package"); | ||
@@ -62,6 +61,4 @@ const lsif_protocol_1 = require("lsif-protocol"); | ||
} | ||
class ExportLinker { | ||
constructor(projectRoot, packageInfo) { | ||
this.projectRoot = projectRoot; | ||
this.packageInfo = packageInfo; | ||
class Linker { | ||
constructor() { | ||
} | ||
@@ -74,22 +71,88 @@ get idGenerator() { | ||
} | ||
ensureIdGenerator(id) { | ||
if (this._idGenerator !== undefined) { | ||
return; | ||
} | ||
if (typeof id === 'number') { | ||
let counter = Number.MAX_SAFE_INTEGER; | ||
this._idGenerator = () => { | ||
return counter--; | ||
}; | ||
} | ||
else { | ||
this._idGenerator = () => { | ||
return uuid.v4(); | ||
}; | ||
} | ||
} | ||
createPackageInformation(packageJson) { | ||
let result = { | ||
id: this.idGenerator(), | ||
type: lsif_protocol_1.ElementTypes.vertex, | ||
label: lsif_protocol_1.VertexLabels.packageInformation, | ||
name: packageJson.name, | ||
manager: 'npm', | ||
version: packageJson.version | ||
}; | ||
if (packageJson.hasRepository()) { | ||
result.repository = packageJson.repository; | ||
} | ||
return result; | ||
} | ||
createMoniker(identifier, kind, scheme) { | ||
return { | ||
id: this.idGenerator(), | ||
type: lsif_protocol_1.ElementTypes.vertex, | ||
label: lsif_protocol_1.VertexLabels.moniker, | ||
kind: kind, | ||
scheme: scheme, | ||
identifier: identifier | ||
}; | ||
} | ||
createNextEdge(outV, inV) { | ||
return { | ||
id: this.idGenerator(), | ||
type: lsif_protocol_1.ElementTypes.edge, | ||
label: lsif_protocol_1.EdgeLabels.next, | ||
outV: outV, | ||
inV: inV | ||
}; | ||
} | ||
createPackageInformationEdge(outV, inV) { | ||
return { | ||
id: this.idGenerator(), | ||
type: lsif_protocol_1.ElementTypes.edge, | ||
label: lsif_protocol_1.EdgeLabels.packageInformation, | ||
outV: outV, | ||
inV: inV | ||
}; | ||
} | ||
} | ||
class ExportLinker extends Linker { | ||
constructor(projectRoot, packageJson) { | ||
super(); | ||
this.projectRoot = projectRoot; | ||
this.packageJson = packageJson; | ||
} | ||
handleMoniker(moniker) { | ||
if (moniker.scheme !== moniker_1.TscMoniker.scheme) { | ||
emit(moniker); | ||
if (moniker.kind !== lsif_protocol_1.MonikerKind.export || moniker.scheme !== moniker_1.TscMoniker.scheme) { | ||
return; | ||
} | ||
this.ensureIdGenerator(moniker.id); | ||
emit(moniker); | ||
let tscMoniker = moniker_1.TscMoniker.parse(moniker.identifier); | ||
if (moniker_1.TscMoniker.hasPath(tscMoniker) && this.isPackaged(path.join(this.projectRoot, tscMoniker.path))) { | ||
if (this.packageInfo.main === tscMoniker.path || this.packageInfo.typings === tscMoniker.path) { | ||
moniker.identifier = moniker_1.NpmMoniker.create(this.packageInfo.name, undefined, tscMoniker.name); | ||
this.ensurePackageInformation(); | ||
let npmIdentifier; | ||
if (this.packageJson.main === tscMoniker.path || this.packageJson.typings === tscMoniker.path) { | ||
npmIdentifier = moniker_1.NpmMoniker.create(this.packageJson.name, undefined, tscMoniker.name); | ||
} | ||
else { | ||
moniker.identifier = moniker_1.NpmMoniker.create(this.packageInfo.name, tscMoniker.path, tscMoniker.name); | ||
npmIdentifier = moniker_1.NpmMoniker.create(this.packageJson.name, tscMoniker.path, tscMoniker.name); | ||
} | ||
moniker.scheme = moniker_1.NpmMoniker.schema; | ||
this.emitPackageInformation(moniker); | ||
let npmMoniker = this.createMoniker(npmIdentifier, moniker.kind, moniker_1.NpmMoniker.scheme); | ||
emit(npmMoniker); | ||
emit(this.createPackageInformationEdge(npmMoniker.id, this.packageInformation.id)); | ||
emit(this.createNextEdge(moniker.id, npmMoniker.id)); | ||
} | ||
emit(moniker); | ||
if (this.packageInformation !== undefined) { | ||
emit({ type: lsif_protocol_1.ElementTypes.edge, label: lsif_protocol_1.EdgeLabels.packageInformation, id: this.idGenerator(), outV: moniker.id, inV: this.packageInformation.id }); | ||
} | ||
} | ||
@@ -102,108 +165,68 @@ isPackaged(uri) { | ||
} | ||
emitPackageInformation(moniker) { | ||
ensurePackageInformation() { | ||
if (this.packageInformation === undefined) { | ||
this.ensureIdGenerator(moniker.id); | ||
this.packageInformation = { | ||
type: lsif_protocol_1.ElementTypes.vertex, | ||
label: lsif_protocol_1.VertexLabels.packageInformation, | ||
id: this.idGenerator(), | ||
name: this.packageInfo.name, | ||
manager: 'npm', | ||
version: this.packageInfo.version | ||
}; | ||
if (this.packageInfo.hasRepository()) { | ||
this.packageInformation.repository = this.packageInfo.repository; | ||
} | ||
this.packageInformation = this.createPackageInformation(this.packageJson); | ||
emit(this.packageInformation); | ||
} | ||
} | ||
ensureIdGenerator(id) { | ||
if (this._idGenerator !== undefined) { | ||
} | ||
class ImportLinker extends Linker { | ||
constructor(projectRoot) { | ||
super(); | ||
this.projectRoot = projectRoot; | ||
this.packageData = new Map(); | ||
} | ||
handleMoniker(moniker) { | ||
if (moniker.kind !== lsif_protocol_1.MonikerKind.import || moniker.scheme !== moniker_1.TscMoniker.scheme) { | ||
return; | ||
} | ||
if (typeof id === 'number') { | ||
let counter = Number.MAX_SAFE_INTEGER; | ||
this._idGenerator = () => { | ||
return counter--; | ||
}; | ||
this.ensureIdGenerator(moniker.id); | ||
emit(moniker); | ||
const tscMoniker = moniker_1.TscMoniker.parse(moniker.identifier); | ||
if (!moniker_1.TscMoniker.hasPath(tscMoniker)) { | ||
return; | ||
} | ||
else { | ||
this._idGenerator = () => { | ||
return uuid.v4(); | ||
}; | ||
} | ||
} | ||
} | ||
class ImportLinker { | ||
constructor() { | ||
this.packageJsons = new Map(); | ||
this.monikers = new Map(); | ||
this.packageInfos = new Map(); | ||
} | ||
handlePackageInformation(packageInfo) { | ||
this.packageInfos.set(packageInfo.id, packageInfo); | ||
emit(packageInfo); | ||
} | ||
handleMoniker(moniker) { | ||
this.monikers.set(moniker.id, moniker); | ||
} | ||
handlePackageInformationEdge(edge) { | ||
const moniker = this.monikers.get(edge.outV); | ||
const packageInfo = this.packageInfos.get(edge.inV); | ||
// do not delete package info from the cache since it is reused by many monikers | ||
// we do remove the moniker since it should only be rewritten once. | ||
this.monikers.delete(edge.outV); | ||
if (moniker !== undefined) { | ||
if (moniker.kind === lsif_protocol_1.MonikerKind.import && moniker.scheme === moniker_1.TscMoniker.scheme && packageInfo !== undefined && packageInfo.manager === moniker_1.NpmMoniker.schema) { | ||
const tscMoniker = moniker_1.TscMoniker.parse(moniker.identifier); | ||
if (moniker_1.TscMoniker.hasPath(tscMoniker)) { | ||
const packageJson = this.getPackageJson(packageInfo); | ||
if (packageJson !== undefined) { | ||
const modulePart = `node_modules/${packageInfo.name}`; | ||
const index = tscMoniker.path.lastIndexOf(modulePart); | ||
const relativePath = tscMoniker.path.substr(index + modulePart.length + 1); | ||
if (relativePath === packageJson.main || relativePath === packageJson.typings) { | ||
moniker.identifier = moniker_1.NpmMoniker.create(packageInfo.name, undefined, tscMoniker.name); | ||
} | ||
else { | ||
moniker.identifier = moniker_1.NpmMoniker.create(packageInfo.name, relativePath, tscMoniker.name); | ||
} | ||
moniker.scheme = moniker_1.NpmMoniker.schema; | ||
} | ||
} | ||
let parts = tscMoniker.path.split('/'); | ||
let packagePath; | ||
let monikerPath; | ||
for (let i = parts.length - 1; i >= 0; i--) { | ||
let part = parts[i]; | ||
if (part === 'node_modules') { | ||
// End is exclusive and one for the name | ||
packagePath = path.join(this.projectRoot, ...parts.slice(0, i + 2), `package.json`); | ||
monikerPath = parts.slice(i + 2).join('/'); | ||
break; | ||
} | ||
emit(moniker); | ||
} | ||
emit(edge); | ||
} | ||
handleMonikerEdge(edge) { | ||
const vertex = this.monikers.get(edge.inV); | ||
// we see a moniker edge before the moniker got converted. So no | ||
// package information available. Simply re-emit. | ||
if (vertex !== undefined) { | ||
this.monikers.delete(edge.inV); | ||
emit(vertex); | ||
if (packagePath === undefined || (monikerPath !== undefined && monikerPath.length === 0)) { | ||
return; | ||
} | ||
emit(edge); | ||
} | ||
getPackageJson(packageInfo) { | ||
let result = this.packageJsons.get(packageInfo.id); | ||
if (result === null) { | ||
return undefined; | ||
let packageData = this.packageData.get(packagePath); | ||
if (packageData === undefined) { | ||
let packageJson = package_1.default.read(packagePath); | ||
if (packageJson === undefined) { | ||
this.packageData.set(packagePath, null); | ||
} | ||
else { | ||
packageData = { | ||
packageInfo: this.createPackageInformation(packageJson), | ||
packageJson: packageJson | ||
}; | ||
emit(packageData.packageInfo); | ||
this.packageData.set(packagePath, packageData); | ||
} | ||
} | ||
if (result === undefined) { | ||
if (packageInfo.uri === undefined) { | ||
this.packageJsons.set(packageInfo.id, null); | ||
return undefined; | ||
if (packageData !== null && packageData !== undefined) { | ||
let npmIdentifier; | ||
if (packageData.packageJson.typings === monikerPath || packageData.packageJson.main === monikerPath) { | ||
npmIdentifier = moniker_1.NpmMoniker.create(packageData.packageJson.name, undefined, tscMoniker.name); | ||
} | ||
const filePath = vscode_uri_1.default.parse(packageInfo.uri).fsPath; | ||
result = package_1.default.read(filePath); | ||
if (result === undefined) { | ||
this.packageJsons.set(packageInfo.id, null); | ||
} | ||
else { | ||
this.packageJsons.set(packageInfo.id, result); | ||
npmIdentifier = moniker_1.NpmMoniker.create(packageData.packageJson.name, monikerPath, tscMoniker.name); | ||
} | ||
let npmMoniker = this.createMoniker(npmIdentifier, moniker.kind, moniker_1.NpmMoniker.scheme); | ||
emit(npmMoniker); | ||
emit(this.createPackageInformationEdge(npmMoniker.id, packageData.packageInfo.id)); | ||
emit(this.createNextEdge(npmMoniker.id, moniker.id)); | ||
} | ||
return result; | ||
} | ||
@@ -257,18 +280,18 @@ } | ||
const packageJson = package_1.default.read(packageFile); | ||
let exportLinker; | ||
if (packageJson === undefined) { | ||
console.warn(`No package.json file found. Will not rewrite export monikers.`); | ||
} | ||
else { | ||
let projectRoot = options.projectRoot; | ||
if (projectRoot === undefined) { | ||
projectRoot = path.posix.dirname(packageFile); | ||
} | ||
let projectRoot = options.projectRoot; | ||
if (projectRoot === undefined && packageFile !== undefined) { | ||
projectRoot = path.posix.dirname(packageFile); | ||
if (!path.isAbsolute(projectRoot)) { | ||
projectRoot = makeAbsolute(projectRoot); | ||
} | ||
} | ||
if (projectRoot === undefined) { | ||
process.exitCode = -1; | ||
return; | ||
} | ||
let exportLinker; | ||
if (packageJson !== undefined) { | ||
exportLinker = new ExportLinker(projectRoot, packageJson); | ||
} | ||
//const exportLinker: undefined; //ExportLinker = new ExportLinker(packageJson); | ||
const importLinker = new ImportLinker(); | ||
const importLinker = new ImportLinker(projectRoot); | ||
let input = process.stdin; | ||
@@ -281,31 +304,10 @@ if (options.file !== undefined && fs.existsSync(options.file)) { | ||
let element = JSON.parse(line); | ||
if (element.type === lsif_protocol_1.ElementTypes.edge) { | ||
if (element.type === lsif_protocol_1.ElementTypes.vertex) { | ||
switch (element.label) { | ||
case lsif_protocol_1.EdgeLabels.moniker: | ||
importLinker.handleMonikerEdge(element); | ||
break; | ||
case lsif_protocol_1.EdgeLabels.packageInformation: | ||
importLinker.handlePackageInformationEdge(element); | ||
break; | ||
default: | ||
emit(line); | ||
} | ||
} | ||
else if (element.type === lsif_protocol_1.ElementTypes.vertex) { | ||
switch (element.label) { | ||
case lsif_protocol_1.VertexLabels.moniker: | ||
switch (element.kind) { | ||
case lsif_protocol_1.MonikerKind.import: | ||
importLinker.handleMoniker(element); | ||
break; | ||
case lsif_protocol_1.MonikerKind.export: | ||
exportLinker && exportLinker.handleMoniker(element); | ||
break; | ||
default: | ||
emit(line); | ||
if (exportLinker !== undefined) { | ||
exportLinker.handleMoniker(element); | ||
} | ||
importLinker.handleMoniker(element); | ||
break; | ||
case lsif_protocol_1.VertexLabels.packageInformation: | ||
importLinker.handlePackageInformation(element); | ||
break; | ||
default: | ||
@@ -315,2 +317,5 @@ emit(line); | ||
} | ||
else { | ||
emit(line); | ||
} | ||
}); | ||
@@ -317,0 +322,0 @@ } |
{ | ||
"name": "lsif-npm", | ||
"description": "A tools to rewrite Typescript LSIF monikers into npm monikers", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"author": "Microsoft Corporation", | ||
@@ -22,4 +22,4 @@ "license": "MIT", | ||
"uuid": "^3.3.2", | ||
"lsif-protocol": "0.3.1", | ||
"lsif-tsc": "0.3.0" | ||
"lsif-protocol": "0.4.0", | ||
"lsif-tsc": "0.4.0" | ||
}, | ||
@@ -26,0 +26,0 @@ "devDependencies": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
392
22287
+ Addedlsif-protocol@0.4.0(transitive)
+ Addedlsif-tsc@0.4.0(transitive)
+ Addedvscode-uri@2.1.2(transitive)
- Removedlsif-protocol@0.3.1(transitive)
- Removedlsif-tsc@0.3.0(transitive)
- Removedvscode-uri@1.0.8(transitive)
Updatedlsif-protocol@0.4.0
Updatedlsif-tsc@0.4.0