Skip to content

Commit

Permalink
fix: default value with special chars with anyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
p-spacek committed May 2, 2024
1 parent b4642d2 commit 0e3ce42
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/languageservice/services/yamlCompletion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1292,7 +1292,7 @@ export class YamlCompletion {
case 'anyOf': {
let value = propertySchema.default || propertySchema.const;
if (value) {
if (type === 'string') {
if (type === 'string' || typeof value === 'string') {
value = convertToStringValue(value);
}
insertText += `${indent}${key}: \${${insertIndex++}:${value}}\n`;
Expand Down Expand Up @@ -1406,7 +1406,7 @@ export class YamlCompletion {
case 'string': {
let snippetValue = JSON.stringify(value);
snippetValue = snippetValue.substr(1, snippetValue.length - 2); // remove quotes
snippetValue = this.getInsertTextForPlainText(snippetValue); // escape \ and }
snippetValue = getInsertTextForPlainText(snippetValue); // escape \ and }
if (type === 'string') {
snippetValue = convertToStringValue(snippetValue);
}
Expand All @@ -1419,10 +1419,6 @@ export class YamlCompletion {
return this.getInsertTextForValue(value, separatorAfter, type);
}

private getInsertTextForPlainText(text: string): string {
return text.replace(/[\\$}]/g, '\\$&'); // escape $, \ and }
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
private getInsertTextForValue(value: any, separatorAfter: string, type: string | string[]): string {
if (value === null) {
Expand All @@ -1435,13 +1431,13 @@ export class YamlCompletion {
}
case 'number':
case 'boolean':
return this.getInsertTextForPlainText(value + separatorAfter);
return getInsertTextForPlainText(value + separatorAfter);
}
type = Array.isArray(type) ? type[0] : type;
if (type === 'string') {
value = convertToStringValue(value);
}
return this.getInsertTextForPlainText(value + separatorAfter);
return getInsertTextForPlainText(value + separatorAfter);
}

private getInsertTemplateForValue(
Expand All @@ -1466,14 +1462,14 @@ export class YamlCompletion {
if (typeof element === 'object') {
valueTemplate = `${this.getInsertTemplateForValue(element, indent + this.indentation, navOrder, separatorAfter)}`;
} else {
valueTemplate = ` \${${navOrder.index++}:${this.getInsertTextForPlainText(element + separatorAfter)}}\n`;
valueTemplate = ` \${${navOrder.index++}:${getInsertTextForPlainText(element + separatorAfter)}}\n`;
}
insertText += `${valueTemplate}`;
}
}
return insertText;
}
return this.getInsertTextForPlainText(value + separatorAfter);
return getInsertTextForPlainText(value + separatorAfter);
}

private addSchemaValueCompletions(
Expand Down Expand Up @@ -1860,6 +1856,13 @@ export class YamlCompletion {
}
}

/**
* escape $, \ and }
*/
function getInsertTextForPlainText(text: string): string {
return text.replace(/[\\$}]/g, '\\$&'); //
}

const isNumberExp = /^\d+$/;
function convertToStringValue(param: unknown): string {
let value: string;
Expand All @@ -1872,6 +1875,8 @@ function convertToStringValue(param: unknown): string {
return value;
}

value = getInsertTextForPlainText(value); // escape $, \ and }

if (value === 'true' || value === 'false' || value === 'null' || isNumberExp.test(value)) {
return `"${value}"`;
}
Expand Down
28 changes: 28 additions & 0 deletions test/autoCompletion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,34 @@ describe('Auto Completion Tests', () => {
);
});

it('Autocompletion should escape $ in defaultValue in anyOf', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
properties: {
car: {
type: 'object',
required: ['engine'],
properties: {
engine: {
anyOf: [
{
type: 'object',
},
{
type: 'string',
},
],
default: 'type$1234',
},
},
},
},
});
const content = '';
const completion = await parseSetup(content, 0);
expect(completion.items.map((i) => i.insertText)).to.deep.equal(['car:\n engine: ${1:type\\$1234}']);
});

it('Autocompletion should escape colon when indicating map', async () => {
schemaProvider.addSchema(SCHEMA_ID, {
type: 'object',
Expand Down

0 comments on commit 0e3ce42

Please sign in to comment.