Skip to content

Commit

Permalink
feature: support extensions containing dots
Browse files Browse the repository at this point in the history
Also, allow specifying extensions without the initial dot.

Fixes #127
  • Loading branch information
BenoitZugmeyer committed Sep 5, 2020
1 parent 930d8fd commit 2f7e315
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 16 deletions.
51 changes: 51 additions & 0 deletions src/__tests__/getFileMode.js
@@ -0,0 +1,51 @@
"use strict"

const getFileMode = require("../getFileMode")

test("undefined if filename is empty", () => {
expect(
getFileMode({ htmlExtensions: [], xmlExtensions: [] }, undefined)
).toBe(undefined)
})

test("'html' if filename matches an HTML extension", () => {
expect(
getFileMode({ htmlExtensions: ["html"], xmlExtensions: [] }, "foo.html")
).toBe("html")
})

test("'html' if filename matches an HTML and an XML extension", () => {
expect(
getFileMode(
{ htmlExtensions: ["html"], xmlExtensions: ["html"] },
"foo.html"
)
).toBe("html")
})

test("'xml' if filename matches an XML extension", () => {
expect(
getFileMode({ htmlExtensions: ["html"], xmlExtensions: ["xml"] }, "foo.xml")
).toBe("xml")
})

test("undefined if filename ends with extension without dot", () => {
expect(
getFileMode({ htmlExtensions: [], xmlExtensions: ["xml"] }, "fooxml")
).toBe(undefined)
})

test("works with extensions starting with a dot", () => {
expect(
getFileMode({ htmlExtensions: [".html"], xmlExtensions: [] }, "foo.html")
).toBe("html")
})

test("works with extensions containgin a dot", () => {
expect(
getFileMode(
{ htmlExtensions: [".html.in"], xmlExtensions: [] },
"foo.html.in"
)
).toBe("html")
})
25 changes: 25 additions & 0 deletions src/getFileMode.js
@@ -0,0 +1,25 @@
"use strict"

module.exports = function getMode(pluginSettings, filenameOrOptions) {
const filename =
typeof filenameOrOptions === "object"
? filenameOrOptions.filename
: filenameOrOptions

if (!filename) {
return
}
if (pluginSettings.htmlExtensions.some(hasExtension)) {
return "html"
}
if (pluginSettings.xmlExtensions.some(hasExtension)) {
return "xml"
}

function hasExtension(extension) {
if (!extension.startsWith(".")) {
extension = `.${extension}`
}
return filename.endsWith(extension)
}
}
18 changes: 2 additions & 16 deletions src/index.js
Expand Up @@ -5,6 +5,7 @@ const extract = require("./extract")
const utils = require("./utils")
const splatSet = utils.splatSet
const getSettings = require("./settings").getSettings
const getFileMode = require("./getFileMode")

const PREPARE_RULE_NAME = "__eslint-plugin-html-prepare"
const LINTER_ISPATCHED_PROPERTY_NAME =
Expand Down Expand Up @@ -108,21 +109,6 @@ In the report, please include *all* those informations:
}
}

function getMode(pluginSettings, filenameOrOptions) {
const filename =
typeof filenameOrOptions === "object"
? filenameOrOptions.filename
: filenameOrOptions
const extension = path.extname(filename || "")

if (pluginSettings.htmlExtensions.indexOf(extension) >= 0) {
return "html"
}
if (pluginSettings.xmlExtensions.indexOf(extension) >= 0) {
return "xml"
}
}

function patch(Linter) {
const verifyMethodName = Linter.prototype._verifyWithoutProcessors
? "_verifyWithoutProcessors" // ESLint 6+
Expand All @@ -145,7 +131,7 @@ function patch(Linter) {
}

const pluginSettings = getSettings(config.settings || {})
const mode = getMode(pluginSettings, filenameOrOptions)
const mode = getFileMode(pluginSettings, filenameOrOptions)

if (!mode || typeof textOrSourceCode !== "string") {
return verify.call(
Expand Down

0 comments on commit 2f7e315

Please sign in to comment.