diff --git a/src/languageservice/services/yamlCompletion.ts b/src/languageservice/services/yamlCompletion.ts index 558a5a44..c0d09177 100644 --- a/src/languageservice/services/yamlCompletion.ts +++ b/src/languageservice/services/yamlCompletion.ts @@ -1067,10 +1067,17 @@ const isNumberExp = /^\d+$/; function convertToStringValue(value: string): string { if (value === 'true' || value === 'false' || value === 'null' || isNumberExp.test(value)) { return `"${value}"`; - } else { + } + + // eslint-disable-next-line prettier/prettier, no-useless-escape + if (value.indexOf('\"') !== -1) { value = value.replace(doubleQuotesEscapeRegExp, '"'); } + if (value.length > 0 && value.charAt(0) === '@') { + value = `"${value}"`; + } + return value; } // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/ban-types diff --git a/test/autoCompletion.test.ts b/test/autoCompletion.test.ts index d46c59bd..8137b797 100644 --- a/test/autoCompletion.test.ts +++ b/test/autoCompletion.test.ts @@ -706,6 +706,26 @@ describe('Auto Completion Tests', () => { }) .then(done, done); }); + + it('Autocompletion should escape key if needed', async () => { + languageService.addSchema(SCHEMA_ID, { + type: 'object', + properties: { + '@type': { + type: 'string', + enum: ['foo'], + }, + }, + }); + const content = ''; + const completion = await parseSetup(content, 0); + expect(completion.items.length).to.be.equal(1); + expect(completion.items[0]).to.deep.equal( + createExpectedCompletion('@type', '"@type": ${1:foo}', 0, 0, 0, 0, 10, 2, { + documentation: '', + }) + ); + }); }); describe('Array Specific Tests', function () {