-
-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(language-service): move compiler dom errors to separate plugin
- Loading branch information
1 parent
0b0dc46
commit 61b3418
Showing
4 changed files
with
84 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/language-service/lib/plugins/vue-compiler-dom-errors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { VueVirtualCode } from '@vue/language-core'; | ||
import type * as vscode from 'vscode-languageserver-protocol'; | ||
import type { TextDocument } from 'vscode-languageserver-textdocument'; | ||
import { URI } from 'vscode-uri'; | ||
import { LanguageServicePlugin } from '../types'; | ||
|
||
export function create(): LanguageServicePlugin { | ||
return { | ||
name: 'vue-compiler-dom-errors', | ||
capabilities: { | ||
diagnosticProvider: { | ||
interFileDependencies: false, | ||
workspaceDiagnostics: false, | ||
}, | ||
}, | ||
create(context) { | ||
return { | ||
provideDiagnostics(document) { | ||
|
||
if (!isSupportedDocument(document)) { | ||
return; | ||
} | ||
|
||
const uri = URI.parse(document.uri); | ||
const decoded = context.decodeEmbeddedDocumentUri(uri); | ||
const sourceScript = decoded && context.language.scripts.get(decoded[0]); | ||
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]); | ||
if (!virtualCode) { | ||
return; | ||
} | ||
|
||
const root = sourceScript?.generated?.root; | ||
if (!(root instanceof VueVirtualCode)) { | ||
return; | ||
} | ||
|
||
const templateErrors: vscode.Diagnostic[] = []; | ||
const { template } = root.sfc; | ||
|
||
if (template) { | ||
|
||
for (const error of template.errors) { | ||
onCompilerError(error, 1 satisfies typeof vscode.DiagnosticSeverity.Error); | ||
} | ||
|
||
for (const warning of template.warnings) { | ||
onCompilerError(warning, 2 satisfies typeof vscode.DiagnosticSeverity.Warning); | ||
} | ||
|
||
function onCompilerError(error: NonNullable<typeof template>['errors'][number], severity: vscode.DiagnosticSeverity) { | ||
|
||
const templateHtmlRange = { | ||
start: error.loc?.start.offset ?? 0, | ||
end: error.loc?.end.offset ?? 0, | ||
}; | ||
let errorMessage = error.message; | ||
|
||
templateErrors.push({ | ||
range: { | ||
start: document.positionAt(templateHtmlRange.start), | ||
end: document.positionAt(templateHtmlRange.end), | ||
}, | ||
severity, | ||
code: error.code, | ||
source: 'vue', | ||
message: errorMessage, | ||
}); | ||
} | ||
} | ||
|
||
return templateErrors; | ||
}, | ||
}; | ||
}, | ||
}; | ||
|
||
function isSupportedDocument(document: TextDocument) { | ||
return document.languageId === 'jade' || document.languageId === 'html'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters