Skip to content

Commit

Permalink
fix eslint and reformat some code
Browse files Browse the repository at this point in the history
  • Loading branch information
hax committed Jun 29, 2015
1 parent c01bbd3 commit 1a7bd92
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 44 deletions.
6 changes: 2 additions & 4 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@
rules:
semi: [2, never]
quotes: [2, single, avoid-escape]
strict: [2, global]
global-strict: 0
no-use-before-define: [2, nofunc]
curly: [2, multi-line]
no-underscore-dangle: 0
no-comma-dangle: 0
new-parens: 0
no-proto: 0
global-strict: 0
no-constant-condition: 1
no-spaced-func: 1
new-cap: [2, capIsNew: false]
82 changes: 44 additions & 38 deletions src/class.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,48 @@ Class.extend = function(superclass, props) {
var protoParent, constructorParent
if (superclass === null) {
protoParent = null
} else switch (typeof superclass) {
case 'function': // TODO: should be constructor
if (typeof superclass.prototype === 'object' || superclass.prototype === null) {
protoParent = superclass.prototype
constructorParent = superclass
} else throw new TypeError('superclass.prototype should be an object or null')
break
case 'object':
protoParent = superclass
break
default: throw new TypeError('superclass should be an object or null')
} else {
switch (typeof superclass) {
case 'function': // TODO: should be constructor
if (typeof superclass.prototype === 'object' || superclass.prototype === null) {
protoParent = superclass.prototype
constructorParent = superclass
} else throw new TypeError('superclass.prototype should be an object or null')
break
case 'object':
protoParent = superclass
break
default: throw new TypeError('superclass should be an object or null')
}
}
var pds = {}
if (props !== undefined)
if (props !== undefined) {
for (var names = Object.getOwnPropertyNames(props), i = 0; i < names.length; i++) {
var name = names[i]
pds[name] = Object.getOwnPropertyDescriptor(props, name)
}
}
return function(methods) {
return createClass(methods, protoParent, pds, constructorParent)
}
}

var _isPrototypeOf = {}.isPrototypeOf
var inherit = supportSetProto() ?
function (obj, proto) { obj.__proto__ = proto } :
function (obj, proto) {
// copy all properties from proto
while (proto !== null && !_isPrototypeOf.call(proto, obj)) { // stop if obj has the same prototype
for (var names = Object.getOwnPropertyNames(proto), i = 0; i < names.length; i++) {
var name = names[i]
if (!obj.hasOwnProperty(name)) {
Object.defineProperty(obj, name, Object.getOwnPropertyDescriptor(proto, name))
}
}
proto = Object.getPrototypeOf(proto)
}
}

function createClass(methods, protoParent, protoPDs, constructorParent) {
var methodDescriptors = {}
if (arguments.length <= 1) protoParent = Object.prototype
Expand All @@ -43,9 +62,9 @@ function createClass(methods, protoParent, protoPDs, constructorParent) {
var name = names[i]
var pd = Object.getOwnPropertyDescriptor(methods, name)
if ('value' in pd) {
if (typeof pd.value === 'function')
if (typeof pd.value === 'function') {
pd.value = createMethod(name, pd.value, protoParent)
else throw new TypeError('[' + name + '] is not a function')
} else throw new TypeError('[' + name + '] is not a function')
} else {
if (pd.get) pd.get = createMethod('get ' + name, pd.get, protoParent)
if (pd.set) pd.set = createMethod('set ' + name, pd.set, protoParent)
Expand Down Expand Up @@ -73,22 +92,6 @@ function createClass(methods, protoParent, protoPDs, constructorParent) {
return Ctor
}

var _isPrototypeOf = {}.isPrototypeOf
var inherit = supportSetProto() ?
function (obj, proto) { obj.__proto__ = proto } :
function (obj, proto) {
// copy all properties from proto
while (proto !== null && !_isPrototypeOf.call(proto, obj)) { // stop if obj has the same prototype
for (var names = Object.getOwnPropertyNames(proto), i = 0; i < names.length; i++) {
var name = names[i]
if (!obj.hasOwnProperty(name))
Object.defineProperty(obj, name,
Object.getOwnPropertyDescriptor(proto, name))
}
proto = Object.getPrototypeOf(proto)
}
}

function supportSetProto() {
var x = {}
x.__proto__ = C.prototype
Expand All @@ -103,12 +106,13 @@ var toFunctionSource = f.toSource || f.toString
function createMethod(name, func, base) {
var params = /\(([\s\S]*?)\)/.exec(toFunctionSource.call(func))[1].split(/\s*,\s*/)
var method
if (params[0] === '$super') method = function() {
var args = [].slice.call(arguments)
args.unshift(createSuper(this, base, name))
return func.apply(this, args)
}
else method = func
if (params[0] === '$super') {
method = function() {
var args = [].slice.call(arguments)
args.unshift(createSuper(this, base, name))
return func.apply(this, args)
}
} else method = func
return method
}

Expand All @@ -125,14 +129,16 @@ function createSuper(actualThis, base, methodName) {
}
Super.get = function(name) {
var p = base, get
while (!(get = Object.getOwnPropertyDescriptor(p, name).get))
while (!(get = Object.getOwnPropertyDescriptor(p, name).get)) {
p = Object.getPrototypeOf(p)
}
return get.apply(actualThis)
}
Super.set = function(name, value) {
var p = base, set
while (!(set = Object.getOwnPropertyDescriptor(p, name).set))
while (!(set = Object.getOwnPropertyDescriptor(p, name).set)) {
p = Object.getPrototypeOf(p)
}
return set.apply(actualThis, [value])
}
inherit(Super, createSuperProto(actualThis, base))
Expand Down
5 changes: 3 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ test('class extends Super {}', function(){
var Test = Class.extend(Super)()

assert.isFunction(Test)
if ('__proto__' in function(){})
if ('__proto__' in function(){}) {
assert.egal(Object.getPrototypeOf(Test), Super)
else
} else {
assert.egal(Test.className, 'Super')
}

assert.egal(Object.getPrototypeOf(Test.prototype), Super.prototype)
assert.egal(Test.prototype.constructor, Test)
Expand Down

0 comments on commit 1a7bd92

Please sign in to comment.