Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function parseKeybindings(content: string): Keybinding[] {
const strippedContent = jsoncparser.stripComments(content);
const validationError = new ValidationError('Content validation error.');
// parse the raw content
const errors: jsoncparser.ParseError[] = [];
const rawBindings = jsoncparser.parse(strippedContent, errors);
if (errors.length) {
for (const error of errors) {
validationError.messages.push(JSON.stringify(error));
}
throw validationError;
}
// validate the parsed object
if (rawBindings) {
if (keymapsValidator(rawBindings)) {
return rawBindings;
return this.fileSystem.resolveContent(uri).then(({ stat, content }) => {
const strippedContent = jsoncparser.stripComments(content);
const errors: ParseError[] = [];
const preferences = jsoncparser.parse(strippedContent, errors);
if (errors.length) {
for (const error of errors) {
this.logger.error("JSON parsing error", error);
}
}
return preferences;
});
}).catch(reason => {
parse(content: string, errors?: string[]): Keybinding[] {
const strippedContent = parser.stripComments(content);
const parsingErrors: parser.ParseError[] | undefined = errors ? [] : undefined;
const bindings = parser.parse(strippedContent, parsingErrors);
if (parsingErrors && errors) {
for (const error of parsingErrors) {
errors.push(`${this.printParseErrorCode(error.error)} at ${error.offset} offset of ${error.length} length`);
}
}
if (this.validate(bindings)) {
return bindings;
}
if (errors && this.validate.errors) {
for (const error of this.validate.errors) {
errors.push(`${error.message} at ${error.dataPath}`);
}
}
return [];
function jsoncParse(text) {
return JSON.parse(jsoncParser.stripComments(text));
}
protected async readJsonFromFile(filePath: string): Promise {
if (await fs.pathExists(filePath)) {
const rawContent = await fs.readFile(filePath, 'utf-8');
const strippedContent = jsoncparser.stripComments(rawContent);
return jsoncparser.parse(strippedContent);
}
}