Skip to content

Commit

Permalink
入れ子オブジェクトのデフォルト値の処理を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
elleonard committed Oct 13, 2023
1 parent 05ab915 commit 04bc1a1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 15 deletions.
33 changes: 23 additions & 10 deletions scripts/generateFromConfig/generateHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ class TypedParameter {
* prettierでエスケープが最適化されてしまうので、 singleQuote オプションを切るのが良いだろう
*
* @param {string} language 言語
* @param {boolean} withoutExtraQuotes トップコメント用のクオートを外す(パラメータパースコード用)
* @param {boolean} forParameterParse トップコメント用のクオートを外す(パラメータパースコード用)
* @return {string}
*/
defaultText(language, withoutExtraQuotes) {
defaultText(language, forParameterParse) {
const defaultValue = this.defaultValue(language);
if (defaultValue || typeof defaultValue === 'boolean' || Number.isFinite(defaultValue)) {
if (Array.isArray(defaultValue)) {
Expand All @@ -111,11 +111,11 @@ class TypedParameter {
const objectKeyValue = this.escapeDoubleQuote(
Object.entries(defaultValue)
.map(([key, value]) => {
return this.defaultObjectKeyValueToText(key, value, withoutExtraQuotes);
return this.defaultObjectKeyValueToText(key, value, forParameterParse);
})
.join(',')
);
return withoutExtraQuotes
return forParameterParse
? `{${objectKeyValue}}`
: `"{${objectKeyValue}}"`;
})
Expand All @@ -127,7 +127,7 @@ class TypedParameter {
if (typeof defaultValue === 'object') {
return `{${Object.entries(defaultValue)
.map(([key, value]) => {
return this.defaultObjectKeyValueToText(key, value, withoutExtraQuotes);
return this.defaultObjectKeyValueToText(key, value, forParameterParse);
})
.join(', ')}}`;
}
Expand All @@ -136,24 +136,26 @@ class TypedParameter {
return '';
}

defaultObjectKeyValueToText(key, value, withoutExtraQuotes) {
defaultObjectKeyValueToText(key, value, forParameterParse) {
if (Array.isArray(value)) {
if (value.length === 0) {
return `"${key}":${withoutExtraQuotes ? '[]' : '"[]"'}`;
return `"${key}":${forParameterParse ? '[]' : '"[]"'}`;
}
if (typeof value[0] === 'object') {
const objectKeyValue = Object.entries(value)
.map(([k, v]) => {
return this.defaultObjectKeyValueToText(k, v);
})
.join(', ');
return `"${key}":${withoutExtraQuotes ? `{${objectKeyValue}}` : `"{${objectKeyValue}}"`}`;
return `"${key}":${forParameterParse ? `{${objectKeyValue}}` : `"{${objectKeyValue}}"`}`;
}
/**
* オブジェクト内配列は、更にダブルクオートを1回エスケープする必要がある
*/
return `"${key}":${
withoutExtraQuotes ? `[${value.map((v) => `"${v}"`).join(',')}]` : `"[${this.escapeDoubleQuote(value.map((v) => `"${v}"`).join(','))}]"`
forParameterParse
? `[${value.map((v) => `"${v}"`).join(',')}]`
: `"[${this.escapeDoubleQuote(value.map((v) => `"${v}"`).join(','))}]"`
}`;
}
if (value && typeof value === 'object') {
Expand All @@ -164,7 +166,10 @@ class TypedParameter {
})
.join(', ')
);
return `"${key}":${withoutExtraQuotes ? `{${objectKeyValue}}` : `"{${objectKeyValue}}"`}`;
return `"${key}":${forParameterParse
? `"{${this.escapeDoubleQuoteForParser(objectKeyValue)}}"`
: `"{${objectKeyValue}}"`
}`;
}
return `"${key}":"${value}"`;
}
Expand All @@ -177,6 +182,14 @@ class TypedParameter {
escapeDoubleQuote(string) {
return string.replace(/"/g, '\\"');
}

/**
* パーサがデフォルト値として渡す入れ子オブジェクトについては
* バックスラッシュを2つ追加する
*/
escapeDoubleQuoteForParser(string) {
return string.replace(/"/g, '\\\\"');
}
}

class PluginParameter extends TypedParameter {
Expand Down
11 changes: 9 additions & 2 deletions scripts/generateFromConfig/generateParameterReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function generateParameterReader(config, destDir) {
};
`;

return prettier.format(code, options);
return escapeBackslashQuote(prettier.format(code, options));
});
}

Expand All @@ -40,7 +40,7 @@ function generateParameterReaderFunction(config, destDir) {
})("${config.name}");
`;

return prettier.format(code, options);
return escapeBackslashQuote(prettier.format(code, options));
});
}

Expand All @@ -57,6 +57,13 @@ function configToParameters(config, symbolType) {
: [];
}

/**
* prettierが勝手にバックスラッシュを消してしまうため、苦肉の策として
*/
function escapeBackslashQuote(string) {
return string.replace(/\\"/g, '\\\\"');
}

module.exports = {
generateParameterReader,
generateParameterReaderFunction
Expand Down
2 changes: 0 additions & 2 deletions scripts/generateFromConfig/generateParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ function arrayParser(config, parameter, symbolType) {
}
const default_ = parameter.default
? parameterObject.defaultText('ja', true)
.replace(/\\/g, '')
.replace(/\n/g, '\\n')
: '[]';
return `JSON.parse(${parameterSymbol(parameter, symbolType)} || '${default_}').map(${subParameter.symbol} => {
Expand All @@ -117,7 +116,6 @@ function structParser(config, parameter, symbolType) {
const parameterObject = new PluginParameter(parameter);
const default_ = parameter.default
? parameterObject.defaultText('ja', true)
.replace(/\\/g, '')
.replace(/\n/g, '\\n')
: '{}';
return `((parameter) => {
Expand Down
9 changes: 8 additions & 1 deletion scripts/generateFromConfig/generatePluginCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function generatePluginCommand(config) {
.map(command => `export const command_${commandNameToSymbol(command.name)} = "${command.name}";`)
)
.join('\n\n');
return prettier.format(code, options);
return escapeBackslashQuote(prettier.format(code, options));
});
}

Expand Down Expand Up @@ -52,6 +52,13 @@ function commandNameToSymbol(commandName) {
return commandName.replace(/ ([a-z])/g, (m) => m.toUpperCase().trim());
}

/**
* prettierが勝手にバックスラッシュを消してしまうため、苦肉の策として
*/
function escapeBackslashQuote(string) {
return string.replace(/\\"/g, '\\\\"');
}

module.exports = {
generatePluginCommand,
commandNameToSymbol,
Expand Down

0 comments on commit 04bc1a1

Please sign in to comment.