Skip to content

Commit

Permalink
feat: add ESLint integration
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed May 1, 2024
1 parent bf61f7a commit 1e2bf3c
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/eslint/LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023-present Johnson Chu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
117 changes: 117 additions & 0 deletions packages/eslint/index.ts
@@ -0,0 +1,117 @@
import { FileMap, LanguagePlugin, VirtualCode, createLanguage, forEachEmbeddedCode, isDiagnosticsEnabled } from '@volar/language-core';
import type { Linter } from 'eslint';
import { TextDocument } from 'vscode-languageserver-textdocument';

const windowsPath = /\\/g;

export function createProcessor(
languagePlugins: LanguagePlugin[],
caseSensitive: boolean,
extensionsMap: Record<string, string> = {
'javascript': '.js',
'typescript': '.ts',
'javascriptreact': '.jsx',
'typescriptreact': '.tsx',
'css': '.css',
'less': '.less',
'scss': '.scss',
'sass': '.sass',
'postcss': '.pcss',
'stylus': '.styl',
'html': '.html',
'pug': '.pug',
'json': '.json',
'jsonc': '.json',
'yaml': '.yaml',
'markdown': '.md',
},
supportsAutofix = true,
): Linter.Processor {
const language = createLanguage(languagePlugins, caseSensitive, () => { });
const documents = new FileMap<{
sourceDocument: TextDocument;
embeddedDocuments: TextDocument[];
codes: VirtualCode[];
}>(caseSensitive);
return {
supportsAutofix,
preprocess(text, filename) {
filename = filename.replace(windowsPath, '/');
const files: Linter.ProcessorFile[] = [];
const sourceScript = language.scripts.set(filename, {
getLength() {
return text.length;
},
getText(start, end) {
return text.substring(start, end);
},
getChangeRange() {
return undefined;
},
});
if (sourceScript?.generated) {
const codes = [];
const embeddedDocuments = [];
for (const code of forEachEmbeddedCode(sourceScript.generated.root)) {
if (code.mappings.some(mapping => isDiagnosticsEnabled(mapping.data))) {
const ext = extensionsMap[code.languageId];
if (!ext) {
continue;
}
files.push({
filename: filename + ext,
text: code.snapshot.getText(0, code.snapshot.getLength()),
});
codes.push(code);
embeddedDocuments.push(TextDocument.create(filename + ext, code.languageId, 0, code.snapshot.getText(0, code.snapshot.getLength())));
}
}
documents.set(filename, {
sourceDocument: TextDocument.create(filename, sourceScript.languageId, 0, text),
embeddedDocuments,
codes,
});
}
return files;
},
postprocess(messagesArr, filename) {
filename = filename.replace(windowsPath, '/');
const docs = documents.get(filename);
if (docs) {
const { codes, sourceDocument, embeddedDocuments } = docs;
for (let i = 0; i < messagesArr.length; i++) {
const code = codes[i];
const map = language.maps.get(code);
if (!map) {
messagesArr[i].length = 0;
continue;
}
const embeddedDocument = embeddedDocuments[i];
messagesArr[i] = messagesArr[i].filter(message => {
const start = embeddedDocument.offsetAt({ line: message.line - 1, character: message.column - 1 });
const end = embeddedDocument.offsetAt({ line: (message.endLine ?? message.line) - 1, character: (message.endColumn ?? message.column) - 1 });
for (const [sourceStart, mapping] of map.getSourceOffsets(start)) {
if (isDiagnosticsEnabled(mapping.data)) {
for (const [sourceEnd, mapping] of map.getSourceOffsets(end)) {
if (isDiagnosticsEnabled(mapping.data)) {
const sourcePosition = sourceDocument.positionAt(sourceStart);
const sourceEndPosition = sourceDocument.positionAt(sourceEnd);
message.line = sourcePosition.line + 1;
message.column = sourcePosition.character + 1;
message.endLine = sourceEndPosition.line + 1;
message.endColumn = sourceEndPosition.character + 1;
return true;
}
}
break;
}
}
return false;
});
}
return messagesArr.flat();
}
return [];
},
};
}
19 changes: 19 additions & 0 deletions packages/eslint/package.json
@@ -0,0 +1,19 @@
{
"name": "@volar/eslint",
"version": "2.2.0-alpha.12",
"license": "MIT",
"files": [
"**/*.js",
"**/*.d.ts"
],
"repository": {
"type": "git",
"url": "https://github.com/volarjs/volar.js.git",
"directory": "packages/eslint"
},
"dependencies": {
"@types/eslint": "^8.56.10",
"@volar/language-core": "2.2.0-alpha.12",
"vscode-languageserver-textdocument": "^1.0.11"
}
}
7 changes: 7 additions & 0 deletions packages/eslint/tsconfig.json
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"include": [ "*", "lib/**/*" ],
"references": [
{ "path": "../language-core/tsconfig.json" },
],
}
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tsconfig.json
Expand Up @@ -6,6 +6,7 @@
},
"include": [ "packages/*/tests" ],
"references": [
{ "path": "./packages/eslint/tsconfig.json" },
{ "path": "./packages/kit/tsconfig.json" },
{ "path": "./packages/typescript/tsconfig.json" },
{ "path": "./packages/vscode/tsconfig.json" },
Expand Down

0 comments on commit 1e2bf3c

Please sign in to comment.