Skip to content

Commit 34906c1

Browse files
committed
style: after lint
1 parent 2be839e commit 34906c1

3 files changed

Lines changed: 265 additions & 275 deletions

File tree

src/index.js

Lines changed: 110 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,148 @@
11
import fs from 'fs';
22
import path from 'path';
3-
import util from 'util';
3+
import {format} from 'util';
44
import parseToPostHtml from 'posthtml-parser';
5-
import { match } from 'posthtml/lib/api';
5+
import {match} from 'posthtml/lib/api';
66

77
const errors = {
8-
'EXTENDS_NO_SRC': '<extends> has no "src"',
9-
'BLOCK_NO_NAME': '<block> has no "name"',
10-
'UNEXPECTED_BLOCK': 'Unexpected block "%s"'
8+
EXTENDS_NO_SRC: '<extends> has no "src"',
9+
BLOCK_NO_NAME: '<block> has no "name"',
10+
UNEXPECTED_BLOCK: 'Unexpected block "%s"'
1111
};
1212

13-
export default (options = {}) => {
14-
return tree => {
15-
options.encoding = options.encoding || 'utf8';
16-
options.root = options.root || './';
17-
options.plugins = options.plugins || [];
18-
options.strict = Object.prototype.hasOwnProperty.call(options, 'strict') ? !!options.strict : true;
19-
options.slotTagName = options.slotTagName || 'block';
20-
options.fillTagName = options.fillTagName || 'block';
21-
options.tagName = options.tagName || 'extends';
22-
23-
tree = handleExtendsNodes(tree, options, tree.messages);
24-
25-
const blockNodes = getBlockNodes(tree, options.slotTagName);
26-
for (let blockName of Object.keys(blockNodes)) {
27-
let blockNode = blockNodes[blockName];
28-
blockNode.tag = false;
29-
blockNode.content = blockNode.content || [];
30-
blockNodes[blockName] = blockNode;
31-
}
32-
33-
return tree;
34-
};
13+
const extend = (options = {}) => tree => {
14+
options.encoding = options.encoding || 'utf8';
15+
options.root = options.root || './';
16+
options.plugins = options.plugins || [];
17+
options.strict = Object.prototype.hasOwnProperty.call(options, 'strict') ? Boolean(options.strict) : true;
18+
options.slotTagName = options.slotTagName || 'block';
19+
options.fillTagName = options.fillTagName || 'block';
20+
options.tagName = options.tagName || 'extends';
21+
22+
tree = handleExtendsNodes(tree, options, tree.messages);
23+
24+
const blockNodes = getBlockNodes(options.slotTagName, tree);
25+
for (const blockName of Object.keys(blockNodes)) {
26+
const blockNode = blockNodes[blockName];
27+
blockNode.tag = false;
28+
blockNode.content = blockNode.content || [];
29+
blockNodes[blockName] = blockNode;
30+
}
31+
32+
return tree;
3533
};
3634

3735
function handleExtendsNodes(tree, options, messages) {
38-
match.call(tree = applyPluginsToTree(tree, options.plugins), {tag: options.tagName}, extendsNode => {
39-
if (! extendsNode.attrs || ! extendsNode.attrs.src) {
40-
throw getError(errors.EXTENDS_NO_SRC);
41-
}
42-
43-
const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
44-
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
45-
let layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
46-
47-
extendsNode.tag = false;
48-
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict, options.slotTagName, options.fillTagName);
49-
messages.push({
50-
type: 'dependency',
51-
file: layoutPath,
52-
from: options.from
53-
});
54-
55-
return extendsNode;
36+
match.call(tree = applyPluginsToTree(tree, options.plugins), {tag: options.tagName}, extendsNode => {
37+
if (!extendsNode.attrs || !extendsNode.attrs.src) {
38+
throw getError(errors.EXTENDS_NO_SRC);
39+
}
40+
41+
const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
42+
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
43+
const layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
44+
45+
extendsNode.tag = false;
46+
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict, options.slotTagName, options.fillTagName);
47+
messages.push({
48+
type: 'dependency',
49+
file: layoutPath,
50+
from: options.from
5651
});
5752

58-
return tree;
53+
return extendsNode;
54+
});
55+
56+
return tree;
5957
}
6058

6159
function applyPluginsToTree(tree, plugins) {
62-
return plugins.reduce((tree, plugin) => tree = plugin(tree), tree);
60+
return plugins.reduce((tree, plugin) => {
61+
tree = plugin(tree);
62+
return tree;
63+
}, tree);
6364
}
6465

65-
6666
function mergeExtendsAndLayout(layoutTree, extendsNode, strictNames, slotTagName, fillTagName) {
67-
const layoutBlockNodes = getBlockNodes(layoutTree, slotTagName);
68-
const extendsBlockNodes = getBlockNodes(extendsNode.content, fillTagName);
69-
70-
for (let layoutBlockName of Object.keys(layoutBlockNodes)) {
71-
let extendsBlockNode = extendsBlockNodes[layoutBlockName];
72-
if (!extendsBlockNode) {
73-
continue;
74-
}
75-
76-
let layoutBlockNode = layoutBlockNodes[layoutBlockName];
77-
layoutBlockNode.content = mergeContent(
78-
extendsBlockNode.content,
79-
layoutBlockNode.content,
80-
getBlockType(extendsBlockNode)
81-
);
82-
83-
delete extendsBlockNodes[layoutBlockName];
84-
}
67+
const layoutBlockNodes = getBlockNodes(slotTagName, layoutTree);
68+
const extendsBlockNodes = getBlockNodes(fillTagName, extendsNode.content);
8569

86-
if (strictNames) {
87-
for (let extendsBlockName of Object.keys(extendsBlockNodes)) {
88-
throw getError(errors.UNEXPECTED_BLOCK, extendsBlockName);
89-
}
70+
for (const layoutBlockName of Object.keys(layoutBlockNodes)) {
71+
const extendsBlockNode = extendsBlockNodes[layoutBlockName];
72+
if (!extendsBlockNode) {
73+
continue;
9074
}
9175

92-
return layoutTree;
93-
}
76+
const layoutBlockNode = layoutBlockNodes[layoutBlockName];
77+
layoutBlockNode.content = mergeContent(
78+
extendsBlockNode.content,
79+
layoutBlockNode.content,
80+
getBlockType(extendsBlockNode)
81+
);
9482

83+
delete extendsBlockNodes[layoutBlockName];
84+
}
9585

96-
function mergeContent(extendBlockContent, layoutBlockContent, extendBlockType) {
97-
extendBlockContent = extendBlockContent || [];
98-
layoutBlockContent = layoutBlockContent || [];
99-
100-
switch (extendBlockType) {
101-
case 'replace':
102-
layoutBlockContent = extendBlockContent;
103-
break;
104-
105-
case 'prepend':
106-
layoutBlockContent = extendBlockContent.concat(layoutBlockContent);
107-
break;
108-
109-
case 'append':
110-
layoutBlockContent = layoutBlockContent.concat(extendBlockContent);
111-
break;
86+
if (strictNames) {
87+
for (const extendsBlockName of Object.keys(extendsBlockNodes)) {
88+
throw getError(errors.UNEXPECTED_BLOCK, extendsBlockName);
11289
}
90+
}
11391

114-
return layoutBlockContent;
92+
return layoutTree;
11593
}
11694

95+
function mergeContent(extendBlockContent, layoutBlockContent, extendBlockType) {
96+
extendBlockContent = extendBlockContent || [];
97+
layoutBlockContent = layoutBlockContent || [];
98+
99+
switch (extendBlockType) {
100+
case 'replace':
101+
layoutBlockContent = extendBlockContent;
102+
break;
103+
104+
case 'prepend':
105+
layoutBlockContent = extendBlockContent.concat(layoutBlockContent);
106+
break;
107+
108+
case 'append':
109+
layoutBlockContent = layoutBlockContent.concat(extendBlockContent);
110+
break;
111+
default:
112+
break;
113+
}
114+
115+
return layoutBlockContent;
116+
}
117117

118118
function getBlockType(blockNode) {
119-
let blockType = (blockNode.attrs && blockNode.attrs.type) || '';
120-
blockType = blockType.toLowerCase();
121-
if (['replace', 'prepend', 'append'].indexOf(blockType) === -1) {
122-
blockType = 'replace';
123-
}
119+
let blockType = (blockNode.attrs && blockNode.attrs.type) || '';
120+
blockType = blockType.toLowerCase();
121+
if (!['replace', 'prepend', 'append'].includes(blockType)) {
122+
blockType = 'replace';
123+
}
124124

125-
return blockType;
125+
return blockType;
126126
}
127127

128+
function getBlockNodes(tag, content = []) {
129+
const blockNodes = {};
128130

129-
function getBlockNodes(content = [], tag) {
130-
let blockNodes = {};
131-
132-
match.call(content, {tag}, node => {
133-
if (! node.attrs || ! node.attrs.name) {
134-
throw getError(errors.BLOCK_NO_NAME);
135-
}
131+
match.call(content, {tag}, node => {
132+
if (!node.attrs || !node.attrs.name) {
133+
throw getError(errors.BLOCK_NO_NAME);
134+
}
136135

137-
blockNodes[node.attrs.name] = node;
138-
return node;
139-
});
136+
blockNodes[node.attrs.name] = node;
137+
return node;
138+
});
140139

141-
return blockNodes;
140+
return blockNodes;
142141
}
143142

144-
145-
function getError() {
146-
const message = util.format.apply(util, arguments);
147-
return new Error('[posthtml-extend] ' + message);
143+
function getError(...rest) {
144+
const message = format(...rest);
145+
return new Error('[posthtml-extend] ' + message);
148146
}
147+
148+
export default extend;

0 commit comments

Comments
 (0)