|
| 1 | +/* |
| 2 | + * GNU AGPL-3.0 License |
| 3 | + * |
| 4 | + * Copyright (c) 2021 - present core.ai . All rights reserved. |
| 5 | + * Original work Copyright (c) 2012 - 2021 Adobe Systems Incorporated. All rights reserved. |
| 6 | + * |
| 7 | + * This program is free software: you can redistribute it and/or modify it |
| 8 | + * under the terms of the GNU Affero General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License |
| 15 | + * for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License |
| 18 | + * along with this program. If not, see https://opensource.org/licenses/AGPL-3.0. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +// Parts of this file is adapted from https://github.com/cfjedimaster/brackets-jshint |
| 23 | + |
| 24 | +/** |
| 25 | + * Provides JSLint results via the core linting extension point |
| 26 | + */ |
| 27 | +define(function (require, exports, module) { |
| 28 | + |
| 29 | + // Load dependent modules |
| 30 | + const CodeInspection = brackets.getModule("language/CodeInspection"), |
| 31 | + Strings = brackets.getModule("strings"), |
| 32 | + EditorManager = brackets.getModule("editor/EditorManager"), |
| 33 | + IndexingWorker = brackets.getModule("worker/IndexingWorker"); |
| 34 | + |
| 35 | + IndexingWorker.loadScriptInWorker(`${module.uri}/../worker/html-worker.js`); |
| 36 | + |
| 37 | + function getTypeFromSeverity(sev) { |
| 38 | + // https://html-validate.org/guide/api/getting-started.html |
| 39 | + switch (sev) { |
| 40 | + case 1: return CodeInspection.Type.WARNING; |
| 41 | + case 2: return CodeInspection.Type.ERROR; |
| 42 | + default: return CodeInspection.Type.META; |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Run JSLint on the current document. Reports results to the main UI. Displays |
| 48 | + * a gold star when no errors are found. |
| 49 | + */ |
| 50 | + async function lintOneFile(text, fullPath) { |
| 51 | + return new Promise((resolve, reject)=>{ |
| 52 | + IndexingWorker.execPeer("htmlLint", { |
| 53 | + text, |
| 54 | + filePath: fullPath |
| 55 | + }).then(lintResult =>{ |
| 56 | + const editor = EditorManager.getActiveEditor(); |
| 57 | + if(!editor || editor.document.file.fullPath !== fullPath) { |
| 58 | + reject(new Error("Lint failed as "+ Phoenix.app.getDisplayLocation(fullPath) + " is not active.")); |
| 59 | + return; |
| 60 | + } |
| 61 | + if (lintResult && lintResult.length) { |
| 62 | + lintResult = lintResult.map(function (lintError) { |
| 63 | + return { |
| 64 | + pos: editor.posFromIndex(lintError.start), |
| 65 | + endPos: editor.posFromIndex(lintError.end), |
| 66 | + message: `${lintError.message} (${lintError.ruleId})`, |
| 67 | + type: getTypeFromSeverity(lintError.severity), |
| 68 | + moreInfoURL: lintError.ruleUrl |
| 69 | + }; |
| 70 | + }); |
| 71 | + |
| 72 | + resolve({ errors: lintResult }); |
| 73 | + } |
| 74 | + resolve(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + CodeInspection.register("html", { |
| 80 | + name: Strings.HTML_LINT_NAME, |
| 81 | + scanFileAsync: lintOneFile, |
| 82 | + canInspect: function (_fullPath) { |
| 83 | + return true; // no restrictions for now |
| 84 | + } |
| 85 | + }); |
| 86 | +}); |
0 commit comments