Skip to content

Commit

Permalink
fix tests and some small bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jescalan committed Aug 10, 2016
1 parent adb4bb1 commit 9602631
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 20 deletions.
Empty file removed .github/ISSUE_TEMPLATE.md
Empty file.
Empty file removed .github/PULL_REQUEST_TEMPLATE.md
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
.DS_Store
.nyc_output
coverage
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ test
contributing.md
.editorconfig
.travis.yml
coverage
.nyc_output
2 changes: 1 addition & 1 deletion lib/lexer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = function Lexer (input) {
content += char
next()
}
addToken('content', content)
addToken('text', content)
}
}

Expand Down
43 changes: 33 additions & 10 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ module.exports = (tokens) => {
if (typeof en !== 'undefined') { return en }

// if we haven't matched here, there must be an error
throw new TypeError(`Unrecognized token type: ${token.type}`)
throw new TypeError(`Unrecognized token type: ${token.type}\nToken: ${JSON.stringify(token)}`)
}

// run the parser
const root = []
let root = []
while (current < tokens.length) { root.push(walk(root)) }
root = removeNulls(root)
return root

/**
Expand Down Expand Up @@ -98,11 +99,19 @@ module.exports = (tokens) => {
// if we have a value, add it as the value for the previous key
if (token.type === 'attributeValue') {
const previousKey = tokens[current - 1].value
if (node.attrs[previousKey].length > 1) {
node.attrs[previousKey] += ' '
// if there are multiple classes being added, add a space between them
if (node.attrs[previousKey].length) {
node.attrs[previousKey].push({
type: 'text',
content: ' ',
location: { line: token.line, col: token.col }
})
}
// push class into the node's attributes array
node.attrs[previousKey].push({
type: ''
type: 'text',
content: token.value,
location: { line: token.line, col: token.col }
})
next()
}
Expand Down Expand Up @@ -149,7 +158,6 @@ module.exports = (tokens) => {
if (token.level > indentLevel) {
indentLevel = token.level
current++
ctx.push('')
return walk(ctx)
}

Expand All @@ -158,7 +166,7 @@ module.exports = (tokens) => {
if (token.level === indentLevel) {
current++
// TODO figure out better way to not push anything
return { type: 'text', content: '' }
return null
}

// if the indent level is less than before, we decrease the indent level
Expand All @@ -167,11 +175,11 @@ module.exports = (tokens) => {
indentLevel = token.level
current++
// TODO figure out better way to not push anything
return { type: 'text', content: '' }
return null
}
} else {
// TODO figure out better way to not push anything
return { type: 'text', content: '' }
return null
}
}
}
Expand All @@ -184,7 +192,7 @@ module.exports = (tokens) => {
if (typeof token === 'undefined') {
indentLevel = 0
// TODO figure out better way to not push anything
return { type: 'text', content: '' }
return null
}
}

Expand All @@ -194,4 +202,19 @@ module.exports = (tokens) => {
function next () {
token = tokens[++current]
}

/**
* Remove all nulls from the tree.
* TODO handle this more elegantly
*/
function removeNulls (tree) {
return tree.reduce((m, node) => {
if (node && node.type === 'tag' && node.content) {
node.content = removeNulls(node.content)
}

if (node) m.push(node)
return m
}, [])
}
}
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
},
"bugs": "https://github.com/reshape/sugarml/issues",
"devDependencies": {
"ava": "0.15.x",
"coveralls": "2.x",
"nyc": "6.x",
"ava": "^0.16.0",
"coveralls": "^2.11.12",
"nyc": "^7.1.0",
"reshape": "reshape/reshape",
"snazzy": "^4.0.1",
"standard": "7.x"
},
"engines": {
Expand All @@ -22,9 +23,9 @@
"main": "lib",
"repository": "reshape/sugarml",
"scripts": {
"test": "ava",
"lint": "standard",
"coverage": "nyc ava",
"coveralls": "nyc --reporter=lcov ava && cat ./coverage/lcov.info | coveralls"
"test": "npm run lint && nyc ava",
"lint": "standard | snazzy",
"coverage": "npm test && nyc report --reporter=html && open ./coverage/index.html",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
}
}
2 changes: 1 addition & 1 deletion test/fixtures/expected/attributes.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div id="test" @wow="" data-foo="bar" :foo="" v-bind:click="method"></div>
<div id="test" @wow data-foo="bar" :foo v-bind:click="method"></div>
2 changes: 1 addition & 1 deletion test/fixtures/expected/simple.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><div class="bar" id="test" @wow="" data-foo="bar"><p id="foo">hello world!</p><div class="wow"><div class="nest2"><div class="nest3"><p>more text</p></div></div></div><div class="unnest"></div><!-- a comment --><li><a href="wow">content</a></li><p>text on its own line<span>now it's a span!</span>ah, more text on its own line...</p></div>
<!DOCTYPE html><div class="bar" id="test" @wow data-foo="bar"><p id="foo">hello world!</p><div class="wow"><div class="nest2"><div class="nest3"><p>more text</p></div></div></div><div class="unnest"></div><!-- a comment --><li><a href="wow">content</a></li><p>text on its own line<span>now it's a span!</span>ah, more text on its own line...</p></div>

0 comments on commit 9602631

Please sign in to comment.