From b184ae81af8a26be68363b3905cfc4d74411f9c1 Mon Sep 17 00:00:00 2001 From: Lukas Taegert-Atkinson Date: Sat, 12 Jan 2019 16:46:40 +0100 Subject: [PATCH] Make sure object prototype methods are not considered to be falsy --- src/ast/nodes/ObjectExpression.ts | 13 +++++++------ .../builtin-prototypes/truthiness/_config.js | 3 +++ .../samples/builtin-prototypes/truthiness/main.js | 5 +++++ 3 files changed, 15 insertions(+), 6 deletions(-) create mode 100644 test/function/samples/builtin-prototypes/truthiness/_config.js create mode 100644 test/function/samples/builtin-prototypes/truthiness/main.js diff --git a/src/ast/nodes/ObjectExpression.ts b/src/ast/nodes/ObjectExpression.ts index aa3c73e5d85..2f6d7ff06e1 100644 --- a/src/ast/nodes/ObjectExpression.ts +++ b/src/ast/nodes/ObjectExpression.ts @@ -78,6 +78,7 @@ export default class ObjectExpression extends NodeBase { if ( path.length === 1 && !this.propertyMap[key] && + !objectMembers[key] && this.unmatchablePropertiesRead.length === 0 ) { if (!this.expressionsToBeDeoptimized[key]) { @@ -167,8 +168,8 @@ export default class ObjectExpression extends NodeBase { for (const property of typeof key !== 'string' ? this.properties : this.propertyMap[key] - ? this.propertyMap[key].propertiesRead - : []) { + ? this.propertyMap[key].propertiesRead + : []) { if (property.hasEffectsWhenAccessedAtPath(subPath, options)) return true; } return false; @@ -191,10 +192,10 @@ export default class ObjectExpression extends NodeBase { for (const property of typeof key !== 'string' ? this.properties : path.length > 1 - ? this.propertyMap[key].propertiesRead - : this.propertyMap[key] - ? this.propertyMap[key].propertiesSet - : []) { + ? this.propertyMap[key].propertiesRead + : this.propertyMap[key] + ? this.propertyMap[key].propertiesSet + : []) { if (property.hasEffectsWhenAssignedAtPath(subPath, options)) return true; } return false; diff --git a/test/function/samples/builtin-prototypes/truthiness/_config.js b/test/function/samples/builtin-prototypes/truthiness/_config.js new file mode 100644 index 00000000000..9c57e067add --- /dev/null +++ b/test/function/samples/builtin-prototypes/truthiness/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'regards builtin methods as truthy in objects' +}; diff --git a/test/function/samples/builtin-prototypes/truthiness/main.js b/test/function/samples/builtin-prototypes/truthiness/main.js new file mode 100644 index 00000000000..f24b8952032 --- /dev/null +++ b/test/function/samples/builtin-prototypes/truthiness/main.js @@ -0,0 +1,5 @@ +if (!{}.hasOwnProperty) { + throw new Error('Prototype method evaluated as falsy'); +} + +assert.strictEqual({}.hasOwnProperty ? true : false, true);