forked from arduino/arduino-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: jsonc parsing in the IDE2 backend
Occurred when `settings.json` contained comments or a trailing comma. Closes arduino#1945 Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
- Loading branch information
Showing
7 changed files
with
118 additions
and
57 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
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
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,53 @@ | ||
import { EnvVariablesServer } from '@theia/core/lib/common/env-variables'; | ||
import { FileUri } from '@theia/core/lib/node/file-uri'; | ||
import { inject, injectable } from '@theia/core/shared/inversify'; | ||
import { promises as fs } from 'fs'; | ||
import { | ||
parse as parseJsonc, | ||
ParseError, | ||
printParseErrorCode, | ||
} from 'jsonc-parser'; | ||
import { join } from 'path'; | ||
import { ErrnoException } from './utils/errors'; | ||
|
||
// Poor man's preferences on the backend. (https://github.com/arduino/arduino-ide/issues/1056#issuecomment-1153975064) | ||
@injectable() | ||
export class SettingsReader { | ||
@inject(EnvVariablesServer) | ||
private readonly envVariableServer: EnvVariablesServer; | ||
|
||
async read(): Promise<Record<string, unknown> | undefined> { | ||
const configDirUri = await this.envVariableServer.getConfigDirUri(); | ||
const configDirPath = FileUri.fsPath(configDirUri); | ||
const settingsPath = join(configDirPath, 'settings.json'); | ||
try { | ||
const raw = await fs.readFile(settingsPath, { encoding: 'utf8' }); | ||
return parse(raw); | ||
} catch (err) { | ||
if (ErrnoException.isENOENT(err)) { | ||
return undefined; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export function parse(raw: string): Record<string, unknown> | undefined { | ||
const errors: ParseError[] = []; | ||
const settings = | ||
parseJsonc(raw, errors, { | ||
allowEmptyContent: true, | ||
allowTrailingComma: true, | ||
disallowComments: false, | ||
}) ?? {}; | ||
if (errors.length) { | ||
console.error('Detected JSONC parser errors:'); | ||
console.error('----- CONTENT START -----'); | ||
console.error(raw); | ||
console.error('----- CONTENT END -----'); | ||
errors.forEach(({ error, offset }) => | ||
console.error(` - ${printParseErrorCode(error)} at ${offset}`) | ||
); | ||
return undefined; | ||
} | ||
return typeof settings === 'object' ? settings : undefined; | ||
} |
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
45 changes: 45 additions & 0 deletions
45
arduino-ide-extension/src/test/node/settings.reader.test.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,45 @@ | ||
import { expect } from 'chai'; | ||
import { parse } from '../../node/settings-reader'; | ||
|
||
describe('settings-reader', () => { | ||
describe('parse', () => { | ||
it('should handle comments', () => { | ||
const actual = parse(` | ||
{ | ||
"alma": "korte", | ||
// comment | ||
"szilva": false | ||
}`); | ||
expect(actual).to.be.deep.equal({ | ||
alma: 'korte', | ||
szilva: false, | ||
}); | ||
}); | ||
|
||
it('should handle trailing comma', () => { | ||
const actual = parse(` | ||
{ | ||
"alma": "korte", | ||
"szilva": 123, | ||
}`); | ||
expect(actual).to.be.deep.equal({ | ||
alma: 'korte', | ||
szilva: 123, | ||
}); | ||
}); | ||
|
||
it('should parse empty', () => { | ||
const actual = parse(''); | ||
expect(actual).to.be.deep.equal({}); | ||
}); | ||
|
||
it('should parse to undefined when parse has failed', () => { | ||
const actual = parse(` | ||
{ | ||
alma:: 'korte' | ||
trash | ||
}`); | ||
expect(actual).to.be.undefined; | ||
}); | ||
}); | ||
}); |
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