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 dfccc6f commit b64dd17
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 @@ -1115,7 +1115,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 @@ -1229,7 +1229,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 @@ -1242,10 +1242,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 @@ -1258,13 +1254,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 @@ -1289,14 +1285,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 @@ -1668,6 +1664,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 @@ -1680,6 +1683,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 @@ -931,6 +931,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 b64dd17

Please sign in to comment.