Skip to content

Commit

Permalink
Fix indent
Browse files Browse the repository at this point in the history
  • Loading branch information
izolate committed Sep 25, 2019
1 parent 01fb608 commit 4790da5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
35 changes: 18 additions & 17 deletions src/Parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
DefaultTreeCommentNode,
DefaultTreeDocument,
DefaultTreeDocumentType,
DefaultTreeElement,
DefaultTreeNode,
DefaultTreeTextNode,
Expand Down Expand Up @@ -73,36 +74,32 @@ class Parser {
return;
}

for (const treeNode of tree) {
const node = treeNode as DefaultTreeElement;

const pugNode = this.parseHtmlNode(node, indentLevel);
for (const treeNode of tree as DefaultTreeElement[]) {
const pugNode = this.parseHtmlNode(treeNode, indentLevel);
if (pugNode) {
this.pug += `\n${pugNode.toString()}`;
}

if (
Array.isArray(node.childNodes) &&
node.childNodes.length &&
!hasOnlyTextChildNode(node)
Array.isArray(treeNode.childNodes) &&
treeNode.childNodes.length &&
!hasOnlyTextChildNode(treeNode)
) {
yield* this.walk(node.childNodes, indentLevel + 1);
yield* this.walk(treeNode.childNodes, indentLevel + 1);
}
}
}

/**
* Creates a [PugNode] from a #documentType element.
*
* @param indentLevel
*/
private createDoctypeNode = (indentLevel: number): PugNode =>
new PugNode(
Nodes.Doctype,
'doctype html',
indentLevel,
this.tabs,
this.commas,
);
private createDoctypeNode = (
node: DefaultTreeDocumentType,
indentLevel: number,
): PugNode =>
new PugNode(Nodes.Doctype, node.name, indentLevel, this.tabs, this.commas);

/**
* Creates a [PugNode] from a #comment element.
Expand Down Expand Up @@ -142,6 +139,7 @@ class Parser {
* Converts an HTML element into a [PugNode].
*
* @param node
* @param indentLevel
*/
private createElementNode(
node: DefaultTreeElement,
Expand Down Expand Up @@ -178,7 +176,10 @@ class Parser {
): PugNode | void {
switch (node.nodeName) {
case Nodes.Doctype:
return this.createDoctypeNode(indentLevel);
return this.createDoctypeNode(
node as DefaultTreeDocumentType,
indentLevel,
);
case Nodes.Comment:
return this.createCommentNode(
node as DefaultTreeCommentNode,
Expand Down
12 changes: 7 additions & 5 deletions src/PugNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ class PugNode {
this.name = name;
this.value = value;
this.indentLevel = indentLevel;
if (tabs) { this.tabs = tabs; }
if (commas) { this.commas = commas; }
if (tabs) {
this.tabs = tabs;
}
if (commas) {
this.commas = commas;
}
}

/**
Expand Down Expand Up @@ -105,9 +109,7 @@ class PugNode {
const hasClassOrId = this.attributes.some((attr: Attribute) =>
['class', 'id'].includes(attr.name),
);
if (hasClassOrId) {
return '';
}
return hasClassOrId ? '' : this.name;
}
default:
return this.name;
Expand Down

0 comments on commit 4790da5

Please sign in to comment.