Skip to content

Commit 7a3a3c9

Browse files
committed
Refactor to improve bundle size
1 parent e75a23b commit 7a3a3c9

File tree

2 files changed

+33
-48
lines changed

2 files changed

+33
-48
lines changed

index.js

Lines changed: 32 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
'use strict'
22

3-
var html = require('property-information/html')
4-
var svg = require('property-information/svg')
5-
var find = require('property-information/find')
6-
var ns = require('web-namespaces')
73
var s = require('hastscript/svg')
84
var h = require('hastscript')
5+
var find = require('property-information/find')
6+
var html = require('property-information/html')
7+
var svg = require('property-information/svg')
98
var vfileLocation = require('vfile-location')
9+
var ns = require('web-namespaces')
1010

1111
module.exports = wrapper
1212

@@ -45,7 +45,7 @@ function transform(ast, config) {
4545
var schema = config.schema
4646
var fn = own.call(map, ast.nodeName) ? map[ast.nodeName] : element
4747
var children
48-
var node
48+
var result
4949
var position
5050

5151
if (fn === element) {
@@ -56,20 +56,20 @@ function transform(ast, config) {
5656
children = nodes(ast.childNodes, config)
5757
}
5858

59-
node = fn(ast, children, config)
59+
result = fn(ast, children, config)
6060

6161
if (ast.sourceCodeLocation && config.file) {
62-
position = location(node, ast.sourceCodeLocation, config)
62+
position = location(result, ast.sourceCodeLocation, config)
6363

6464
if (position) {
6565
config.location = true
66-
node.position = position
66+
result.position = position
6767
}
6868
}
6969

7070
config.schema = schema
7171

72-
return node
72+
return result
7373
}
7474

7575
// Transform children.
@@ -87,7 +87,7 @@ function nodes(children, config) {
8787
// Transform a document.
8888
// Stores `ast.quirksMode` in `node.data.quirksMode`.
8989
function root(ast, children, config) {
90-
var node = {
90+
var result = {
9191
type: 'root',
9292
children: children,
9393
data: {quirksMode: ast.mode === 'quirks' || ast.mode === 'limited-quirks'}
@@ -98,13 +98,13 @@ function root(ast, children, config) {
9898
if (config.file && config.location) {
9999
doc = String(config.file)
100100
location = vfileLocation(doc)
101-
node.position = {
101+
result.position = {
102102
start: location.toPoint(0),
103103
end: location.toPoint(doc.length)
104104
}
105105
}
106106

107-
return node
107+
return result
108108
}
109109

110110
// Transform a doctype.
@@ -130,73 +130,58 @@ function comment(ast) {
130130
// Transform an element.
131131
function element(ast, children, config) {
132132
var fn = config.schema.space === 'svg' ? s : h
133-
var name = ast.tagName
134-
var attributes = ast.attrs
135-
var length = attributes.length
136133
var props = {}
137134
var index = -1
135+
var result
138136
var attribute
139-
var prop
140-
var node
141137
var pos
142138
var start
143139
var end
144140

145-
while (++index < length) {
146-
attribute = attributes[index]
147-
prop = (attribute.prefix ? attribute.prefix + ':' : '') + attribute.name
148-
props[prop] = attribute.value
141+
while (++index < ast.attrs.length) {
142+
attribute = ast.attrs[index]
143+
props[(attribute.prefix ? attribute.prefix + ':' : '') + attribute.name] =
144+
attribute.value
149145
}
150146

151-
node = fn(name, props, children)
147+
result = fn(ast.tagName, props, children)
152148

153-
if (name === 'template' && 'content' in ast) {
149+
if (result.tagName === 'template' && 'content' in ast) {
154150
pos = ast.sourceCodeLocation
155151
start = pos && pos.startTag && position(pos.startTag).end
156152
end = pos && pos.endTag && position(pos.endTag).start
157153

158-
node.content = transform(ast.content, config)
154+
result.content = transform(ast.content, config)
159155

160156
if ((start || end) && config.file) {
161-
node.content.position = {start: start, end: end}
157+
result.content.position = {start: start, end: end}
162158
}
163159
}
164160

165-
return node
161+
return result
166162
}
167163

168164
// Create clean positional information.
169165
function location(node, location, config) {
170-
var schema = config.schema
171-
var verbose = config.verbose
172-
var pos = position(location)
173-
var reference
174-
var attributes
175-
var attribute
166+
var result = position(location)
167+
var tail
168+
var key
176169
var props
177-
var prop
178170

179171
if (node.type === 'element') {
180-
reference = node.children[node.children.length - 1]
172+
tail = node.children[node.children.length - 1]
181173

182174
// Bug for unclosed with children.
183175
// See: <https://github.com/inikulin/parse5/issues/109>.
184-
if (
185-
!location.endTag &&
186-
reference &&
187-
reference.position &&
188-
reference.position.end
189-
) {
190-
pos.end = Object.assign({}, reference.position.end)
176+
if (!location.endTag && tail && tail.position && tail.position.end) {
177+
result.end = Object.assign({}, tail.position.end)
191178
}
192179

193-
if (verbose) {
194-
attributes = location.attrs
180+
if (config.verbose) {
195181
props = {}
196182

197-
for (attribute in attributes) {
198-
prop = find(schema, attribute).property
199-
props[prop] = position(attributes[attribute])
183+
for (key in location.attrs) {
184+
props[find(config.schema, key).property] = position(location.attrs[key])
200185
}
201186

202187
node.data = {
@@ -209,7 +194,7 @@ function location(node, location, config) {
209194
}
210195
}
211196

212-
return pos
197+
return result
213198
}
214199

215200
function position(loc) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"xo": "^0.34.0"
5454
},
5555
"scripts": {
56-
"format": "remark . -qfo && prettier --write . && xo --fix",
56+
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
5757
"build-bundle": "browserify index.js -s hastUtilFromParse5 > hast-util-from-parse5.js",
5858
"build-mangle": "browserify index.js -p tinyify -s hastUtilFromParse5 > hast-util-from-parse5.min.js",
5959
"build": "npm run build-bundle && npm run build-mangle",

0 commit comments

Comments
 (0)