1
0
Fork 0

Properly handle error subtype #constructor (#__constructor alias)

Using `#constructor' in place of `#__construct' caused an error attempting
to "redefine" `#__constructor' (once the alias was applied); the system was
making improper assumptions and not accounting for aliases.

In fact, everthing about how this was being done was bad, since adding any
keywords would have thrown it off as well!

* lib/ClassBuilder.js (build): Move ector detection past prop
  parsing.  Check parsed properties instead of raw.
* test/ClassBuilder/ErrorExtendTest.js: Iterate ctor override test on new
    `ctors'.
  (caseSetUp) [ctors]: Add property.
master
Mike Gerwitz 2017-11-04 14:51:29 -04:00
parent 994c6ca1da
commit 1d9c06a169
Signed by: mikegerwitz
GPG Key ID: 8C917B7F5DC51BA2
2 changed files with 28 additions and 23 deletions

View File

@ -447,22 +447,6 @@ exports.prototype.build = function extend( _, __ )
}
}
// we transparently handle extending errors in a sane manner, which is
// traditionally a huge mess (you're welcome)
if ( this._ector && this._ector.isError( base ) )
{
// declare public properties (otherwise, they'll be confined to the
// private visibility object in ES5+ environments)
props.message = '';
props.stack = '';
// user-provided constructor
var ector_own = props.__construct;
// everything else is handled by the constructor
props.__construct = this._ector.createCtor( base, cname, ector_own );
}
// increment class identifier
this._classId++;
@ -508,6 +492,24 @@ exports.prototype.build = function extend( _, __ )
}
}
// we transparently handle extending errors in a sane manner, which is
// traditionally a huge mess (you're welcome)
if ( this._ector && this._ector.isError( base ) )
{
// declare public properties (otherwise, they'll be confined to the
// private visibility object in ES5+ environments)
props.message = '';
props.stack = '';
// user-provided constructor
var ector_own = members[ 'public' ].__construct;
// everything else is handled by the constructor
members[ 'public' ].__construct = this._ector.createCtor(
base, cname, ector_own
);
}
// reference to the parent prototype (for more experienced users)
prototype.___$$parent$$ = base.prototype;

View File

@ -38,6 +38,8 @@ require( 'common' ).testCase(
RangeError,
URIError,
];
this.ctors = [ '__construct', 'constructor' ];
},
@ -164,7 +166,7 @@ require( 'common' ).testCase(
* implementation. However, a user can provide a method to be invoked
* after the generated constructor.
*/
'Can override generated constructor': function()
'@each(ctors) Can override generated constructor': function( ctor )
{
var called_gen = false,
called_own = false;
@ -178,12 +180,13 @@ require( 'common' ).testCase(
};
};
var result = this.builder.build( {}, {
__construct: function()
{
called_own = true;
},
} )();
var dfn = {};
dfn[ ctor ] = function()
{
called_own = true;
};
var result = this.builder.build( {}, dfn )();
this.assertOk( called_gen );
this.assertOk( called_own );