Skip to content

Commit 1e62323

Browse files
committed
Organize all uppercasing logic to single function
1 parent 3ebd3d8 commit 1e62323

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

src/core/Formatter.js

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export default class Formatter {
106106
}
107107

108108
formatLineComment(token, query) {
109-
return this.addNewline(query + token.value);
109+
return this.addNewline(query + this.show(token));
110110
}
111111

112112
formatBlockComment(token, query) {
@@ -119,7 +119,7 @@ export default class Formatter {
119119

120120
formatTopLevelReservedWordNoIndent(token, query) {
121121
this.indentation.decreaseTopLevel();
122-
query = this.addNewline(query) + this.equalizeWhitespace(this.formatReservedWord(token.value));
122+
query = this.addNewline(query) + this.equalizeWhitespace(this.show(token));
123123
return this.addNewline(query);
124124
}
125125

@@ -130,14 +130,12 @@ export default class Formatter {
130130

131131
this.indentation.increaseTopLevel();
132132

133-
query += this.equalizeWhitespace(this.formatReservedWord(token.value));
133+
query += this.equalizeWhitespace(this.show(token));
134134
return this.addNewline(query);
135135
}
136136

137137
formatNewlineReservedWord(token, query) {
138-
return (
139-
this.addNewline(query) + this.equalizeWhitespace(this.formatReservedWord(token.value)) + ' '
140-
);
138+
return this.addNewline(query) + this.equalizeWhitespace(this.show(token)) + ' ';
141139
}
142140

143141
// Replace any sequence of whitespace characters with single space
@@ -158,7 +156,7 @@ export default class Formatter {
158156
if (!preserveWhitespaceFor[this.previousToken().type]) {
159157
query = trimSpacesEnd(query);
160158
}
161-
query += this.cfg.uppercase ? token.value.toUpperCase() : token.value;
159+
query += this.show(token);
162160

163161
this.inlineBlock.beginIfPossible(this.tokens, this.index);
164162

@@ -171,7 +169,6 @@ export default class Formatter {
171169

172170
// Closing parentheses decrease the block indent level
173171
formatClosingParentheses(token, query) {
174-
token.value = this.cfg.uppercase ? token.value.toUpperCase() : token.value;
175172
if (this.inlineBlock.isActive()) {
176173
this.inlineBlock.end();
177174
return this.formatWithSpaceAfter(token, query);
@@ -187,7 +184,7 @@ export default class Formatter {
187184

188185
// Commas start a new line (unless within inline parentheses or SQL "LIMIT" clause)
189186
formatComma(token, query) {
190-
query = trimSpacesEnd(query) + token.value + ' ';
187+
query = trimSpacesEnd(query) + this.show(token) + ' ';
191188

192189
if (this.inlineBlock.isActive()) {
193190
return query;
@@ -199,26 +196,37 @@ export default class Formatter {
199196
}
200197

201198
formatWithSpaceAfter(token, query) {
202-
return trimSpacesEnd(query) + token.value + ' ';
199+
return trimSpacesEnd(query) + this.show(token) + ' ';
203200
}
204201

205202
formatWithoutSpaces(token, query) {
206-
return trimSpacesEnd(query) + token.value;
203+
return trimSpacesEnd(query) + this.show(token);
207204
}
208205

209206
formatWithSpaces(token, query) {
210-
const value =
211-
token.type === tokenTypes.RESERVED ? this.formatReservedWord(token.value) : token.value;
212-
return query + value + ' ';
213-
}
214-
215-
formatReservedWord(value) {
216-
return this.cfg.uppercase ? value.toUpperCase() : value;
207+
return query + this.show(token) + ' ';
217208
}
218209

219210
formatQuerySeparator(token, query) {
220211
this.indentation.resetIndentation();
221-
return trimSpacesEnd(query) + token.value + '\n'.repeat(this.cfg.linesBetweenQueries || 1);
212+
return trimSpacesEnd(query) + this.show(token) + '\n'.repeat(this.cfg.linesBetweenQueries || 1);
213+
}
214+
215+
// Converts token to string (uppercasing it if needed)
216+
show({ type, value }) {
217+
if (
218+
this.cfg.uppercase &&
219+
(type === tokenTypes.RESERVED ||
220+
type === tokenTypes.RESERVED_TOP_LEVEL ||
221+
type === tokenTypes.RESERVED_TOP_LEVEL_NO_INDENT ||
222+
type === tokenTypes.RESERVED_NEWLINE ||
223+
type === tokenTypes.OPEN_PAREN ||
224+
type === tokenTypes.CLOSE_PAREN)
225+
) {
226+
return value.toUpperCase();
227+
} else {
228+
return value;
229+
}
222230
}
223231

224232
addNewline(query) {

0 commit comments

Comments
 (0)