Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quotation to object key if it start with '@' #435

Merged
merged 1 commit into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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