Skip to content

Commit

Permalink
Use of hasOwnProperty is required
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Nov 18, 2022
1 parent 4df9768 commit 6a38375
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,16 +193,16 @@ export class JsonTemplateTranslator {
private translatePathRoot(path: PathExpression, dest: string, ctx: string): string {
if (typeof path.root === 'object') {
return this.translateExpr(path.root, dest, ctx);
} else if (path.subPath && path.parts.length) {
if (JsonTemplateTranslator.isSinglePropSelection(path.parts[0])) {
const part = path.parts.shift() as SelectorExpression;
const propStr = CommonUtils.escapeStr(part.prop?.value);
const code: string[] = [];
code.push(`if(!${ctx}[${propStr}]) {continue;}`);
code.push(`${dest} = ${ctx}[${propStr}];`);
return code.join('');
}
}
} else if(path.subPath && path.parts.length) {
if(JsonTemplateTranslator.isSinglePropSelection(path.parts[0])) {
const part = path.parts.shift() as SelectorExpression;
const propStr = CommonUtils.escapeStr(part.prop?.value);
const code: string[] = [];
code.push(`if(!${ctx}[${propStr}]) {continue;}`);
code.push(`${dest} = ${ctx}[${propStr}];`);
return code.join('');
}
}
return `${dest} = ${path.root || ctx};`;
}

Expand All @@ -223,7 +223,7 @@ export class JsonTemplateTranslator {
if (JsonTemplateTranslator.isSinglePropSelection(part)) {
const selector = part as SelectorExpression;
const propStr = CommonUtils.escapeStr(selector.prop?.value);
code.push(`if(${data}[${propStr}]){`);
code.push(`if(Object.prototype.hasOwnProperty.call(${data}, ${propStr})){`);
code.push(`${data} = [${data}];`);
code.push('}');
} else if (JsonTemplateTranslator.isArrayFilterExpr(part)) {
Expand Down Expand Up @@ -284,7 +284,7 @@ export class JsonTemplateTranslator {
code.push(`${dest} = ${valuesCode}.flat();`);
} else if (prop) {
const propStr = CommonUtils.escapeStr(prop);
code.push(`if(${ctx}[${propStr}]){`);
code.push(`if(${ctx} && Object.prototype.hasOwnProperty.call(${ctx}, ${propStr})){`);
code.push(`${dest}=${ctx}[${propStr}];`);
code.push('} else {');
code.push(`${dest} = undefined`);
Expand Down Expand Up @@ -328,7 +328,7 @@ export class JsonTemplateTranslator {
if (prop?.value === '*') {
code.push(`${result} = ${result}.concat(${valuesCode});`);
} else {
code.push(`if(${currCtx}[${propStr}]){`);
code.push(`if(Object.prototype.hasOwnProperty.call(${currCtx}, ${propStr})){`);
code.push(`${result}.push(${currCtx}[${propStr}]);`);
code.push('}');
}
Expand Down Expand Up @@ -587,10 +587,9 @@ export class JsonTemplateTranslator {
private static isSinglePropSelection(expr: Expression): boolean {
if (expr.type === SyntaxType.SELECTOR) {
const part = expr as SelectorExpression;
return (
part.selector === '.' &&
(part.prop?.type === TokenType.ID || part.prop?.type === TokenType.STR)
);
return part.selector === '.' &&
(part.prop?.type === TokenType.ID ||
part.prop?.type === TokenType.STR);
}
return false;
}
Expand Down Expand Up @@ -639,7 +638,9 @@ export class JsonTemplateTranslator {
}
code.push(`${resultVar} = {};`);
code.push(`for(let key of ${allKeys}){`);
code.push(`if(${ctx}[key]){${resultVar}[key] = ${ctx}[key];}`);
code.push(
`if(Object.prototype.hasOwnProperty.call(${ctx}, key)){${resultVar}[key] = ${ctx}[key];}`,
);
code.push('}');
return code.join('');
}
Expand All @@ -650,14 +651,16 @@ export class JsonTemplateTranslator {
code.push(`for(let key of ${allKeys}){`);
code.push(`if(typeof key === 'string'){`);
code.push(`for(let childCtx of ${ctx}){`);
code.push(`if(childCtx[key]){`);
code.push(`if(Object.prototype.hasOwnProperty.call(childCtx, key)){`);
code.push(`${resultVar}.push(childCtx[key]);`);
code.push('}');
code.push('}');
code.push('continue;');
code.push('}');
code.push(`if(key < 0){key = ${ctx}.length + key;}`);
code.push(`if(${ctx}[key]){${resultVar}.push(${ctx}[key]);}`);
code.push(
`if(Object.prototype.hasOwnProperty.call(${ctx}, key)){${resultVar}.push(${ctx}[key]);}`,
);
code.push('}');
code.push(`if(${allKeys}.length === 1) {${resultVar} = ${resultVar}[0];}`);
return code.join('');
Expand Down

0 comments on commit 6a38375

Please sign in to comment.