Skip to content

Commit 2bda86a

Browse files
committed
chore: should lint html files without an active editor present
1 parent 15c079e commit 2bda86a

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

src/extensions/default/HTMLCodeHints/html-lint.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,24 @@ define(function (require, exports, module) {
9292
configID,
9393
config: projectSpecificOptions
9494
}).then(lintResult =>{
95-
const editor = EditorManager.getCurrentFullEditor();
95+
let editor = EditorManager.getCurrentFullEditor();
9696
if(!editor || editor.document.file.fullPath !== fullPath) {
97-
reject(new Error("Lint failed as "+ ProjectManager.getProjectRelativeOrDisplayPath(fullPath)
98-
+ " is not active."));
99-
return;
97+
// this is not the active editor the lint is about. so the html validate api sends the startPos of
98+
// the error, but doesn't send the end pos, instead it sends the end offset. We need end line:ch pos
99+
//to highlight error properly, and computing that needs an editor(or we need to impl that logic)
100+
// so for now, we use the active editor if there is one to properly underline errors, and if we
101+
// cant find the active editor, we will do an approximation on the current line
102+
editor = null;
100103
}
101104
if (lintResult && lintResult.length) {
102105
lintResult = lintResult.map(function (lintError) {
103106
return {
104-
pos: editor.posFromIndex(lintError.start),
105-
endPos: editor.posFromIndex(lintError.end),
107+
pos: editor ? editor.posFromIndex(lintError.start) : lintError.startPos,
108+
endPos: editor ? editor.posFromIndex(lintError.end): {
109+
line: lintError.startPos.line,
110+
// highlightOffset can span multiple lines, so this is at best an approximation
111+
ch: lintError.startPos.ch + lintError.highlightOffset
112+
},
106113
message: `${lintError.message} (${lintError.ruleId})`,
107114
type: getTypeFromSeverity(lintError.severity),
108115
moreInfoURL: lintError.ruleUrl

src/extensions/default/HTMLCodeHints/worker/html-worker.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
if(result.messages && result.messages.length) {
6969
for(let message of result.messages){
7070
errors.push({
71+
// The starting position of the error (0-based line and column index)
72+
startPos: {line: (message.line || 1) - 1, ch: (message.column || 1) - 1},
73+
// The length (in characters) of the error from the starting position. this is an offset
74+
// than can span multiple lines.
75+
highlightOffset: (message.size || 1),
7176
start: message.offset,
7277
end: message.offset + (message.size || 1) - 1,
7378
severity: message.severity,

test/spec/Extn-HTMLCodeHints-Lint-integ-test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,5 +195,14 @@ define(function (require, exports, module) {
195195
await SpecRunnerUtils.deletePathAsync(testProjectsFolder + testFile, true, FileSystem);
196196
}, 5000);
197197
}
198+
199+
it(`should lint non active html files`, async function () {
200+
const htmlPath = path.join(testProjectsFolder, "simple1.html");
201+
const codeInspectionResults = await jsPromise(
202+
CodeInspection.inspectFile(FileSystem.getFileForPath(htmlPath)));
203+
expect(codeInspectionResults[0].result.errors.length).toBe(1);
204+
expect(codeInspectionResults[0].result.errors[0].pos).toEql({line: 1, ch: 1});
205+
expect(codeInspectionResults[0].result.errors[0].endPos).toEql({line: 1, ch: 5});
206+
}, 5000);
198207
});
199208
});

0 commit comments

Comments
 (0)