Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: should lint html files without an active editor present #2042

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/extensions/default/HTMLCodeHints/html-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,24 @@ define(function (require, exports, module) {
configID,
config: projectSpecificOptions
}).then(lintResult =>{
const editor = EditorManager.getCurrentFullEditor();
let editor = EditorManager.getCurrentFullEditor();
if(!editor || editor.document.file.fullPath !== fullPath) {
reject(new Error("Lint failed as "+ ProjectManager.getProjectRelativeOrDisplayPath(fullPath)
+ " is not active."));
return;
// this is not the active editor the lint is about. so the html validate api sends the startPos of
// the error, but doesn't send the end pos, instead it sends the end offset. We need end line:ch pos
//to highlight error properly, and computing that needs an editor(or we need to impl that logic)
// so for now, we use the active editor if there is one to properly underline errors, and if we
// cant find the active editor, we will do an approximation on the current line
editor = null;
}
if (lintResult && lintResult.length) {
lintResult = lintResult.map(function (lintError) {
return {
pos: editor.posFromIndex(lintError.start),
endPos: editor.posFromIndex(lintError.end),
pos: editor ? editor.posFromIndex(lintError.start) : lintError.startPos,
endPos: editor ? editor.posFromIndex(lintError.end): {
line: lintError.startPos.line,
// highlightOffset can span multiple lines, so this is at best an approximation
ch: lintError.startPos.ch + lintError.highlightOffset
},
message: `${lintError.message} (${lintError.ruleId})`,
type: getTypeFromSeverity(lintError.severity),
moreInfoURL: lintError.ruleUrl
Expand Down
5 changes: 5 additions & 0 deletions src/extensions/default/HTMLCodeHints/worker/html-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
if(result.messages && result.messages.length) {
for(let message of result.messages){
errors.push({
// The starting position of the error (0-based line and column index)
startPos: {line: (message.line || 1) - 1, ch: (message.column || 1) - 1},
// The length (in characters) of the error from the starting position. this is an offset
// than can span multiple lines.
highlightOffset: (message.size || 1),
start: message.offset,
end: message.offset + (message.size || 1) - 1,
severity: message.severity,
Expand Down
9 changes: 9 additions & 0 deletions test/spec/Extn-HTMLCodeHints-Lint-integ-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,14 @@ define(function (require, exports, module) {
await SpecRunnerUtils.deletePathAsync(testProjectsFolder + testFile, true, FileSystem);
}, 5000);
}

it(`should lint non active html files`, async function () {
const htmlPath = path.join(testProjectsFolder, "simple1.html");
const codeInspectionResults = await jsPromise(
CodeInspection.inspectFile(FileSystem.getFileForPath(htmlPath)));
expect(codeInspectionResults[0].result.errors.length).toBe(1);
expect(codeInspectionResults[0].result.errors[0].pos).toEql({line: 1, ch: 1});
expect(codeInspectionResults[0].result.errors[0].endPos).toEql({line: 1, ch: 5});
}, 5000);
});
});
Loading