Skip to content

Commit

Permalink
Add test cases (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jul 21, 2018
1 parent f1bf15f commit 2f26cfd
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 28 deletions.
47 changes: 19 additions & 28 deletions lib/ast/html-nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,10 @@ class HTMLAttribute extends HTMLNode {
get eqToken() {
if (!this._eqToken && !this._nothingEq) {
const startIndex = this.range[0] + this.key.length
const endIndex = this.value ? this.range[1] : this._getMaxEndIndex()
const endIndex =
startIndex < this.range[1] && this.value
? this.range[1]
: this._getMaxEndIndex()
for (let i = startIndex; i < endIndex; i++) {
const c = this.html[i]
if (c === "=") {
Expand Down Expand Up @@ -582,35 +585,23 @@ class HTMLAttribute extends HTMLNode {
const text = this.sourceCodeStore.text
const maxEndIndex = this._getMaxEndIndex()

const startIndex = (() => {
for (let i = eqToken.range[1]; i < maxEndIndex; i++) {
const c = text[i]
if (
c !== " " &&
c !== "\t" &&
c !== "\r" &&
c !== "\n"
) {
return i
}
let startIndex = undefined
for (let i = eqToken.range[1]; i < maxEndIndex; i++) {
const c = text[i]
if (c.match(/[^ \t\r\n]/)) {
startIndex = i
break
}
return undefined
})()
}
if (startIndex !== undefined) {
const endIndex = (() => {
for (let i = maxEndIndex - 1; i >= startIndex; i--) {
const c = text[i]
if (
c !== " " &&
c !== "\t" &&
c !== "\r" &&
c !== "\n"
) {
return i
}
let endIndex = startIndex
for (let i = maxEndIndex - 1; i > startIndex; i--) {
const c = text[i]
if (c.match(/[^ \t\r\n]/)) {
endIndex = i
break
}
return undefined
})()
}
this._valueToken = new HTMLLiteral(
this.html,
startIndex,
Expand Down Expand Up @@ -762,7 +753,7 @@ class HTMLStartTag extends HTMLNode {
*/
const getAttrRange = attrIndex => {
const attr = attributes[attrIndex]
if (attr.value) {
if (attr.range[0] + attr.key.length < attr.range[1]) {
return attr.range
}
const nextStart =
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/rules/attribute-value-quote.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ tester.run("attribute-value-quote", rule, {
options: ["either"],
},
"<body><a download>download</a></body>",
"<body><div class=></div></body>",
"<body><div class= ></div></body>",
"<body><div class <%= '' %> ></div></body>",
// duplicate
"<body><div a = v a = ></div></body>",
],
invalid: [
{
Expand Down Expand Up @@ -145,5 +150,23 @@ tester.run("attribute-value-quote", rule, {
options: ["either"],
errors: ["Expected to be enclosed by quotes."],
},
{
filename: "test.html",
code: "<body><div class = v ></div></body>",
output: "<body><div class = \"v\" ></div></body>",
errors: ["Expected to be enclosed by double quotes."],
},
{
filename: "test.html",
code: "<body><div a b = = = = ></div></body>",
output: "<body><div a b = \"=\" = = ></div></body>",
errors: ["Expected to be enclosed by double quotes."],
},
{
filename: "test.html",
code: "<body><div = = = ></div></body>",
output: "<body><div = = \"=\" ></div></body>",
errors: ["Expected to be enclosed by double quotes."],
},
],
})
26 changes: 26 additions & 0 deletions tests/lib/rules/no-duplicate-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,5 +153,31 @@ tester.run("no-duplicate-attributes", rule, {
column: 21,
}],
},
{
filename: "test.html",
code:
`
<div
foo="a"
foo =
>
</div>
`,
errors: [{
message: "Duplicate attribute \"foo\".",
line: 3,
column: 17,
endLine: 3,
endColumn: 24,
},
{
message: "Duplicate attribute \"foo\".",
line: 4,
column: 17,
// The result changes depending on the version of parse5.
// endLine: 4,
// endColumn: 20,
}],
},
],
})
1 change: 1 addition & 0 deletions tests/lib/rules/no-space-attribute-equal-sign.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ tester.run("no-space-attribute-equal-sign", rule, {
"<div class=item>",
"<div class>",
"<div class=<%= aaa %>item>",
"<div dup=<%= aaa %>a dup=<%= bbb %>>",
],

invalid: [
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/rules/no-template-tag-in-start-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,9 @@ tester.run("no-template-tag-in-start-tag", rule, {
code: "<input <%- disabled ? 'disabled' : '' %> >",
errors: ["The template interpolate tag in start tag outside attribute values are forbidden."],
},
{
code: "<input disabled <%= 'class=' + c %> >",
errors: ["The template interpolate tag in start tag outside attribute values are forbidden."],
},
],
})
19 changes: 19 additions & 0 deletions tests/shared-container.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"use strict"

const assert = require("assert")
const container = require("../lib/shared-container")


describe("shared-container", () => {
it("empty", () => {
assert.strictEqual(container.popService("shared-container_test.html"), null)
assert.strictEqual(container.popService("shared-container_test.html"), null)
})
it("2 push", () => {
container.addService("shared-container_test.html", "1")
container.addService("shared-container_test.html", "2")
assert.strictEqual(container.popService("shared-container_test.html"), "2")
assert.strictEqual(container.popService("shared-container_test.html"), "1")
assert.strictEqual(container.popService("shared-container_test.html"), null)
})
})

0 comments on commit 2f26cfd

Please sign in to comment.