This document covers the JSON file processor that handles parsing, error detection, and message formatting for ESLint integration.
The JSON processor is the core component that integrates VSCode JSON language service with ESLint. It preprocesses JSON files to extract parsing errors and postprocesses ESLint messages to provide properly formatted output.
interface JsonProcessor {
preprocess(text: string, fileName: string): string[];
postprocess(messages: ESLintMessage[][], fileName: string): ESLintMessage[];
}Processor for legacy ESLint configurations.
const processor = plugin.processors['.json'];Processor for flat config ESLint configurations. Functionally identical to the legacy processor.
const processor = plugin.processors['json'];Parses JSON content and stores validation results for later processing.
function preprocess(text: string, fileName: string): string[];Parameters:
text (string): The raw JSON file contentfileName (string): The file path being processedReturns:
string[]: Array containing a single empty string (ESLint requirement)Behavior:
Processes ESLint messages and formats them with proper source information.
function postprocess(messages: ESLintMessage[][], fileName: string): ESLintMessage[];Parameters:
messages (ESLintMessage[][]): Nested array of ESLint messages from rulesfileName (string): The file path being processedReturns:
ESLintMessage[]: Formatted array of validation messagesBehavior:
The processor handles cases where multiple rules report the same error:
// Example: Both 'json/*' and 'json/trailing-comma' might report the same issue
// The processor keeps the more specific error message
const errorSignature = (err) =>
['message', 'line', 'column', 'endLine', 'endColumn'].map(field => err[field]).join('::');Deduplication Algorithm:
When duplicate errors are found:
json/*, json/json) have lower priorityjson/trailing-comma, etc.) have higher priorityEach processed message includes the actual source code snippet:
interface ProcessedMessage extends ESLintMessage {
source: string; // The actual JSON text that caused the error
}import json from 'eslint-plugin-json';
export default [
{
files: ["**/*.json"],
plugins: { json },
rules: {
'json/*': 'error'
},
processor: 'json/json' // Specifies which processor to use
}
];{
"plugins": ["json"],
"rules": {
"json/*": "error"
},
"overrides": [
{
"files": ["*.json"],
"processor": "json/.json"
}
]
}The processor maintains several internal caches during processing:
// Internal state (not exposed in public API)
interface ProcessorState {
fileLintResults: Record<string, DiagnosticResult[]>;
fileComments: Record<string, CommentRange[]>;
fileDocuments: Record<string, TextDocument>;
}
interface DiagnosticResult {
code: number;
message: string;
range: {
start: { line: number; character: number };
end: { line: number; character: number };
};
}
interface CommentRange {
start: { line: number; character: number };
end: { line: number; character: number };
}The processor maps VSCode JSON language service error codes to ESLint rule names:
const ErrorCodes = {
Undefined: 0,
EnumValueMismatch: 1,
UnexpectedEndOfComment: 0x101,
UnexpectedEndOfString: 0x102,
UnexpectedEndOfNumber: 0x103,
InvalidUnicode: 0x104,
InvalidEscapeCharacter: 0x105,
InvalidCharacter: 0x106,
PropertyExpected: 0x201,
CommaExpected: 0x202,
ColonExpected: 0x203,
ValueExpected: 0x204,
CommaOrCloseBacketExpected: 0x205,
CommaOrCloseBraceExpected: 0x206,
TrailingComma: 0x207,
DuplicateKey: 0x208,
CommentNotPermitted: 0x209,
SchemaResolveError: 0x300
};// 1. ESLint calls preprocess with JSON file content
const result = processor.preprocess('{"key": "value",}', 'example.json');
// Returns: ['']
// 2. ESLint runs rules against the empty string (rules access cached data)
// Rules report errors based on the parsed JSON data
// 3. ESLint calls postprocess with rule messages
const messages = processor.postprocess([[
{
ruleId: 'json/trailing-comma',
message: 'Trailing comma in object',
line: 1,
column: 16,
endLine: 1,
endColumn: 17
}
]], 'example.json');
// Returns formatted messages with source code and adjusted positionsThe processor leverages the VSCode JSON language service for parsing:
// Internal usage (not exposed in public API)
const jsonService = require('vscode-json-languageservice');
const jsonServiceHandle = jsonService.getLanguageService({});
const textDocument = jsonService.TextDocument.create(fileName, 'json', 1, text);
const parsed = jsonServiceHandle.parseJSONDocument(textDocument);
const diagnostics = parsed.syntaxErrors; // Array of parsing errors
const comments = parsed.comments; // Array of comment rangesThis integration provides:
Internal Dependencies:
vscode-json-languageservice: For JSON parsing and validationlodash/fp: For functional data transformations and processing