@nodesecure/js-x-ray
Version:
JavaScript AST XRay analysis
62 lines • 1.79 kB
JavaScript
// Import Internal Dependencies
import {} from "./utils/toArrayLocation.js";
export class DefaultCollectableSet {
#entries = new Map();
type;
static mergeData(set, data) {
for (const { value, locations } of data.entries) {
for (const { file, location, metadata } of locations) {
for (const loc of location) {
set.add(value, {
file,
location: loc,
...(metadata && { metadata })
});
}
}
}
return set;
}
constructor(type) {
this.type = type;
}
add(value, { file = null, location, metadata }) {
const entry = { file, location, ...(metadata && { metadata }) };
const entries = this.#entries.get(value);
if (entries) {
entries.push(entry);
}
else {
this.#entries.set(value, [entry]);
}
}
toJSON() {
return {
type: this.type,
entries: [...this]
};
}
static fromJSON(data) {
const set = new DefaultCollectableSet(data.type);
DefaultCollectableSet.mergeData(set, data);
return set;
}
values() {
return this.#entries.keys();
}
*[Symbol.iterator]() {
for (const [value, entries] of this.#entries) {
yield {
value,
locations: entries.map(({ file, location, metadata }) => {
return {
file,
location: [location],
...(metadata && { metadata })
};
})
};
}
}
}
//# sourceMappingURL=CollectableSet.js.map