Skip to content

Build your personal knowledge base with Trilium Notes

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

azivner5dd93e4cdc90
diff --git a/src/public/libraries/codemirror/addon/lint/eslint.js b/src/public/libraries/codemirror/addon/lint/eslint.js
index dce86d9fb1..a2f8daa1c4 100644
--- a/src/public/libraries/codemirror/addon/lint/eslint.js
+++ b/src/public/libraries/codemirror/addon/lint/eslint.js
@@ -9,68 +9,80 @@
else // Plain browser env
mod(CodeMirror);
})(function(CodeMirror) {
- "use strict";
+ "use strict";
- async function validator(text, options) {
- await requireLibrary(ESLINT);
+ async function validatorHtml(text, options) {
+ const result = /<script[^>]*>([\s\S]+)<\/script>/ig.exec(text);
- if (text.length > 20000) {
- console.log("Skipping linting because of large size: ", text.length);
+ 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 [];
}
- var errors = new eslint().verify(text, {
- root: true,
- parserOptions: {
- ecmaVersion: 2017,
- sourceType: 'module'
- },
- extends: ['eslint:recommended', 'airbnb-base'],
- env: {
- 'node': true
- },
- rules: {
- 'import/no-unresolved': 'off',
- 'import/no-extraneous-dependencies': 'off',
- 'func-names': 'off',
- 'no-multi-spaces': 'off',
- 'comma-dangle': ['error'],
- 'padded-blocks': 'off',
- 'linebreak-style': 'off',
- 'class-methods-use-this': 'off',
- 'no-unused-vars': ['error', { vars: 'local', args: 'after-used' }],
- 'no-multiple-empty-lines': ['error', { max: 2, maxEOF: 1 }],
- 'no-nested-ternary': 'off',
- 'no-underscore-dangle': ['error', {'allow': ['_super', '_lookupFactory']}],
- 'object-shorthand': ['error', 'methods'],
- }
- });
+ async function validatorJavaScript(text, options) {
+ await requireLibrary(ESLINT);
- var result = [];
- if (errors) parseErrors(errors, result);
- return result;
- }
+ if (text.length > 20000) {
+ console.log("Skipping linting because of large size: ", text.length);
- CodeMirror.registerHelper("lint", "javascript", validator);
- // CodeMirror.registerHelper("lint", "htmlmixed", validator);
- // CodeMirror.registerHelper("lint", "html", validator);
+ return [];
+ }
- function parseErrors(errors, output) {
- for (const error of errors) {
- var startLine = error.line - 1;
- var endLine = error.endLine !== undefined ? error.endLine - 1 : startLine;
- var startCol = error.column - 1;
- var endCol = error.endColumn !== undefined ? error.endColumn - 1 : startCol + 1;
+ const errors = new eslint().verify(text, {
+ root: true,
+ parserOptions: {
+ ecmaVersion: 2017,
+ sourceType: 'module'
+ },
+ extends: ['eslint:recommended', 'airbnb-base'],
+ env: {
+ 'node': true
+ },
+ rules: {
+ 'import/no-unresolved': 'off',
+ 'func-names': 'off',
+ 'comma-dangle': ['error'],
+ 'padded-blocks': 'off',
+ 'linebreak-style': 'off',
+ 'class-methods-use-this': 'off',
+ 'no-unused-vars': ['error', { vars: 'local', args: 'after-used' }],
+ 'no-nested-ternary': 'off',
+ 'no-underscore-dangle': ['error', {'allow': ['_super', '_lookupFactory']}]
+ }
+ });
- output.push({
- message: error.message,
- severity: error.severity === 1 ? "warning" : "error",
- from: CodeMirror.Pos(startLine, startCol),
- to: CodeMirror.Pos(endLine, endCol)
- });
+ const result = [];
+ if (errors) {
+ parseErrors(errors, result);
+ }
+
+ return result;
}
- console.log(output);
- }
+ CodeMirror.registerHelper("lint", "javascript", validatorJavaScript);
+ CodeMirror.registerHelper("lint", "html", validatorHtml);
+
+ function parseErrors(errors, output) {
+ for (const error of errors) {
+ const startLine = error.line - 1;
+ const endLine = error.endLine !== undefined ? error.endLine - 1 : startLine;
+ const startCol = error.column - 1;
+ const endCol = error.endColumn !== undefined ? error.endColumn - 1 : startCol + 1;
+
+ output.push({
+ message: error.message,
+ severity: error.severity === 1 ? "warning" : "error",
+ from: CodeMirror.Pos(startLine, startCol),
+ to: CodeMirror.Pos(endLine, endCol)
+ });
+ }
+ }
});