Skip to content

Commit f18c225

Browse files
committed
Code style and lint
1 parent f55b598 commit f18c225

File tree

5 files changed

+191
-170
lines changed

5 files changed

+191
-170
lines changed

.eslintrc.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module.exports = {
2+
env: {
3+
browser: true,
4+
commonjs: true,
5+
es6: true,
6+
node: true,
7+
},
8+
parserOptions: {
9+
ecmaVersion: 2017,
10+
},
11+
extends: 'eslint:recommended',
12+
rules: {
13+
'prefer-const': 'error',
14+
'no-var': 'error',
15+
indent: ['error', 2],
16+
'linebreak-style': ['error', 'unix'],
17+
quotes: ['error', 'single'],
18+
semi: ['error', 'never'],
19+
indent: 'off',
20+
},
21+
}

.eslintrc.json

Lines changed: 0 additions & 19 deletions
This file was deleted.

.prettierrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"useTabs": false,
3+
"printWidth": 80,
4+
"tabWidth": 2,
5+
"singleQuote": true,
6+
"trailingComma": "es5",
7+
"semi": false
8+
}

index.js

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,67 +3,70 @@
33
const m = require('mithril/hyperscript')
44
const Vnode = require('mithril/render/vnode')
55

6-
const VOID_TAGS = new RegExp('^(?:' +
7-
'area|' +
8-
'base|' +
9-
'br|' +
10-
'col|' +
11-
'command|' +
12-
'embed|' +
13-
'hr|' +
14-
'img|' +
15-
'input|' +
16-
'keygen|' +
17-
'link|' +
18-
'meta|' +
19-
'param|' +
20-
'source|' +
21-
'track|' +
22-
'wbr|' +
23-
'!doctype' +
24-
')$', 'i')
6+
const VOID_TAGS = new RegExp(
7+
'^(?:' +
8+
'area|' +
9+
'base|' +
10+
'br|' +
11+
'col|' +
12+
'command|' +
13+
'embed|' +
14+
'hr|' +
15+
'img|' +
16+
'input|' +
17+
'keygen|' +
18+
'link|' +
19+
'meta|' +
20+
'param|' +
21+
'source|' +
22+
'track|' +
23+
'wbr|' +
24+
'!doctype' +
25+
')$',
26+
'i'
27+
)
2528

2629
const hasOwn = {}.hasOwnProperty
2730

28-
function toStyleKey (str) {
31+
function toStyleKey(str) {
2932
return str
3033
.replace(/\W+/g, '-')
3134
.replace(/([a-z\d])([A-Z])/g, '$1-$2')
3235
.toLowerCase()
3336
}
3437

35-
function replaceHtml (m) {
38+
function replaceHtml(m) {
3639
if (m === '&') return '&'
3740
if (m === '<') return '&lt;'
3841
return '&gt;'
3942
}
4043

41-
function replaceAttribute (m) {
44+
function replaceAttribute(m) {
4245
if (m === '&') return '&amp;'
4346
if (m === '<') return '&lt;'
4447
if (m === '>') return '&gt;'
4548
return '&quot;'
4649
}
4750

4851
const defaults = {
49-
escapeText (s) {
52+
escapeText(s) {
5053
return s.replace(/[&<>]/g, replaceHtml)
5154
},
5255

53-
escapeAttribute (s) {
56+
escapeAttribute(s) {
5457
return s.replace(/[&<>"]/g, replaceAttribute)
55-
}
58+
},
5659
}
5760

58-
function bindOpt (options, key) {
61+
function bindOpt(options, key) {
5962
return options[key] ? options[key].bind(options) : defaults[key]
6063
}
6164

6265
// Using a generator so I can just yield promises that need awaited. Can't just
6366
// use `async`/`await` since this is used for both sync and async renders. At
6467
// least I have generators (read: coroutines), or I'd have to implement this
6568
// using a giant pushdown automaton. :-)
66-
function * tryRender (view, attrs, options, allowAwait) {
69+
function* tryRender(view, attrs, options, allowAwait) {
6770
// Fast-path a very simple case. Also lets me perform some renderer
6871
// optimizations later.
6972
if (view == null) return ''
@@ -81,14 +84,17 @@ function * tryRender (view, attrs, options, allowAwait) {
8184
const xml = !!options.xml
8285
const strict = xml || !!options.strict
8386

84-
function write (value) {
87+
function write(value) {
8588
result = '' + result + value
8689
}
8790

88-
function * setHooks (source, vnode) {
91+
function* setHooks(source, vnode) {
8992
const promises = []
9093
let waitFor
91-
if (allowAwait) waitFor = p => { promises.push(p) }
94+
if (allowAwait)
95+
waitFor = p => {
96+
promises.push(p)
97+
}
9298
if (source.oninit) {
9399
source.oninit.call(vnode.state, vnode, waitFor)
94100
}
@@ -98,7 +104,7 @@ function * tryRender (view, attrs, options, allowAwait) {
98104
if (promises.length) yield promises
99105
}
100106

101-
function createAttrString (view) {
107+
function createAttrString(view) {
102108
for (const key in view.attrs) {
103109
if (hasOwn.call(view.attrs, key)) {
104110
let value = view.attrs[key]
@@ -132,7 +138,7 @@ function * tryRender (view, attrs, options, allowAwait) {
132138
}
133139
}
134140

135-
function * renderComponent (vnode) {
141+
function* renderComponent(vnode) {
136142
if (typeof vnode.tag !== 'function') {
137143
vnode.state = Object.create(vnode.tag)
138144
} else if (vnode.tag.prototype && vnode.tag.prototype.view) {
@@ -141,13 +147,13 @@ function * tryRender (view, attrs, options, allowAwait) {
141147
vnode.state = vnode.tag(vnode)
142148
}
143149

144-
yield * setHooks(vnode.state, vnode)
145-
if (vnode.attrs != null) yield * setHooks(vnode.attrs, vnode)
150+
yield* setHooks(vnode.state, vnode)
151+
if (vnode.attrs != null) yield* setHooks(vnode.attrs, vnode)
146152
vnode.instance = Vnode.normalize(vnode.state.view(vnode))
147-
if (vnode.instance != null) yield * renderNode(vnode.instance)
153+
if (vnode.instance != null) yield* renderNode(vnode.instance)
148154
}
149155

150-
function * renderElement (vnode) {
156+
function* renderElement(vnode) {
151157
write(`<${vnode.tag}`)
152158
createAttrString(vnode)
153159
// Don't write children for void HTML elements
@@ -159,43 +165,51 @@ function * tryRender (view, attrs, options, allowAwait) {
159165
const text = '' + vnode.text
160166
if (text !== '') write(escapeText(text))
161167
} else {
162-
yield * renderChildren(vnode.children)
168+
yield* renderChildren(vnode.children)
163169
}
164170
write(`</${vnode.tag}>`)
165171
}
166172
}
167173

168-
function * renderChildren (vnodes) {
174+
function* renderChildren(vnodes) {
169175
for (const v of vnodes) {
170-
if (v != null) yield * renderNode(v)
176+
if (v != null) yield* renderNode(v)
171177
}
172178
}
173179

174-
function * renderNode (vnode) {
180+
function* renderNode(vnode) {
175181
if (vnode == null) return
176182
if (typeof vnode.tag === 'string') {
177183
vnode.state = {}
178-
if (vnode.attrs != null) yield * setHooks(vnode.attrs, vnode)
184+
if (vnode.attrs != null) yield* setHooks(vnode.attrs, vnode)
179185
switch (vnode.tag) {
180-
case '#': write(escapeText('' + vnode.children)); break
181-
case '<': write(vnode.children); break
182-
case '[': yield * renderChildren(vnode.children); break
183-
default: yield * renderElement(vnode)
186+
case '#':
187+
write(escapeText('' + vnode.children))
188+
break
189+
case '<':
190+
write(vnode.children)
191+
break
192+
case '[':
193+
yield* renderChildren(vnode.children)
194+
break
195+
default:
196+
yield* renderElement(vnode)
184197
}
185198
} else {
186-
yield * renderComponent(vnode)
199+
yield* renderComponent(vnode)
187200
}
188201
}
189202

190-
yield * renderNode(Vnode.normalize(view))
203+
yield* renderNode(Vnode.normalize(view))
191204
for (const hook of hooks) hook()
192205
return result.concat() // hint to flatten
193206
}
194207

195208
module.exports = async (view, attrs, options) => {
196209
const iter = tryRender(view, attrs, options, true)
210+
// eslint-disable-next-line no-constant-condition
197211
while (true) {
198-
const {done, value} = iter.next()
212+
const { done, value } = iter.next()
199213
if (done) return value
200214
await Promise.all(value)
201215
}

0 commit comments

Comments
 (0)