Skip to content

Commit 30d18b8

Browse files
authored
fix: ensure reserved keywords can never be rendered for JS output (#313)
1 parent 2795f66 commit 30d18b8

File tree

9 files changed

+381
-136
lines changed

9 files changed

+381
-136
lines changed

docs/generators.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Below is a list of additional options available for a given generator.
3939
| `modelType` | String | It indicates which model type should be rendered for the `object` type. Its value can be either `interface` or `class`. | `class` |
4040
| `namingConvention` | Object | Options for naming conventions. | - |
4141
| `namingConvention.type` | Function | A function that returns the format of the type. | _Returns pascal cased name, and ensures that reserved keywords are never rendered__ |
42-
| `namingConvention.property` | Function | A function that returns the format of the property. | _Returns camel cased name, and ensures that names of properties does not clash against reserved keywords_ |
42+
| `namingConvention.property` | Function | A function that returns the format of the property. | _Returns camel cased name, and ensures that names of properties does not clash against reserved keywords for TS, as well as JS to ensure painless transpilation_ |
4343

4444
### [Java](../src/generators/java/JavaGenerator.ts)
4545

@@ -55,8 +55,8 @@ Below is a list of additional options available for a given generator.
5555
| Option | Type | Description | Default value |
5656
|---|---|---|---|
5757
| `namingConvention` | Object | Options for naming conventions. | - |
58-
| `namingConvention.type` | Function | A function that returns the format of the type. | _Returns pascal cased name_ |
59-
| `namingConvention.property` | Function | A function that returns the format of the property. | _Returns camel cased name_ |
58+
| `namingConvention.type` | Function | A function that returns the format of the type. | _Returns pascal cased name, and ensures that reserved keywords are never rendered_ |
59+
| `namingConvention.property` | Function | A function that returns the format of the property. | _Returns camel cased name, and ensures that names of properties does not clash against reserved keywords_ |
6060

6161
### [Go](../src/generators/javascript/GoGenerator.ts)
6262

src/generators/java/Constants.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
export const RESERVED_JAVA_KEYWORDS = [
2+
'abstract',
3+
'continue',
4+
'for',
5+
'new',
6+
'switch assert',
7+
'default',
8+
'goto',
9+
'package',
10+
'synchronized',
11+
'boolean',
12+
'do',
13+
'if',
14+
'private',
15+
'this',
16+
'break',
17+
'double',
18+
'implements',
19+
'protected',
20+
'throw',
21+
'byte',
22+
'else',
23+
'import',
24+
'public',
25+
'throws',
26+
'case',
27+
'enum',
28+
'instanceof',
29+
'return',
30+
'transient',
31+
'catch',
32+
'extends',
33+
'int',
34+
'short',
35+
'try',
36+
'char',
37+
'final',
38+
'interface',
39+
'static',
40+
'void',
41+
'class',
42+
'finally',
43+
'long',
44+
'strictfp',
45+
'volatile',
46+
'const',
47+
'float',
48+
'native',
49+
'super',
50+
'while'
51+
];
52+
53+
export function isReservedJavaKeyword(word: string): boolean {
54+
return RESERVED_JAVA_KEYWORDS.includes(word);
55+
}

src/generators/java/JavaRenderer.ts

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,7 @@ import { AbstractRenderer } from '../AbstractRenderer';
22
import { JavaGenerator, JavaOptions } from './JavaGenerator';
33
import { CommonModel, CommonInputModel, Preset } from '../../models';
44
import { FormatHelpers, ModelKind, TypeHelpers } from '../../helpers';
5-
6-
/**
7-
* List of reserved java keywords that may not be rendered as is.
8-
*/
9-
export const ReservedJavaKeywordList = [
10-
'abstract',
11-
'continue',
12-
'for',
13-
'new',
14-
'switch assert',
15-
'default',
16-
'goto',
17-
'package',
18-
'synchronized',
19-
'boolean',
20-
'do',
21-
'if',
22-
'private',
23-
'this',
24-
'break',
25-
'double',
26-
'implements',
27-
'protected',
28-
'throw',
29-
'byte',
30-
'else',
31-
'import',
32-
'public',
33-
'throws',
34-
'case',
35-
'enum',
36-
'instanceof',
37-
'return',
38-
'transient',
39-
'catch',
40-
'extends',
41-
'int',
42-
'short',
43-
'try',
44-
'char',
45-
'final',
46-
'interface',
47-
'static',
48-
'void',
49-
'class',
50-
'finally',
51-
'long',
52-
'strictfp',
53-
'volatile',
54-
'const',
55-
'float',
56-
'native',
57-
'super',
58-
'while'
59-
];
5+
import { isReservedJavaKeyword } from './Constants';
606

617
/**
628
* Common renderer for Java types
@@ -73,10 +19,6 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions, JavaGen
7319
) {
7420
super(options, generator, presets, model, inputModel);
7521
}
76-
77-
static isReservedJavaKeyword(word: string): boolean {
78-
return ReservedJavaKeywordList.includes(word);
79-
}
8022

8123
/**
8224
* Renders the name of a type based on provided generator option naming convention type function.
@@ -88,7 +30,7 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions, JavaGen
8830
*/
8931
nameType(name: string | undefined, model?: CommonModel): string {
9032
return this.options?.namingConvention?.type
91-
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel, isReservedKeyword: JavaRenderer.isReservedJavaKeyword(`${name}`) })
33+
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel, isReservedKeyword: isReservedJavaKeyword(`${name}`) })
9234
: name || '';
9335
}
9436

@@ -100,7 +42,7 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions, JavaGen
10042
*/
10143
nameProperty(propertyName: string | undefined, property?: CommonModel): string {
10244
return this.options?.namingConvention?.property
103-
? this.options.namingConvention.property(propertyName, { model: this.model, inputModel: this.inputModel, property, isReservedKeyword: JavaRenderer.isReservedJavaKeyword(`${propertyName}`) })
45+
? this.options.namingConvention.property(propertyName, { model: this.model, inputModel: this.inputModel, property, isReservedKeyword: isReservedJavaKeyword(`${propertyName}`) })
10446
: propertyName || '';
10547
}
10648

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
//Based on https://www.w3schools.com/js/js_reserved.asp
2+
export const RESERVED_JAVASCRIPT_KEYWORDS = [
3+
//Standard reserved keywords
4+
'abstract',
5+
'arguments',
6+
'await',
7+
'boolean',
8+
'break',
9+
'byte',
10+
'case',
11+
'catch',
12+
'char',
13+
'class',
14+
'const',
15+
'continue',
16+
'debugger',
17+
'default',
18+
'delete',
19+
'do',
20+
'double',
21+
'else',
22+
'enum',
23+
'eval',
24+
'export',
25+
'extends',
26+
'false',
27+
'final',
28+
'finally',
29+
'float',
30+
'for',
31+
'function',
32+
'goto',
33+
'if',
34+
'implements',
35+
'import',
36+
'in',
37+
'instanceof',
38+
'int',
39+
'interface',
40+
'let',
41+
'long',
42+
'native',
43+
'new',
44+
'null',
45+
'package',
46+
'private',
47+
'protected',
48+
'public',
49+
'return',
50+
'short',
51+
'static',
52+
'super',
53+
'switch',
54+
'synchronized',
55+
'this',
56+
'throw',
57+
'throws',
58+
'transient',
59+
'true',
60+
'try',
61+
'typeof',
62+
'var',
63+
'void',
64+
'volatile',
65+
'while',
66+
'with',
67+
'yield',
68+
// Reserved for > ECMAScript 5/6
69+
'abstract',
70+
'boolean',
71+
'byte',
72+
'char',
73+
'double',
74+
'final',
75+
'float',
76+
'goto',
77+
'int',
78+
'long',
79+
'native',
80+
'short',
81+
'synchronized',
82+
'throws',
83+
'transient',
84+
'volatile',
85+
//Reserved built-in objects, properties and methods
86+
'hasOwnProperty',
87+
'Infinity',
88+
'isFinite',
89+
'isNaN',
90+
'isPrototypeOf',
91+
'length',
92+
'Math',
93+
'NaN',
94+
'name',
95+
'Number',
96+
'Object',
97+
'prototype',
98+
'String',
99+
'toString',
100+
'undefined',
101+
'valueOf',
102+
//Java reserved words
103+
'getClass',
104+
'java',
105+
'JavaArray',
106+
'javaClass',
107+
'JavaObject',
108+
'JavaPackage',
109+
//Other reserved words
110+
'alert',
111+
'all',
112+
'anchor',
113+
'anchors',
114+
'area',
115+
'assign',
116+
'blur',
117+
'button',
118+
'checkbox',
119+
'clearInterval',
120+
'clearTimeout',
121+
'clientInformation',
122+
'close',
123+
'closed',
124+
'confirm',
125+
'constructor',
126+
'crypto',
127+
'decodeURI',
128+
'decodeURIComponent',
129+
'defaultStatus',
130+
'document',
131+
'element',
132+
'elements',
133+
'embed',
134+
'embeds',
135+
'encodeURI',
136+
'encodeURIComponent',
137+
'escape',
138+
'event',
139+
'fileUpload',
140+
'focus',
141+
'form',
142+
'forms',
143+
'frame',
144+
'innerHeight',
145+
'innerWidth',
146+
'layer',
147+
'layers',
148+
'link',
149+
'location',
150+
'mimeTypes',
151+
'navigate',
152+
'navigator',
153+
'frames',
154+
'frameRate',
155+
'hidden',
156+
'history',
157+
'image',
158+
'images',
159+
'offscreenBuffering',
160+
'open',
161+
'opener',
162+
'option',
163+
'outerHeight',
164+
'outerWidth',
165+
'packages',
166+
'pageXOffset',
167+
'pageYOffset',
168+
'parent',
169+
'parseFloat',
170+
'parseInt',
171+
'password',
172+
'pkcs11',
173+
'plugin',
174+
'prompt',
175+
'propertyIsEnum',
176+
'radio',
177+
'reset',
178+
'screenX',
179+
'screenY',
180+
'scroll',
181+
'secure',
182+
'select',
183+
'self',
184+
'setInterval',
185+
'setTimeout',
186+
'status',
187+
'submit',
188+
'taint',
189+
'text',
190+
'textarea',
191+
'top',
192+
'unescape',
193+
'untaint',
194+
'window'
195+
];
196+
197+
export function isReservedJavaScriptKeyword(word: string): boolean {
198+
return RESERVED_JAVASCRIPT_KEYWORDS.includes(word);
199+
}

src/generators/javascript/JavaScriptRenderer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { AbstractRenderer } from '../AbstractRenderer';
22
import { JavaScriptGenerator, JavaScriptOptions } from './JavaScriptGenerator';
3-
43
import { getUniquePropertyName, FormatHelpers, DefaultPropertyNames } from '../../helpers';
54
import { CommonModel, CommonInputModel, Preset, PropertyType } from '../../models';
5+
import { isReservedJavaScriptKeyword } from './Constants';
66

77
/**
88
* Common renderer for JavaScript types
@@ -30,7 +30,7 @@ export abstract class JavaScriptRenderer extends AbstractRenderer<JavaScriptOpti
3030
*/
3131
nameType(name: string | undefined, model?: CommonModel): string {
3232
return this.options?.namingConvention?.type
33-
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel })
33+
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel, isReservedKeyword: isReservedJavaScriptKeyword(`${name}`) })
3434
: name || '';
3535
}
3636

@@ -42,7 +42,7 @@ export abstract class JavaScriptRenderer extends AbstractRenderer<JavaScriptOpti
4242
*/
4343
nameProperty(propertyName: string | undefined, property?: CommonModel): string {
4444
return this.options?.namingConvention?.property
45-
? this.options.namingConvention.property(propertyName, { model: this.model, inputModel: this.inputModel, property })
45+
? this.options.namingConvention.property(propertyName, { model: this.model, inputModel: this.inputModel, property, isReservedKeyword: isReservedJavaScriptKeyword(`${propertyName}`) })
4646
: propertyName || '';
4747
}
4848

0 commit comments

Comments
 (0)