diff --git a/lib/services/html-parser.js b/lib/services/html-parser.js index b99249bd..cf0ef312 100644 --- a/lib/services/html-parser.js +++ b/lib/services/html-parser.js @@ -1,6 +1,7 @@ "use strict" const parse5 = require("parse5") +const ast = require("../ast/html-nodes") const NODENAME_TO_TYPE_MAP = { "#document": "HTMLDocument", @@ -9,8 +10,6 @@ const NODENAME_TO_TYPE_MAP = { "#documentType": "HTMLDocumentType", "#text": "HTMLText", } -const ast = require("../ast/html-nodes") - const HTMLDocument = ast.HTMLDocument const HTMLDocumentFragment = ast.HTMLDocumentFragment const HTMLDocumentType = ast.HTMLDocumentType @@ -136,7 +135,10 @@ const TOKEN_BUILDERS = { { parent: token } )) startTag.attributes = node.attrs.map((attr) => { - const attrLoc = location.startTag.attrs[attr.name] + const key = attr.prefix + ? `${attr.prefix}:${attr.name}` + : attr.name + const attrLoc = location.startTag.attrs[key] const attrToken = new HTMLAttribute( html, attrLoc.startOffset, @@ -144,7 +146,7 @@ const TOKEN_BUILDERS = { sourceCodeStore, { parent: startTag, - key: attr.name, + key, value: attr.value, } ) diff --git a/package.json b/package.json index 01cf9ce9..851c27f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-lodash-template", - "version": "0.18.0", + "version": "0.18.1", "description": "ESLint plugin for John Resig-style micro template, Lodash's template, Underscore's template and EJS.", "main": "lib/index.js", "scripts": { diff --git a/tests/lib/rules/attribute-name-casing.js b/tests/lib/rules/attribute-name-casing.js index 495478b9..e2a0bb4a 100644 --- a/tests/lib/rules/attribute-name-casing.js +++ b/tests/lib/rules/attribute-name-casing.js @@ -36,6 +36,10 @@ tester.run("attribute-name-casing", rule, { code: '
', options: [{ ignore: ["onClick"] }], }, + { + filename: "test.html", + code: '', + }, ], invalid: [ diff --git a/tests/lib/services/html-parser.js b/tests/lib/services/html-parser.js new file mode 100644 index 00000000..8f9996a6 --- /dev/null +++ b/tests/lib/services/html-parser.js @@ -0,0 +1,38 @@ +"use strict" + +const assert = require("assert") +const htmlParser = require("../../../lib/services/html-parser") +const SourceCodeStore = require("../../../lib/services/source-code-store") + +/** + * Parse html + * @param {string} code source code + */ +function parse(code) { + const store = new SourceCodeStore(code) + return htmlParser(code, store) +} + +describe("html-parser test", () => { + it("prefixed attributes", () => { + // https://github.com/ota-meshi/eslint-plugin-lodash-template/issues/136 + const ast = parse('') + assert.strictEqual(ast.type, "HTMLDocumentFragment") + assert.strictEqual(ast.children.length, 1) + assert.strictEqual(ast.children[0].type, "HTMLElement") + assert.strictEqual(ast.children[0].name, "svg") + assert.strictEqual(ast.children[0].startTag.attributes.length, 1) + assert.deepStrictEqual(ast.children[0].startTag.attributes[0].range, [ + 5, + 25, + ]) + assert.strictEqual( + ast.children[0].startTag.attributes[0].key, + "xml:space" + ) + assert.strictEqual( + ast.children[0].startTag.attributes[0].value, + "preserve" + ) + }) +})