@azure-tools/codegen
Advanced tools
Comparing version 2.7.0 to 2.8.0-dev.1
@@ -23,3 +23,3 @@ { | ||
"packages/libs/codegen/src/safe-eval.ts": "658d4e78226ece6f5f52fd8028dc3e42b88a35cd", | ||
"packages/libs/codegen/src/source-track.ts": "d6cd9672ca157c544e57b0c782756453dfdb0616", | ||
"packages/libs/codegen/src/source-track.ts": "561504a716005c1b99e72f9c6f456d0b25e1e35d", | ||
"packages/libs/codegen/src/text-manipulation.ts": "be00464a9a71edf9b790c798e3bd2d87e89fcd22", | ||
@@ -26,0 +26,0 @@ "packages/libs/codegen/src/type.ts": "5d3203985562968a486e283d9a87311fe44ee154", |
export declare function getMappings(instance: any): any; | ||
export declare function enableSourceTracking<T extends object>(instance: T, enforce?: boolean, path?: string, map?: Record<string, any>, cache?: Map<string, any>): T; | ||
export declare function shadow<T extends object>(source: T, document: string, path?: string, cache?: Map<string, any>): T; | ||
export declare const ShadowedNodePath: unique symbol; | ||
export interface ProxyPosition { | ||
[ShadowedNodePath]: Array<string | number>; | ||
} | ||
export declare type ShadowedObject<T> = T & ProxyPosition; | ||
export declare function shadowPosition<T extends object>(source: T, path?: Array<string | number>): ShadowedObject<T>; | ||
//# sourceMappingURL=source-track.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.shadow = exports.enableSourceTracking = exports.getMappings = void 0; | ||
exports.shadowPosition = exports.ShadowedNodePath = exports.enableSourceTracking = exports.getMappings = void 0; | ||
const type_1 = require("./type"); | ||
@@ -80,224 +80,42 @@ const getMap = "_#get-map#_"; | ||
exports.enableSourceTracking = enableSourceTracking; | ||
function shadow(source, document, path = "$", cache = new Map()) { | ||
let proxy = cache.get(path); | ||
if (!proxy) { | ||
proxy = new Proxy(source, { | ||
get(target, p, receiver) { | ||
if (p === getPosition) { | ||
// they want the source location for this node. | ||
return { | ||
document, | ||
Position: { path: [path] }, | ||
}; | ||
} | ||
if (p === getActualValue) { | ||
return target.valueOf(); | ||
} | ||
const value = target[p]; | ||
switch ((0, type_1.typeOf)(value)) { | ||
case "undefined": | ||
case "null": | ||
case "function": | ||
return value; | ||
case "string": | ||
case "boolean": | ||
case "number": | ||
return shadow(new Object(value), document, `${path}.${String(p)}`, cache); | ||
case "object": | ||
return shadow(value, document, `${path}.${String(p)}`, cache); | ||
case "array": | ||
return shadow(value, document, `${path}[${String(p)}]`, cache); | ||
default: | ||
throw new Error(`Unhandled shadow of type '${(0, type_1.typeOf)(value)}' `); | ||
} | ||
}, | ||
}); | ||
cache.set(path, proxy); | ||
} | ||
return proxy; | ||
exports.ShadowedNodePath = Symbol("ObjectPosition"); | ||
function shadowPosition(source, path = []) { | ||
return new Proxy(source, { | ||
get(target, p) { | ||
if (p === exports.ShadowedNodePath) { | ||
// they want the source location for this node. | ||
return path; | ||
} | ||
const value = target[p]; | ||
const key = getKey(p); | ||
switch ((0, type_1.typeOf)(value)) { | ||
case "undefined": | ||
case "null": | ||
case "function": | ||
case "string": | ||
case "boolean": | ||
case "number": | ||
return value; | ||
case "array": | ||
return shadowPosition(value, [...path, key]); | ||
case "object": | ||
return shadowPosition(value, [...path, key]); | ||
default: | ||
throw new Error(`Unhandled shadow of type '${(0, type_1.typeOf)(value)}' `); | ||
} | ||
}, | ||
}); | ||
} | ||
exports.shadow = shadow; | ||
/* | ||
export function shadow1(source: any, sourceFile: string, path = '$', hash = new WeakMap()) { | ||
if (hash.has(source)) { | ||
// cyclic reference | ||
return hash.get(source); | ||
} | ||
if (source[sourceMarker]) { | ||
return source; | ||
} | ||
switch (typeOf(source)) { | ||
case 'null': | ||
case 'undefined': | ||
case 'symbol': | ||
return source; | ||
case 'string': | ||
case 'number': | ||
case 'boolean': { | ||
const v: any = new Object(source); | ||
v[sourceMarker] = { | ||
$f: sourceFile, | ||
$p: path | ||
}; | ||
return v; | ||
exports.shadowPosition = shadowPosition; | ||
function getKey(p) { | ||
switch (typeof p) { | ||
case "symbol": | ||
return p.toString(); | ||
case "number": | ||
return p; | ||
case "string": { | ||
return isNaN(p) || isNaN(parseFloat(p)) ? p : parseInt(p, 10); | ||
} | ||
} | ||
case 'date': { | ||
const v: any = new Date(source); | ||
v[sourceMarker] = { | ||
$f: sourceFile, | ||
$p: path | ||
}; | ||
return v; | ||
} | ||
case 'array': { | ||
const v: any = []; | ||
hash.set(source, v); | ||
for (const { key, value } of items(source)) { | ||
v[key] = shadow1(value, sourceFile, `${path}[${key}]`, hash); | ||
} | ||
v[sourceMarker] = { | ||
$f: sourceFile, | ||
$p: path | ||
}; | ||
return v; | ||
} | ||
case 'set': { | ||
const v: any = new Set(); | ||
hash.set(source, v); | ||
let index = 0; | ||
for (const value of values(source)) { | ||
v[index] = shadow1(value, sourceFile, `${path}[${index}]`, hash); | ||
index++; | ||
} | ||
v[sourceMarker] = { | ||
$f: sourceFile, | ||
$p: path | ||
}; | ||
return v; | ||
} | ||
case 'map': { | ||
const v: any = new Map(); | ||
hash.set(source, v); | ||
for (const { key, value } of items(source)) { | ||
v[key] = shadow1(value, sourceFile, `${path}[${key}]`, hash); | ||
} | ||
v[sourceMarker] = { | ||
$f: sourceFile, | ||
$p: path | ||
}; | ||
return v; | ||
} | ||
case 'object': { | ||
const v: any = {}; | ||
hash.set(source, v); | ||
for (const { key, value } of items(source)) { | ||
v[key] = shadow1(value, sourceFile, `${path}.${key}`, hash); | ||
} | ||
v[sourceMarker] = { | ||
$f: sourceFile, | ||
$p: path | ||
}; | ||
return v; | ||
} | ||
} | ||
// some other object type. | ||
// we should throw, as we should know what we're dealing with. | ||
} | ||
export function attachMap<T extends object>(instance: T, enforce = true, $map: any = {}): T { | ||
// does this already hav a map attached? | ||
if ((<any>instance)[mapMarker]) { | ||
return instance; | ||
} | ||
// is it something that we can't work with? | ||
if (instance === null || instance === undefined) { | ||
return instance; | ||
} | ||
// set the marker on the original | ||
(<any>instance)[mapMarker] = $map; | ||
return new Proxy(instance, { | ||
ownKeys: (target: T): Array<PropertyKey> => Object.keys(target).filter(each => !each.startsWith('_#')), | ||
get: (target: T, p: PropertyKey, receiver: any): any => { | ||
if (p === getMap) { | ||
return (<any>target)[sourceMarker]; | ||
} | ||
if (p === getActualValue) { | ||
return target.valueOf(); | ||
} | ||
const v: any = (<any>target)[p]; | ||
const type = typeof (v); | ||
if (v === undefined || v === null || type === 'function') { | ||
return v; | ||
} | ||
if (type === 'object') { | ||
// we're asked to return a member of the object | ||
// if we check and see that the member isn't | ||
// proxied, maybe we should stop and replace it with | ||
// a proxied version before giving it back. | ||
if (!v[mapMarker]) { | ||
(<any>target)[p] = attachMap(v, enforce, $map); | ||
} | ||
return v[getActualValue]; | ||
} | ||
// whatever it is, just let it go. | ||
return v.valueOf(); | ||
}, | ||
set: (target: T, p: PropertyKey, value: any, receiver: any): boolean => { | ||
if (value === undefined || value === null) { | ||
// remove the existing value | ||
delete (<any>target)[p]; | ||
return true; | ||
} | ||
// does the object have source data? | ||
const sm = value[sourceMarker]; | ||
if (sm) { | ||
// yes! | ||
if (value.valueOf() === value) { | ||
(<any>target)[p] = attachMap(clone(value), enforce); | ||
} else { | ||
const vv: any = new Object(value.valueOf()); | ||
vv[sourceMarker] = sm; | ||
console.log(`p has sm obj ${JSON.stringify(vv[sourceMarker])}`); | ||
(<any>target)[p] = attachMap(vv, enforce); | ||
} | ||
return true; | ||
} | ||
// no source data, just set the object value | ||
if (enforce) { | ||
throw new Error(`Must supply source informaton on property '${new String(p)}' when enforce is true.`); | ||
} | ||
if (isPrimitive(value)) { | ||
console.log('prim'); | ||
// wrap it first! | ||
(<any>target)[p] = attachMap(new Object(value), enforce); | ||
return true; | ||
} | ||
(<any>target)[p] = attachMap(value, enforce); | ||
return true; | ||
} | ||
}); | ||
}*/ | ||
//# sourceMappingURL=source-track.js.map |
{ | ||
"name": "@azure-tools/codegen", | ||
"version": "2.7.0", | ||
"version": "2.8.0-dev.1", | ||
"description": "Autorest Code generator common and base classes", | ||
@@ -62,2 +62,2 @@ "directories": { | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
351316
4530
2