Skip to content

Commit

Permalink
Add quotation to objec key if it start with '@'
Browse files Browse the repository at this point in the history
Signed-off-by: Yevhen Vydolob <[email protected]>
  • Loading branch information
evidolob committed Apr 8, 2021
1 parent d1eca1f commit 4e11857
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down

0 comments on commit 4e11857

Please sign in to comment.