inherits-ex
Advanced tools
Comparing version 2.1.0-alpha.6 to 2.1.0-alpha.7
@@ -11,3 +11,3 @@ "use strict"; | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
// get latest non-empty constructor function through inherits link | ||
// get latest non-empty constructor function through inherits chain | ||
@@ -14,0 +14,0 @@ /** |
@@ -10,2 +10,3 @@ export * from "./_clone.js"; | ||
export * from "./get-class-by-name.js"; | ||
export * from "./getClosestCommonAncestorCtor.js"; | ||
export * from "./getConstructor.js"; | ||
@@ -17,2 +18,3 @@ export * from "./getCtorOfProperty.js"; | ||
export * from "./getPrototypeOf.js"; | ||
export * from "./getRootCtor.js"; | ||
export * from "./getSuper.js"; | ||
@@ -19,0 +21,0 @@ export * from "./getSuperCtor.js"; |
@@ -105,2 +105,13 @@ "use strict"; | ||
}); | ||
var _getClosestCommonAncestorCtor = require("./getClosestCommonAncestorCtor.js"); | ||
Object.keys(_getClosestCommonAncestorCtor).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _getClosestCommonAncestorCtor[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _getClosestCommonAncestorCtor[key]; | ||
} | ||
}); | ||
}); | ||
var _getConstructor = require("./getConstructor.js"); | ||
@@ -172,2 +183,13 @@ Object.keys(_getConstructor).forEach(function (key) { | ||
}); | ||
var _getRootCtor = require("./getRootCtor.js"); | ||
Object.keys(_getRootCtor).forEach(function (key) { | ||
if (key === "default" || key === "__esModule") return; | ||
if (key in exports && exports[key] === _getRootCtor[key]) return; | ||
Object.defineProperty(exports, key, { | ||
enumerable: true, | ||
get: function () { | ||
return _getRootCtor[key]; | ||
} | ||
}); | ||
}); | ||
var _getSuper = require("./getSuper.js"); | ||
@@ -174,0 +196,0 @@ Object.keys(_getSuper).forEach(function (key) { |
@@ -12,2 +12,4 @@ "use strict"; | ||
var _getSuperCtor = _interopRequireDefault(require("./getSuperCtor.js")); | ||
var _getRootCtor = _interopRequireDefault(require("./getRootCtor.js")); | ||
var _getClosestCommonAncestorCtor = _interopRequireDefault(require("./getClosestCommonAncestorCtor.js")); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -19,2 +21,13 @@ const isArray = Array.isArray; | ||
* | ||
* **Note**: | ||
* | ||
* The `superCtor`'s root will be inherited from the `ctor`'s parent class if the `ctor` has already inherited from others. | ||
* | ||
* ``` | ||
* ctor -> ctorParent -> ctorRoot | ||
* superCtor -> superParent | ||
* ------------------------ | ||
* ctor -> superCtor -> superParent -> ctorParent -> ctorRoot | ||
* | ||
* ``` | ||
* @param {function} ctor the class which needs to inherit the prototype. | ||
@@ -36,2 +49,9 @@ * @param {function} superCtor the parent class to inherit prototype from. | ||
(0, _inheritsDirectly.inheritsDirectly)(ctor, superCtor, staticInherit); | ||
let rootCtor = (0, _getClosestCommonAncestorCtor.default)(v, superCtor); | ||
// if (!rootCtor || rootCtor === Object) { | ||
// rootCtor = undefined | ||
// } | ||
superCtor = (0, _getRootCtor.default)(superCtor, rootCtor); | ||
if (v && v !== Object) (0, _inheritsDirectly.inheritsDirectly)(superCtor, v); | ||
/* | ||
// patch the missing prototype chain if exists ctor.super. | ||
@@ -41,5 +61,8 @@ while (v != null && v !== Object && superCtor !== v) { | ||
superCtor = v; | ||
(0, _inheritsDirectly.inheritsDirectly)(ctor, superCtor, staticInherit); | ||
v = (0, _getSuperCtor.default)(ctor); | ||
if (!isInheritedFrom(ctor, superCtor) && !isInheritedFrom(superCtor, ctor)) { | ||
inheritsDirectly(ctor, superCtor, staticInherit); | ||
} | ||
v = getSuperCtor(ctor); | ||
} | ||
*/ | ||
result = true; | ||
@@ -46,0 +69,0 @@ } else if (isInherited) { |
@@ -1,3 +0,3 @@ | ||
export const setPrototypeOf: any; | ||
export const setPrototypeOf: (o: any, proto: object) => any; | ||
export default _setPrototypeOf; | ||
declare let _setPrototypeOf: any; | ||
declare let _setPrototypeOf: (o: any, proto: object) => any; |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/snowyu/inherits-ex.js", | ||
"version": "2.1.0-alpha.6", | ||
"version": "2.1.0-alpha.7", | ||
"author": { | ||
@@ -8,0 +8,0 @@ "name": "Riceball LEE", |
import getSuperCtor from './getSuperCtor.js'; | ||
import { isEmptyFunction } from './isEmptyFunction.js' | ||
// get latest non-empty constructor function through inherits link | ||
// get latest non-empty constructor function through inherits chain | ||
@@ -6,0 +6,0 @@ /** |
@@ -10,2 +10,3 @@ export * from './_clone.js'; | ||
export * from './get-class-by-name.js'; | ||
export * from './getClosestCommonAncestorCtor.js' | ||
export * from './getConstructor.js'; | ||
@@ -17,2 +18,3 @@ export * from './getCtorOfProperty.js'; | ||
export * from './getPrototypeOf.js'; | ||
export * from './getRootCtor.js'; | ||
export * from './getSuper.js'; | ||
@@ -19,0 +21,0 @@ export * from './getSuperCtor.js'; |
@@ -5,2 +5,4 @@ import { isInheritedFrom } from './isInheritedFrom.js'; | ||
import getSuperCtor from './getSuperCtor.js'; | ||
import getRootCtor from './getRootCtor.js'; | ||
import getClosestCommonAncestorCtor from './getClosestCommonAncestorCtor.js'; | ||
@@ -12,2 +14,13 @@ const isArray = Array.isArray; | ||
* | ||
* **Note**: | ||
* | ||
* The `superCtor`'s root will be inherited from the `ctor`'s parent class if the `ctor` has already inherited from others. | ||
* | ||
* ``` | ||
* ctor -> ctorParent -> ctorRoot | ||
* superCtor -> superParent | ||
* ------------------------ | ||
* ctor -> superCtor -> superParent -> ctorParent -> ctorRoot | ||
* | ||
* ``` | ||
* @param {function} ctor the class which needs to inherit the prototype. | ||
@@ -29,2 +42,9 @@ * @param {function} superCtor the parent class to inherit prototype from. | ||
inheritsDirectly(ctor, superCtor, staticInherit); | ||
let rootCtor = getClosestCommonAncestorCtor(v, superCtor) | ||
// if (!rootCtor || rootCtor === Object) { | ||
// rootCtor = undefined | ||
// } | ||
superCtor = getRootCtor(superCtor, rootCtor) | ||
if (v && v !== Object) inheritsDirectly(superCtor, v) | ||
/* | ||
// patch the missing prototype chain if exists ctor.super. | ||
@@ -34,5 +54,8 @@ while (v != null && v !== Object && superCtor !== v) { | ||
superCtor = v; | ||
inheritsDirectly(ctor, superCtor, staticInherit); | ||
if (!isInheritedFrom(ctor, superCtor) && !isInheritedFrom(superCtor, ctor)) { | ||
inheritsDirectly(ctor, superCtor, staticInherit); | ||
} | ||
v = getSuperCtor(ctor); | ||
} | ||
*/ | ||
result = true; | ||
@@ -39,0 +62,0 @@ } else if (isInherited) { |
@@ -1,6 +0,3 @@ | ||
try{ | ||
eval('class A{}') | ||
require('./inherits-es6') | ||
require('./mixin-es6') | ||
}catch(err){ | ||
} | ||
import './inherits-es6' | ||
import './mixin-es6' | ||
@@ -33,4 +33,4 @@ import chai from 'chai' | ||
aMethod = () => "aMethod" | ||
a1Method = () => "a1Method" | ||
function aMethod() {return "aMethod"} | ||
const a1Method = () => "a1Method" | ||
@@ -141,3 +141,3 @@ class Root { | ||
assert.equal(isInheritedFrom(A, B), false, "A is not inherited from B") | ||
o = new A() | ||
let o = new A() | ||
assert.equal(o.rootMethod, Root.prototype.rootMethod) | ||
@@ -169,3 +169,7 @@ o = new A1() | ||
class MyClass{} | ||
assert.equal(inherits(C, Root), true) | ||
class B{} | ||
// C -> Root | ||
assert.equal(inherits(C, Root), true); | ||
// B -> Root | ||
assert.equal(inherits(B, Root), true); | ||
@@ -183,2 +187,22 @@ //# MyClass -> B -> Root | ||
}) | ||
it("should multi-inheritances and void circular inherit2", () => { | ||
class Ctor {} | ||
class CtorParent {} | ||
class CtorRoot {} | ||
inherits(Ctor, [CtorParent, CtorRoot]) | ||
class SuperCtor {} | ||
class SuperParent {} | ||
inherits(SuperCtor, SuperParent) | ||
assert.equal(inherits(Ctor, SuperCtor), true); | ||
assert.deepEqual(getProtoChain(Ctor), ['CtorRoot', 'CtorParent', 'SuperParent', 'SuperCtor', 'Ctor']); | ||
}); | ||
it("should multi-inheritances and void circular inherit3", () => { | ||
class CtorRoot {} | ||
class CtorParent extends CtorRoot{} | ||
class Ctor extends CtorParent{} | ||
class SuperParent {} | ||
class SuperCtor extends SuperParent{} | ||
assert.equal(inherits(Ctor, SuperCtor), true); | ||
assert.deepEqual(getProtoChain(Ctor), ['CtorRoot', 'CtorParent', 'SuperParent', 'SuperCtor', 'Ctor']); | ||
}); | ||
it("test isInheritedFrom with class name", function(){ | ||
@@ -194,5 +218,5 @@ assert.equal(isInheritedFrom(A, 'Root'), A) | ||
var cMethod = ()=> "cMethod" | ||
var C = function(){return "C"} | ||
function C(){return "C"} | ||
C.name = "C" | ||
// C.name = "C" | ||
C.prototype.cMethod = cMethod | ||
@@ -202,3 +226,3 @@ var b = new B() | ||
// bProto = b.__proto__ | ||
bProto = getPrototypeOf(b) | ||
let bProto = getPrototypeOf(b) | ||
assert.equal(bProto.cMethod, cMethod) | ||
@@ -219,11 +243,11 @@ assert.equal(bProto.constructor, C) | ||
var R = function(){return "R"} | ||
R.name = "R" | ||
// R.name = "R" | ||
function C(){return "C"} | ||
C.name = "C" | ||
// C.name = "C" | ||
C.prototype.cMethod = cMethod | ||
var C1 = function(){ return "C1"} | ||
C1.name = "C1" | ||
// C1.name = "C1" | ||
var C11 = function() {return "C11"} | ||
C11.name = "C11" | ||
// C11.name = "C11" | ||
var C2 = function() {return "C2"} | ||
@@ -236,3 +260,3 @@ | ||
//# C11 > C1 > C | ||
baseClass = isInheritedFrom(C11, C) | ||
const baseClass = isInheritedFrom(C11, C) | ||
assert.equal(baseClass, C1) | ||
@@ -252,3 +276,3 @@ inheritsDirectly(baseClass, C2) | ||
//# C11 > C1 > C | ||
baseClass = isInheritedFrom(C11, C) | ||
const baseClass = isInheritedFrom(C11, C) | ||
assert.equal(baseClass, C1) | ||
@@ -330,3 +354,3 @@ inheritsDirectly(baseClass, C2) | ||
class A {} | ||
result = createObject(A) | ||
const result = createObject(A) | ||
result.should.be.instanceof(A) | ||
@@ -338,3 +362,3 @@ A.should.not.have.ownProperty('Class') | ||
assert.equal(inherits(A12, A1), true) | ||
a = createObject(A12) | ||
const a = createObject(A12) | ||
assert.equal(a.Class, A12) | ||
@@ -352,3 +376,3 @@ assert.instanceOf(a, A12) | ||
assert.equal(inherits(A2, A), true) | ||
a = createObject(A2) | ||
const a = createObject(A2) | ||
assert.equal(a.Class, A2) | ||
@@ -368,3 +392,3 @@ assert.instanceOf(a, A2) | ||
assert.equal(inherits(A2, A), true) | ||
a = createObject(A2, "hiFirst", 1881) | ||
const a = createObject(A2, "hiFirst", 1881) | ||
assert.instanceOf(a, A2) | ||
@@ -378,3 +402,3 @@ assert.instanceOf(a, A) | ||
class A2{} | ||
a = createObject(A2) | ||
const a = createObject(A2) | ||
assert.instanceOf(a, A2) | ||
@@ -434,3 +458,3 @@ a.should.have.property('Class', A2) | ||
inherits(A2, R) | ||
a = createObject(A2) | ||
const a = createObject(A2) | ||
assert.instanceOf(a, A2) | ||
@@ -455,3 +479,3 @@ a.should.have.property('Class', A2) | ||
inherits(A2, R).should.be.ok | ||
a = createObject(A2) | ||
const a = createObject(A2) | ||
assert.instanceOf(a, A2) | ||
@@ -458,0 +482,0 @@ a.should.have.property('Class', A2) |
@@ -158,5 +158,5 @@ import chai from 'chai' | ||
it("should not inheritances dead circular", () => { | ||
class C1 {}; | ||
class C2 {}; | ||
class C3 {}; | ||
function C1() {}; | ||
function C2() {}; | ||
function C3() {}; | ||
assert.equal(inherits(C1, C2), true); | ||
@@ -167,6 +167,6 @@ assert.equal(inherits(C2, C3), true); | ||
it("should multi-inheritances", () => { | ||
class C {}; | ||
class D {}; | ||
class E {}; | ||
class MyClass {}; | ||
function C() {}; | ||
function D() {}; | ||
function E() {}; | ||
function MyClass() {}; | ||
// MyClass -> C -> D -> E | ||
@@ -177,5 +177,9 @@ assert.equal(inherits(MyClass, [C, D, E]), true); | ||
it("should multi-inheritances and void circular inherit", () => { | ||
class C {}; | ||
class MyClass {}; | ||
function C() {}; | ||
function MyClass() {}; | ||
function B() {}; | ||
// C -> Root | ||
assert.equal(inherits(C, Root), true); | ||
// B -> Root | ||
assert.equal(inherits(B, Root), true); | ||
// MyClass -> B -> Root | ||
@@ -191,2 +195,15 @@ assert.equal(inherits(MyClass, B), true); | ||
}); | ||
it("should multi-inheritances and void circular inherit2", () => { | ||
function Ctor() {} | ||
function CtorParent() {} | ||
function CtorRoot() {} | ||
function SuperCtor() {} | ||
function SuperParent() {} | ||
// Ctor -> CtorParent -> CtorRoot | ||
inherits(Ctor, [CtorParent, CtorRoot]) | ||
// SuperCtor -> SuperParent | ||
inherits(SuperCtor, SuperParent) | ||
assert.equal(inherits(Ctor, SuperCtor), true); | ||
assert.deepEqual(getProtoChain(Ctor), ['CtorRoot', 'CtorParent', 'SuperParent', 'SuperCtor', 'Ctor']); | ||
}); | ||
it("test isInheritedFrom with class name", () => { | ||
@@ -193,0 +210,0 @@ assert.equal(isInheritedFrom(A, 'Root'), A); |
@@ -58,3 +58,3 @@ import chai from 'chai' | ||
// log(getProtoChain(B1)) | ||
o = new B1() | ||
const o = new B1() | ||
expect(o.prop1).to.be.equal(2) | ||
@@ -61,0 +61,0 @@ // o.should.have.property('prop1', 2) |
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
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
255063
135
6730
2