eslint for javascript inside HTML (htmlmixed mode), closes #62

This commit is contained in:
azivner 2018-02-24 00:58:11 -05:00
parent 3b4509d833
commit 5dd93e4cdc

View File

@ -11,7 +11,23 @@
})(function(CodeMirror) { })(function(CodeMirror) {
"use strict"; "use strict";
async function validator(text, options) { async function validatorHtml(text, options) {
const result = /<script[^>]*>([\s\S]+)<\/script>/ig.exec(text);
if (result !== null) {
// preceding code is copied over but any (non-newline) character is replaced with space
// this will preserve line numbers etc.
const prefix = text.substr(0, result.index).replace(/./g, " ");
const js = prefix + result[1];
return await validatorJavaScript(js, options);
}
return [];
}
async function validatorJavaScript(text, options) {
await requireLibrary(ESLINT); await requireLibrary(ESLINT);
if (text.length > 20000) { if (text.length > 20000) {
@ -20,7 +36,7 @@
return []; return [];
} }
var errors = new eslint().verify(text, { const errors = new eslint().verify(text, {
root: true, root: true,
parserOptions: { parserOptions: {
ecmaVersion: 2017, ecmaVersion: 2017,
@ -32,36 +48,34 @@
}, },
rules: { rules: {
'import/no-unresolved': 'off', 'import/no-unresolved': 'off',
'import/no-extraneous-dependencies': 'off',
'func-names': 'off', 'func-names': 'off',
'no-multi-spaces': 'off',
'comma-dangle': ['error'], 'comma-dangle': ['error'],
'padded-blocks': 'off', 'padded-blocks': 'off',
'linebreak-style': 'off', 'linebreak-style': 'off',
'class-methods-use-this': 'off', 'class-methods-use-this': 'off',
'no-unused-vars': ['error', { vars: 'local', args: 'after-used' }], 'no-unused-vars': ['error', { vars: 'local', args: 'after-used' }],
'no-multiple-empty-lines': ['error', { max: 2, maxEOF: 1 }],
'no-nested-ternary': 'off', 'no-nested-ternary': 'off',
'no-underscore-dangle': ['error', {'allow': ['_super', '_lookupFactory']}], 'no-underscore-dangle': ['error', {'allow': ['_super', '_lookupFactory']}]
'object-shorthand': ['error', 'methods'],
} }
}); });
var result = []; const result = [];
if (errors) parseErrors(errors, result); if (errors) {
parseErrors(errors, result);
}
return result; return result;
} }
CodeMirror.registerHelper("lint", "javascript", validator); CodeMirror.registerHelper("lint", "javascript", validatorJavaScript);
// CodeMirror.registerHelper("lint", "htmlmixed", validator); CodeMirror.registerHelper("lint", "html", validatorHtml);
// CodeMirror.registerHelper("lint", "html", validator);
function parseErrors(errors, output) { function parseErrors(errors, output) {
for (const error of errors) { for (const error of errors) {
var startLine = error.line - 1; const startLine = error.line - 1;
var endLine = error.endLine !== undefined ? error.endLine - 1 : startLine; const endLine = error.endLine !== undefined ? error.endLine - 1 : startLine;
var startCol = error.column - 1; const startCol = error.column - 1;
var endCol = error.endColumn !== undefined ? error.endColumn - 1 : startCol + 1; const endCol = error.endColumn !== undefined ? error.endColumn - 1 : startCol + 1;
output.push({ output.push({
message: error.message, message: error.message,
@ -70,7 +84,5 @@
to: CodeMirror.Pos(endLine, endCol) to: CodeMirror.Pos(endLine, endCol)
}); });
} }
console.log(output);
} }
}); });