Skip to content

Commit

Permalink
Call validation directly so its synchronous
Browse files Browse the repository at this point in the history
We are calling the internals of the vscode library but its the only
way to keep it synchronous.

Fixes #20
  • Loading branch information
azeemba committed Nov 26, 2018
1 parent 82e0645 commit 06b3863
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.npmignore
.travis.yml
test
.vscode
105 changes: 87 additions & 18 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,113 @@
// Requirements
//------------------------------------------------------------------------------

var jsonService = require("vscode-json-languageservice")
var jsonService = require("vscode-json-languageservice");

var jsonServiceHandle = jsonService.getLanguageService({});
jsonServiceHandle.configure({
"validate": true,
"allowComments": false
})
});

//------------------------------------------------------------------------------
// Plugin Definition
//------------------------------------------------------------------------------

var ErrorCode = {
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
};

var fileContents = {};
var fileDocuments = {};

function toDiagnosticSeverity(severityLevel) {
switch (severityLevel) {
case "error": return 1;
case "warning": return 2;
case "ignore": return 0;
}
return 0;
}

function makeDiagnostic(c, message, severity, errorCode) {
return {
range: c,
message: message,
severity: severity,
code: errorCode
};
}

var getDiagnostics = function(textDocument, jsonDocument) {
var diagnostics = [];
var added = {};
var addProblem = function(problem) {
// remove duplicated messages
var signature = problem.range.start.line + " " + problem.range.start.character + " " + problem.message;
if (!added[signature]) {
added[signature] = true;
diagnostics.push(problem);
}
};
var trailingCommaSeverity = 1; // ERROR
var commentSeverity = 1; // ERROR

let fileContents = {};
let fileDocuments = {}
jsonDocument.syntaxErrors.forEach(function(p) {
if (p.code === ErrorCode.TrailingComma) {
if (typeof commentSeverity !== "number") {
return;
}
p.severity = trailingCommaSeverity;
}
addProblem(p);
});

if (typeof commentSeverity === "number") {
var message = "InvalidCommentToken: Comments are not permitted in JSON.";
jsonDocument.comments.forEach(function(c) {
addProblem(makeDiagnostic(c, message, commentSeverity, ErrorCode.CommentNotPermitted));
});
}
return diagnostics;
};

// import processors
module.exports.processors = {
// add your processors here
".json": {
preprocess: function(text, fileName) {
let textDocument = jsonService.TextDocument.create(fileName, "json", 1, text)
let parsed = jsonServiceHandle.parseJSONDocument(textDocument)
let responsePromise = jsonServiceHandle.doValidation(textDocument, parsed)
responsePromise.then(response => {
fileContents[fileName] = response;
fileDocuments[fileName] = textDocument
});
var textDocument = jsonService.TextDocument.create(fileName, "json", 1, text);
var parsed = jsonServiceHandle.parseJSONDocument(textDocument);
fileContents[fileName] = getDiagnostics(textDocument, parsed);
fileDocuments[fileName] = textDocument;
return [text];
},
postprocess: function(messages, fileName) {
let errors = fileContents[fileName]
var errors = fileContents[fileName];
if (errors === undefined) {
return []
return [];
}
let textDocument = fileDocuments[fileName]
var textDocument = fileDocuments[fileName];
delete fileContents[fileName];
delete fileDocuments[fileName]
return errors.map((error) => {
delete fileDocuments[fileName];
var formattedErrors = errors.map(function(error) {
return {
ruleId: error.code,
severity: (error.severity == 1) ? 2 : 1,
Expand All @@ -58,8 +126,9 @@ module.exports.processors = {
endLine: error.range.end.line + 1,
endColumn: error.range.end.character + 1,
source: textDocument.getText(error.range)
}
})
};
});
return formattedErrors;
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-json",
"version": "1.2.1",
"version": "1.3.1",
"description": "Lint JSON files",
"keywords": [
"eslint",
Expand Down

0 comments on commit 06b3863

Please sign in to comment.