fix most gcc warnings

This commit is contained in:
Leo Horie 2016-06-03 23:55:31 -04:00
parent ca784a684e
commit ddc430a6d3
9 changed files with 248 additions and 244 deletions

View file

@ -38,10 +38,10 @@ Mithril's `config` method is now replaced by several lifecycle methods to improv
## Robustness ## Robustness
There are over 2300 assertions in the test suite, and tests cover even difficult-to-test things like `location.href`, `element.innerHTML` and `XMLHttpRequest` usage. There are over 2500 assertions in the test suite, and tests cover even difficult-to-test things like `location.href`, `element.innerHTML` and `XMLHttpRequest` usage.
## Modularity ## Modularity
Despite the huge performance improvements, the new codebase is smaller than v0.2.x, currently clocking at 5.6kb min+gzip Despite the huge performance improvements, the new codebase is smaller than v0.2.x, currently clocking at 6.5kb min+gzip
In addition, Mithril is now completely modular: you can import only the modules that you need and easily integrate 3rd party modules if you wish to use a different library for routing, ajax, and even rendering In addition, Mithril is now completely modular: you can import only the modules that you need and easily integrate 3rd party modules if you wish to use a different library for routing, ajax, and even rendering

View file

@ -1,5 +1,5 @@
"use strict" "use strict"
;(function () {
var Promise = require("./promise/promise") var Promise = require("./promise/promise")
var m = require("./render/hyperscript") var m = require("./render/hyperscript")
var renderService = require("./render/render")(window) var renderService = require("./render/render")(window)
@ -17,3 +17,4 @@ m.render = renderService.render
m.redraw = redrawService.publish m.redraw = redrawService.publish
module.exports = m module.exports = m
})()

View file

@ -1,5 +1,6 @@
"use strict" "use strict"
if (typeof Promise === "undefined") { ;(function () {
/** @constructor */
var Promise = function(executor) { var Promise = function(executor) {
if (!(this instanceof Promise)) throw new Error("Promise must be called with `new`") if (!(this instanceof Promise)) throw new Error("Promise must be called with `new`")
if (typeof executor !== "function") throw new TypeError("executor must be a function") if (typeof executor !== "function") throw new TypeError("executor must be a function")
@ -22,7 +23,7 @@ if (typeof Promise === "undefined") {
resolvers.length = 0, rejectors.length = 0 resolvers.length = 0, rejectors.length = 0
instance.state = shouldAbsorb instance.state = shouldAbsorb
instance.retry = function() {execute(value)} instance.retry = function() {execute(value)}
}, 0) })
} }
} }
catch (e) { catch (e) {
@ -73,7 +74,7 @@ if (typeof Promise === "undefined") {
var total = list.length, count = 0, values = [] var total = list.length, count = 0, values = []
if (list.length === 0) resolve([]) if (list.length === 0) resolve([])
else for (var i = 0; i < list.length; i++) { else for (var i = 0; i < list.length; i++) {
new function(i) { (function(i) {
function consume(value) { function consume(value) {
count++ count++
values[i] = value values[i] = value
@ -83,7 +84,7 @@ if (typeof Promise === "undefined") {
list[i].then(consume, reject) list[i].then(consume, reject)
} }
else consume(list[i]) else consume(list[i])
}(i) })(i)
} }
}) })
} }
@ -94,7 +95,6 @@ if (typeof Promise === "undefined") {
} }
}) })
} }
}
function Node(tag, key, attrs, children, text, dom) { function Node(tag, key, attrs, children, text, dom) {
return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: {}, events: undefined, instance: undefined} return {tag: tag, key: key, attrs: attrs, children: children, text: text, dom: dom, domSize: undefined, state: {}, events: undefined, instance: undefined}
} }
@ -170,6 +170,7 @@ function hyperscript(selector) {
return Node(selector, attrs && attrs.key, attrs || {}, Node.normalizeChildren(children), undefined, undefined) return Node(selector, attrs && attrs.key, attrs || {}, Node.normalizeChildren(children), undefined, undefined)
} }
var m = hyperscript var m = hyperscript
var renderService = function($window) { var renderService = function($window) {
var $doc = $window.document var $doc = $window.document
var $emptyFragment = $doc.createDocumentFragment() var $emptyFragment = $doc.createDocumentFragment()
@ -242,7 +243,7 @@ var renderService = function($window) {
vnode.dom = element vnode.dom = element
if (attrs != null) { if (attrs != null) {
setAttrs(vnode, attrs) setAttrs(vnode, attrs, ns)
} }
if (vnode.text != null) { if (vnode.text != null) {
@ -273,7 +274,7 @@ var renderService = function($window) {
//update //update
function updateNodes(parent, old, vnodes, hooks, nextSibling) { function updateNodes(parent, old, vnodes, hooks, nextSibling) {
if (old == null && vnodes == null) return if (old == null && vnodes == null) return
else if (old == null) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling) else if (old == null) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, undefined)
else if (vnodes == null) removeNodes(parent, old, 0, old.length, vnodes) else if (vnodes == null) removeNodes(parent, old, 0, old.length, vnodes)
else { else {
var recycling = isRecyclable(old, vnodes) var recycling = isRecyclable(old, vnodes)
@ -320,7 +321,7 @@ var renderService = function($window) {
nextSibling = movable.dom nextSibling = movable.dom
} }
else { else {
var dom = createNode(v, hooks) var dom = createNode(v, hooks, undefined)
insertNode(parent, dom, nextSibling) insertNode(parent, dom, nextSibling)
nextSibling = dom nextSibling = dom
} }
@ -329,7 +330,7 @@ var renderService = function($window) {
} }
if (end < start) break if (end < start) break
} }
createNodes(parent, vnodes, start, end + 1, hooks, nextSibling) createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, undefined)
removeNodes(parent, old, oldStart, oldEnd + 1, vnodes) removeNodes(parent, old, oldStart, oldEnd + 1, vnodes)
} }
} }
@ -354,7 +355,7 @@ var renderService = function($window) {
} }
else { else {
removeNode(parent, old, null, false) removeNode(parent, old, null, false)
insertNode(parent, createNode(vnode, hooks), nextSibling) insertNode(parent, createNode(vnode, hooks, undefined), nextSibling)
} }
} }
function updateText(old, vnode) { function updateText(old, vnode) {
@ -511,12 +512,12 @@ var renderService = function($window) {
} }
} }
//attrs //attrs
function setAttrs(vnode, attrs) { function setAttrs(vnode, attrs, ns) {
for (var key in attrs) { for (var key in attrs) {
setAttr(vnode, key, null, attrs[key]) setAttr(vnode, key, null, attrs[key], ns)
} }
} }
function setAttr(vnode, key, old, value) { function setAttr(vnode, key, old, value, ns) {
var element = vnode.dom var element = vnode.dom
if (key === "key" || (old === value && !isFormAttribute(vnode, key)) && typeof value !== "object" || typeof value === "undefined" || isLifecycleMethod(key)) return if (key === "key" || (old === value && !isFormAttribute(vnode, key)) && typeof value !== "object" || typeof value === "undefined" || isLifecycleMethod(key)) return
var nsLastIndex = key.indexOf(":") var nsLastIndex = key.indexOf(":")
@ -525,7 +526,7 @@ var renderService = function($window) {
} }
else if (key[0] === "o" && key[1] === "n" && typeof value === "function") updateEvent(vnode, key, value) else if (key[0] === "o" && key[1] === "n" && typeof value === "function") updateEvent(vnode, key, value)
else if (key === "style") updateStyle(element, old, value) else if (key === "style") updateStyle(element, old, value)
else if (key in element && !isAttribute(key) && vnode.ns === undefined) { else if (key in element && !isAttribute(key) && ns === undefined) {
//setting input[value] to same value by typing on focused element moves cursor to end in Chrome //setting input[value] to same value by typing on focused element moves cursor to end in Chrome
if (vnode.tag === "input" && key === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return if (vnode.tag === "input" && key === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return
element[key] = value element[key] = value
@ -541,14 +542,14 @@ var renderService = function($window) {
function setLateAttrs(vnode) { function setLateAttrs(vnode) {
var attrs = vnode.attrs var attrs = vnode.attrs
if (vnode.tag === "select" && attrs != null) { if (vnode.tag === "select" && attrs != null) {
if ("value" in attrs) setAttr(vnode, "value", null, attrs.value) if ("value" in attrs) setAttr(vnode, "value", null, attrs.value, undefined)
if ("selectedIndex" in attrs) setAttr(vnode, "selectedIndex", null, attrs.selectedIndex) if ("selectedIndex" in attrs) setAttr(vnode, "selectedIndex", null, attrs.selectedIndex, undefined)
} }
} }
function updateAttrs(vnode, old, attrs) { function updateAttrs(vnode, old, attrs) {
if (attrs != null) { if (attrs != null) {
for (var key in attrs) { for (var key in attrs) {
setAttr(vnode, key, old && old[key], attrs[key]) setAttr(vnode, key, old && old[key], attrs[key], undefined)
} }
} }
if (old != null) { if (old != null) {
@ -609,12 +610,12 @@ var renderService = function($window) {
} }
//lifecycle //lifecycle
function initLifecycle(source, vnode, hooks) { function initLifecycle(source, vnode, hooks) {
if (source.oninit != null) source.oninit.call(vnode.state, vnode) if (typeof source.oninit === "function") source.oninit.call(vnode.state, vnode)
if (source.oncreate != null) hooks.push(source.oncreate.bind(vnode.state, vnode)) if (typeof source.oncreate === "function") hooks.push(source.oncreate.bind(vnode.state, vnode))
} }
function updateLifecycle(source, vnode, hooks, recycling) { function updateLifecycle(source, vnode, hooks, recycling) {
if (recycling) initLifecycle(source, vnode, hooks) if (recycling) initLifecycle(source, vnode, hooks)
else if (source.onupdate != null) hooks.push(source.onupdate.bind(vnode.state, vnode)) else if (typeof source.onupdate === "function") hooks.push(source.onupdate.bind(vnode.state, vnode))
} }
function shouldUpdate(vnode, old) { function shouldUpdate(vnode, old) {
var forceVnodeUpdate, forceComponentUpdate var forceVnodeUpdate, forceComponentUpdate
@ -695,7 +696,7 @@ var requestService = function($window, Promise1) {
var callbackCount = 0 var callbackCount = 0
function xhr(args) { function xhr(args) {
return new Promise1(function(resolve, reject) { return new Promise1(function(resolve, reject) {
var useBody = args.useBody != null ? args.useBody : args.method !== "GET" && args.method !== "TRACE" var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
if (typeof args.serialize !== "function") args.serialize = JSON.stringify if (typeof args.serialize !== "function") args.serialize = JSON.stringify
if (typeof args.deserialize !== "function") args.deserialize = deserialize if (typeof args.deserialize !== "function") args.deserialize = deserialize
@ -706,7 +707,7 @@ var requestService = function($window, Promise1) {
else args.url = assemble(args.url, args.data) else args.url = assemble(args.url, args.data)
var xhr = new $window.XMLHttpRequest() var xhr = new $window.XMLHttpRequest()
xhr.open(args.method, args.url, args.async || true, args.user, args.password) xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
if (args.serialize === JSON.stringify && useBody) { if (args.serialize === JSON.stringify && useBody) {
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8") xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")
@ -822,7 +823,7 @@ var parseQueryString = function(string) {
if (key.indexOf("[") > -1) levels.pop() if (key.indexOf("[") > -1) levels.pop()
for (var j = 0; j < levels.length; j++) { for (var j = 0; j < levels.length; j++) {
var level = levels[j], nextLevel = levels[j + 1] var level = levels[j], nextLevel = levels[j + 1]
var isNumber = nextLevel == "" || !isNaN(parseInt(nextLevel)) var isNumber = nextLevel == "" || !isNaN(parseInt(nextLevel, 10))
var isValue = j === levels.length - 1 var isValue = j === levels.length - 1
if (level === "") { if (level === "") {
var key = levels.slice(0, j).join() var key = levels.slice(0, j).join()
@ -932,7 +933,7 @@ var coreRouter = function($window) {
vnode.dom.setAttribute("href", prefix + vnode.attrs.href) vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
vnode.dom.onclick = function(e) { vnode.dom.onclick = function(e) {
e.preventDefault() e.preventDefault()
setPath(vnode.attrs.href) setPath(vnode.attrs.href, undefined, undefined)
} }
} }
@ -1016,3 +1017,4 @@ m.withAttr = function(attrName, callback, context) {
m.render = renderService.render m.render = renderService.render
m.redraw = redrawService.publish m.redraw = redrawService.publish
module.exports = m module.exports = m
})()

View file

@ -7,6 +7,8 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"build": "node bundler/bundler", "build": "node bundler/bundler",
"minify": "uglifyjs mithril.js -m -c -o mithril.min.js",
"size": "uglifyjs mithril.js -m -c | gzipped",
"lint": "eslint .", "lint": "eslint .",
"test": "node ospec/bin/ospec", "test": "node ospec/bin/ospec",
"cover": "istanbul cover --print both ospec/bin/ospec" "cover": "istanbul cover --print both ospec/bin/ospec"

View file

@ -1,6 +1,5 @@
"use strict" "use strict"
/** @constructor */
if (typeof Promise === "undefined") {
var Promise = function(executor) { var Promise = function(executor) {
if (!(this instanceof Promise)) throw new Error("Promise must be called with `new`") if (!(this instanceof Promise)) throw new Error("Promise must be called with `new`")
if (typeof executor !== "function") throw new TypeError("executor must be a function") if (typeof executor !== "function") throw new TypeError("executor must be a function")
@ -23,7 +22,7 @@ if (typeof Promise === "undefined") {
resolvers.length = 0, rejectors.length = 0 resolvers.length = 0, rejectors.length = 0
instance.state = shouldAbsorb instance.state = shouldAbsorb
instance.retry = function() {execute(value)} instance.retry = function() {execute(value)}
}, 0) })
} }
} }
catch (e) { catch (e) {
@ -74,7 +73,7 @@ if (typeof Promise === "undefined") {
var total = list.length, count = 0, values = [] var total = list.length, count = 0, values = []
if (list.length === 0) resolve([]) if (list.length === 0) resolve([])
else for (var i = 0; i < list.length; i++) { else for (var i = 0; i < list.length; i++) {
new function(i) { (function(i) {
function consume(value) { function consume(value) {
count++ count++
values[i] = value values[i] = value
@ -84,7 +83,7 @@ if (typeof Promise === "undefined") {
list[i].then(consume, reject) list[i].then(consume, reject)
} }
else consume(list[i]) else consume(list[i])
}(i) })(i)
} }
}) })
} }
@ -95,5 +94,5 @@ if (typeof Promise === "undefined") {
} }
}) })
} }
}
module.exports = Promise module.exports = Promise

View file

@ -25,7 +25,7 @@ module.exports = function(string) {
if (key.indexOf("[") > -1) levels.pop() if (key.indexOf("[") > -1) levels.pop()
for (var j = 0; j < levels.length; j++) { for (var j = 0; j < levels.length; j++) {
var level = levels[j], nextLevel = levels[j + 1] var level = levels[j], nextLevel = levels[j + 1]
var isNumber = nextLevel == "" || !isNaN(parseInt(nextLevel)) var isNumber = nextLevel == "" || !isNaN(parseInt(nextLevel, 10))
var isValue = j === levels.length - 1 var isValue = j === levels.length - 1
if (level === "") { if (level === "") {
var key = levels.slice(0, j).join() var key = levels.slice(0, j).join()

View file

@ -75,7 +75,7 @@ module.exports = function($window) {
vnode.dom = element vnode.dom = element
if (attrs != null) { if (attrs != null) {
setAttrs(vnode, attrs) setAttrs(vnode, attrs, ns)
} }
if (vnode.text != null) { if (vnode.text != null) {
@ -107,7 +107,7 @@ module.exports = function($window) {
//update //update
function updateNodes(parent, old, vnodes, hooks, nextSibling) { function updateNodes(parent, old, vnodes, hooks, nextSibling) {
if (old == null && vnodes == null) return if (old == null && vnodes == null) return
else if (old == null) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling) else if (old == null) createNodes(parent, vnodes, 0, vnodes.length, hooks, nextSibling, undefined)
else if (vnodes == null) removeNodes(parent, old, 0, old.length, vnodes) else if (vnodes == null) removeNodes(parent, old, 0, old.length, vnodes)
else { else {
var recycling = isRecyclable(old, vnodes) var recycling = isRecyclable(old, vnodes)
@ -154,7 +154,7 @@ module.exports = function($window) {
nextSibling = movable.dom nextSibling = movable.dom
} }
else { else {
var dom = createNode(v, hooks) var dom = createNode(v, hooks, undefined)
insertNode(parent, dom, nextSibling) insertNode(parent, dom, nextSibling)
nextSibling = dom nextSibling = dom
} }
@ -163,7 +163,7 @@ module.exports = function($window) {
} }
if (end < start) break if (end < start) break
} }
createNodes(parent, vnodes, start, end + 1, hooks, nextSibling) createNodes(parent, vnodes, start, end + 1, hooks, nextSibling, undefined)
removeNodes(parent, old, oldStart, oldEnd + 1, vnodes) removeNodes(parent, old, oldStart, oldEnd + 1, vnodes)
} }
} }
@ -188,7 +188,7 @@ module.exports = function($window) {
} }
else { else {
removeNode(parent, old, null, false) removeNode(parent, old, null, false)
insertNode(parent, createNode(vnode, hooks), nextSibling) insertNode(parent, createNode(vnode, hooks, undefined), nextSibling)
} }
} }
function updateText(old, vnode) { function updateText(old, vnode) {
@ -348,12 +348,12 @@ module.exports = function($window) {
} }
//attrs //attrs
function setAttrs(vnode, attrs) { function setAttrs(vnode, attrs, ns) {
for (var key in attrs) { for (var key in attrs) {
setAttr(vnode, key, null, attrs[key]) setAttr(vnode, key, null, attrs[key], ns)
} }
} }
function setAttr(vnode, key, old, value) { function setAttr(vnode, key, old, value, ns) {
var element = vnode.dom var element = vnode.dom
if (key === "key" || (old === value && !isFormAttribute(vnode, key)) && typeof value !== "object" || typeof value === "undefined" || isLifecycleMethod(key)) return if (key === "key" || (old === value && !isFormAttribute(vnode, key)) && typeof value !== "object" || typeof value === "undefined" || isLifecycleMethod(key)) return
var nsLastIndex = key.indexOf(":") var nsLastIndex = key.indexOf(":")
@ -362,7 +362,7 @@ module.exports = function($window) {
} }
else if (key[0] === "o" && key[1] === "n" && typeof value === "function") updateEvent(vnode, key, value) else if (key[0] === "o" && key[1] === "n" && typeof value === "function") updateEvent(vnode, key, value)
else if (key === "style") updateStyle(element, old, value) else if (key === "style") updateStyle(element, old, value)
else if (key in element && !isAttribute(key) && vnode.ns === undefined) { else if (key in element && !isAttribute(key) && ns === undefined) {
//setting input[value] to same value by typing on focused element moves cursor to end in Chrome //setting input[value] to same value by typing on focused element moves cursor to end in Chrome
if (vnode.tag === "input" && key === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return if (vnode.tag === "input" && key === "value" && vnode.dom.value === value && vnode.dom === $doc.activeElement) return
element[key] = value element[key] = value
@ -378,14 +378,14 @@ module.exports = function($window) {
function setLateAttrs(vnode) { function setLateAttrs(vnode) {
var attrs = vnode.attrs var attrs = vnode.attrs
if (vnode.tag === "select" && attrs != null) { if (vnode.tag === "select" && attrs != null) {
if ("value" in attrs) setAttr(vnode, "value", null, attrs.value) if ("value" in attrs) setAttr(vnode, "value", null, attrs.value, undefined)
if ("selectedIndex" in attrs) setAttr(vnode, "selectedIndex", null, attrs.selectedIndex) if ("selectedIndex" in attrs) setAttr(vnode, "selectedIndex", null, attrs.selectedIndex, undefined)
} }
} }
function updateAttrs(vnode, old, attrs) { function updateAttrs(vnode, old, attrs) {
if (attrs != null) { if (attrs != null) {
for (var key in attrs) { for (var key in attrs) {
setAttr(vnode, key, old && old[key], attrs[key]) setAttr(vnode, key, old && old[key], attrs[key], undefined)
} }
} }
if (old != null) { if (old != null) {
@ -447,12 +447,12 @@ module.exports = function($window) {
//lifecycle //lifecycle
function initLifecycle(source, vnode, hooks) { function initLifecycle(source, vnode, hooks) {
if (source.oninit != null) source.oninit.call(vnode.state, vnode) if (typeof source.oninit === "function") source.oninit.call(vnode.state, vnode)
if (source.oncreate != null) hooks.push(source.oncreate.bind(vnode.state, vnode)) if (typeof source.oncreate === "function") hooks.push(source.oncreate.bind(vnode.state, vnode))
} }
function updateLifecycle(source, vnode, hooks, recycling) { function updateLifecycle(source, vnode, hooks, recycling) {
if (recycling) initLifecycle(source, vnode, hooks) if (recycling) initLifecycle(source, vnode, hooks)
else if (source.onupdate != null) hooks.push(source.onupdate.bind(vnode.state, vnode)) else if (typeof source.onupdate === "function") hooks.push(source.onupdate.bind(vnode.state, vnode))
} }
function shouldUpdate(vnode, old) { function shouldUpdate(vnode, old) {
var forceVnodeUpdate, forceComponentUpdate var forceVnodeUpdate, forceComponentUpdate

View file

@ -7,7 +7,7 @@ module.exports = function($window, Promise) {
function xhr(args) { function xhr(args) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var useBody = args.useBody != null ? args.useBody : args.method !== "GET" && args.method !== "TRACE" var useBody = typeof args.useBody === "boolean" ? args.useBody : args.method !== "GET" && args.method !== "TRACE"
if (typeof args.serialize !== "function") args.serialize = JSON.stringify if (typeof args.serialize !== "function") args.serialize = JSON.stringify
if (typeof args.deserialize !== "function") args.deserialize = deserialize if (typeof args.deserialize !== "function") args.deserialize = deserialize
@ -18,7 +18,7 @@ module.exports = function($window, Promise) {
else args.url = assemble(args.url, args.data) else args.url = assemble(args.url, args.data)
var xhr = new $window.XMLHttpRequest() var xhr = new $window.XMLHttpRequest()
xhr.open(args.method, args.url, args.async || true, args.user, args.password) xhr.open(args.method, args.url, typeof args.async === "boolean" ? args.async : true, typeof args.user === "string" ? args.user : undefined, typeof args.password === "string" ? args.password : undefined)
if (args.serialize === JSON.stringify && useBody) { if (args.serialize === JSON.stringify && useBody) {
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8") xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8")

View file

@ -100,7 +100,7 @@ module.exports = function($window) {
vnode.dom.setAttribute("href", prefix + vnode.attrs.href) vnode.dom.setAttribute("href", prefix + vnode.attrs.href)
vnode.dom.onclick = function(e) { vnode.dom.onclick = function(e) {
e.preventDefault() e.preventDefault()
setPath(vnode.attrs.href) setPath(vnode.attrs.href, undefined, undefined)
} }
} }