tapjs / stack-utils

Captures and cleans stack traces.
https://www.npmjs.com/package/stack-utils
MIT License
190 stars 35 forks source link

Avoid property override hazard #70

Open FUDCo opened 2 years ago

FUDCo commented 2 years ago

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch stack-utils@2.0.5 for the project I'm working on.

Assignments to res.constructor trip over the JavaScript property override mistake, causing Ava test assertion failure results in some environments to become unable to be output. The problem can be reproduced if you freeze Object.prototype.

Here is the diff that solved my problem:

diff --git a/node_modules/stack-utils/index.js b/node_modules/stack-utils/index.js
index ed14bd3..ad9eeb1 100644
--- a/node_modules/stack-utils/index.js
+++ b/node_modules/stack-utils/index.js
@@ -161,7 +161,7 @@ class StackUtils {
     setFile(res, site.getFileName(), this._cwd);

     if (site.isConstructor()) {
-      res.constructor = true;
+      Object.defineProperty(res, 'constructor', { value: true });
     }

     if (site.isEval()) {
@@ -260,7 +260,7 @@ class StackUtils {
     setFile(res, file, this._cwd);

     if (ctor) {
-      res.constructor = true;
+      Object.defineProperty(res, 'constructor', { value: true });
     }

     if (evalOrigin) {

This issue body was partially generated by patch-package.

erights commented 2 years ago

PR now at https://github.com/tapjs/stack-utils/pull/71 Please review