Skip to content

Commit 4dae4df

Browse files
committed
HCK-14288: Escape comment only when it has special chars
1 parent c58c8c8 commit 4dae4df

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

forward_engineering/utils/general.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ const _ = require('lodash');
1111
const { AlterCollectionDto, AlterCollectionRoleDto } = require('../alterScript/types/AlterCollectionDto');
1212
const { ReservedWordsAsArray } = require('../enums/reservedWords');
1313

14-
const MUST_BE_ESCAPED = /[\t\n'\f\r]/gm;
14+
const MUST_BE_ESCAPED = /[\t\n'\\\f\r]/gm;
15+
const ESCAPE_MAP = {
16+
'\n': '\\n',
17+
'\t': '\\t',
18+
'\r': '\\r',
19+
'\f': '\\f',
20+
'\\': '\\\\',
21+
"'": "\\'",
22+
};
1523

1624
const getDbName = containerData => {
1725
return _.get(containerData, '[0].code') || _.get(containerData, '[0].name', '');
@@ -161,9 +169,12 @@ const getDbVersion = (dbVersion = '') => {
161169
return Number(_.get(version, [0], 0));
162170
};
163171

164-
const prepareComment = (comment = '') => comment.replace(MUST_BE_ESCAPED, character => `\\${character}`);
172+
const prepareComment = (comment = '') => comment.replaceAll(MUST_BE_ESCAPED, ch => ESCAPE_MAP[ch]);
165173

166-
const wrapComment = comment => `E'${prepareComment(JSON.stringify(comment)).slice(1, -1)}'`;
174+
const wrapComment = (comment = '') => {
175+
const shouldBeEscaped = MUST_BE_ESCAPED.test(comment);
176+
return shouldBeEscaped ? `E'${prepareComment(comment)}'` : comment;
177+
};
167178

168179
const getFunctionArguments = functionArguments => {
169180
return _.map(functionArguments, arg => {

0 commit comments

Comments
 (0)