Skip to content

Commit

Permalink
feat: Lint html embedded script blocks (fix #100)
Browse files Browse the repository at this point in the history
Add an option, and a block extractor, to lint
embedded script (for any source with a scopeName beginning with
`text.html`)

Could be considered as experimental... Seems to works nice with
basic html and component files like [vuejs component files](https://vuejs.org/guide/application.html#Single-File-Components)
  • Loading branch information
nopnop committed Sep 6, 2016
1 parent e95cab8 commit 28f54f4
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {
checkStyleDevDependencies: {
type: 'boolean',
title: 'Check for standard',
description: 'Only run if standard, semistandard or happiness present in package.json `devDependencies` or `dependencies`',
description: 'Only run if standard, semistandard or happiness present in package.json `devDependencies`',
default: false
},
honorStyleSettings: {
Expand All @@ -30,6 +30,11 @@ module.exports = {
type: 'boolean',
description: 'Lint markdown fenced code blocks',
default: false
},
lintHtmlFiles: {
type: 'boolean',
description: 'Lint html-embedded script blocks',
default: false
}
},
cache: new Map(),
Expand All @@ -56,6 +61,16 @@ module.exports = {

// Check if this file is inside our grammar scope
var grammar = paneItem.getGrammar() || { scopeName: null }

// Check if this file is inside any kind of html scope (such as text.html.basic among others)
if (config.lintHtmlFiles && /^text.html/.test(grammar.scopeName) && self.scope.indexOf(grammar.scopeName) < 0) {
self.scope.push(grammar.scopeName)
}

if (!config.lintHtmlFiles && /^text.html/.test(grammar.scopeName)) {
return
}

if (self.scope.indexOf(grammar.scopeName) < 0 || (!config.lintMarkdownFiles && grammar.scopeName === 'source.gfm')) {
return
}
Expand Down
10 changes: 10 additions & 0 deletions lib/linter-js-standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var linter = require('./utils/linter')
var allowUnsafeNewFunction = require('loophole').allowUnsafeNewFunction
var markdownSplitter = require('./utils/markdown-splitter')
var htmlSplitter = require('./utils/html-splitter')
var Q = require('q')

function generateLintPromise (filePath, fileContent, settings, config, lineStart) {
Expand Down Expand Up @@ -35,6 +36,10 @@ module.exports = function () {
var settings = self.cache.get('text-editor')
var config = self.cache.get('config')

if (!config.lintHtmlFiles && /^text\.html/.test(fileScope)) {
return []
}

if (this.grammarScopes.indexOf(fileScope) < 0 || (!config.lintMarkdownFiles && fileScope === 'source.gfm')) {
return []
}
Expand All @@ -60,6 +65,11 @@ module.exports = function () {
fencedCodeBlocks.forEach(function (block) {
lintPromises.push(generateLintPromise(filePath, block.content, settings, config, block.line))
})
} else if (/^text\.html/.test(fileScope)) {
var scriptCodeBlocks = htmlSplitter(fileContent)
scriptCodeBlocks.forEach(function (block) {
lintPromises.push(generateLintPromise(filePath, block.content, settings, config, block.line))
})
} else {
lintPromises.push(generateLintPromise(filePath, fileContent, settings, config))
}
Expand Down
40 changes: 40 additions & 0 deletions lib/utils/html-splitter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = function (fileContent) {
var startScriptCodeRegex = /(<script[^>]*>)$/gm
var stopScriptCodeRegex = /(<\/script>)$/gm
var blocks = []
var offset = 0

var splitterRecursive = function () {
startScriptCodeRegex.lastIndex = offset

var start = startScriptCodeRegex.exec(fileContent)

if (start) {
var contentCutted = fileContent.substring(0, start.index)

stopScriptCodeRegex.lastIndex = contentCutted.length

var end = stopScriptCodeRegex.exec(fileContent)

if (!end) {
return false
}

var content = fileContent.substring(contentCutted.length + start[0].length, end.index).trimRight() + '\n'
offset = end.index

blocks.push({
line: contentCutted.split('\n').length - 1,
content: content
})

return true
}

return false
}

while (splitterRecursive()) {}

return blocks
}

0 comments on commit 28f54f4

Please sign in to comment.