eslint-plugin-html
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "eslint-plugin-html", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "An ESLint plugin to extract and lint scripts from HTML files.", | ||
"license": "ISC", | ||
"repository": { | ||
@@ -6,0 +7,0 @@ "type": "git", |
@@ -19,5 +19,6 @@ "use strict"; | ||
function iterateScripts(code, onScript) { | ||
function iterateScripts(code, options, onScript) { | ||
var index = 0; | ||
var currentScript = null; | ||
var cdataSize = 0; | ||
@@ -32,3 +33,3 @@ var parser = new htmlparser.Parser({ | ||
if (attrs.type && !/^(application|text)\/(x-)?(javascript|babel)$/i.test(attrs.type)) { | ||
if (attrs.type && !/^(application|text)\/(x-)?(javascript|babel|ecmascript-6)$/i.test(attrs.type)) { | ||
return; | ||
@@ -40,2 +41,6 @@ } | ||
oncdatastart: function () { | ||
cdataSize += 12; // CDATA sections adds a 12 characters overhead (<![CDATA[]]>) | ||
}, | ||
onclosetag: function (name) { | ||
@@ -46,3 +51,3 @@ if (name !== "script" || currentScript === null) { | ||
onScript(code.slice(index, parser.startIndex - currentScript.length), currentScript); | ||
onScript(code.slice(index, parser.startIndex - currentScript.length - cdataSize), currentScript); | ||
@@ -61,2 +66,4 @@ index = parser.startIndex; | ||
}, { | ||
xmlMode: options.xmlMode === true, | ||
}); | ||
@@ -68,5 +75,7 @@ | ||
function extract(code, rawIndentDescriptor, reportBadIndentation) { | ||
function extract(code, options) { | ||
var indentDescriptor = parseIndentDescriptor(rawIndentDescriptor); | ||
var indentDescriptor = parseIndentDescriptor(options && options.indent); | ||
var reportBadIndentation = options && options.reportBadIndent; | ||
var xmlMode = options && options.xmlMode; | ||
var resultCode = ""; | ||
@@ -77,3 +86,5 @@ var map = []; | ||
iterateScripts(code, function (previousCode, scriptCode) { | ||
iterateScripts(code, { | ||
xmlMode: xmlMode, | ||
}, function (previousCode, scriptCode) { | ||
@@ -80,0 +91,0 @@ // Mark that we're inside a <script> a tag and push all new lines |
126
src/index.js
@@ -6,2 +6,18 @@ "use strict"; | ||
var htmlExtensions = [ | ||
".hbs", | ||
".handelbars", | ||
".htm", | ||
".html", | ||
".mustache", | ||
".php", | ||
".twig", | ||
".vue", | ||
]; | ||
var xmlExtensions = [ | ||
".xhtml", | ||
".xml", | ||
]; | ||
// Disclaimer: | ||
@@ -32,66 +48,78 @@ // | ||
var verify = eslint.verify; | ||
var reportBadIndent; | ||
function createProcessor(defaultXMLMode) { | ||
var verify = eslint.verify; | ||
var reportBadIndent; | ||
function patch() { | ||
eslint.verify = function (textOrSourceCode, config, filenameOrOptions, saveState) { | ||
var indentDescriptor = config.settings && config.settings["html/indent"]; | ||
reportBadIndent = config.settings && config.settings["html/report-bad-indent"]; | ||
currentInfos = extract(textOrSourceCode, indentDescriptor, Boolean(reportBadIndent)); | ||
return verify.call(this, currentInfos.code, config, filenameOrOptions, saveState); | ||
}; | ||
} | ||
var currentInfos; | ||
function unpatch() { | ||
eslint.verify = verify; | ||
} | ||
function patch() { | ||
eslint.verify = function (textOrSourceCode, config, filenameOrOptions, saveState) { | ||
var indentDescriptor = config.settings && config.settings["html/indent"]; | ||
var xmlMode = config.settings && config.settings["html/xml-mode"]; | ||
reportBadIndent = config.settings && config.settings["html/report-bad-indent"]; | ||
var currentInfos; | ||
var allowedExtensions = ["htm", "html", "xhtml", "vue", "hbs", "mustache", "php"]; | ||
if (typeof xmlMode !== "boolean") { | ||
xmlMode = defaultXMLMode; | ||
} | ||
var htmlProcessor = { | ||
currentInfos = extract(textOrSourceCode, { | ||
indent: indentDescriptor, | ||
reportBadIndent: Boolean(reportBadIndent), | ||
xmlMode: xmlMode, | ||
}); | ||
return verify.call(this, currentInfos.code, config, filenameOrOptions, saveState); | ||
}; | ||
} | ||
preprocess: function (content) { | ||
patch(); | ||
return [content]; | ||
}, | ||
function unpatch() { | ||
eslint.verify = verify; | ||
} | ||
return { | ||
postprocess: function (messages) { | ||
unpatch(); | ||
preprocess: function (content) { | ||
patch(); | ||
return [content]; | ||
}, | ||
messages[0].forEach(function (message) { | ||
message.column += currentInfos.map[message.line] || 0; | ||
}); | ||
postprocess: function (messages) { | ||
unpatch(); | ||
currentInfos.badIndentationLines.forEach(function (line) { | ||
messages[0].push({ | ||
message: "Bad line indentation.", | ||
line: line, | ||
column: 1, | ||
ruleId: "(html plugin)", | ||
severity: reportBadIndent === true ? 2 : reportBadIndent, | ||
messages[0].forEach(function (message) { | ||
message.column += currentInfos.map[message.line] || 0; | ||
}); | ||
}); | ||
messages[0].sort(function (ma, mb) { | ||
return ma.line - mb.line || ma.column - mb.column; | ||
}); | ||
currentInfos.badIndentationLines.forEach(function (line) { | ||
messages[0].push({ | ||
message: "Bad line indentation.", | ||
line: line, | ||
column: 1, | ||
ruleId: "(html plugin)", | ||
severity: reportBadIndent === true ? 2 : reportBadIndent, | ||
}); | ||
}); | ||
return messages[0]; | ||
}, | ||
messages[0].sort(function (ma, mb) { | ||
return ma.line - mb.line || ma.column - mb.column; | ||
}); | ||
}; | ||
return messages[0]; | ||
}, | ||
var getProcessors = function() { | ||
var processors = {}; | ||
}; | ||
allowedExtensions.forEach(function(ext) { | ||
processors["." + ext] = htmlProcessor; | ||
}); | ||
} | ||
return processors; | ||
}; | ||
var htmlProcessor = createProcessor(false); | ||
var xmlProcessor = createProcessor(true); | ||
module.exports = { | ||
processors: getProcessors(), | ||
}; | ||
var processors = {}; | ||
htmlExtensions.forEach(function(ext) { | ||
processors[ext] = htmlProcessor; | ||
}); | ||
xmlExtensions.forEach(function(ext) { | ||
processors[ext] = xmlProcessor; | ||
}); | ||
exports.processors = processors; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
0
218
8438
1
0