Skip to content

Commit

Permalink
fix: ensure reserved keywords can never be rendered for JS output (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jul 28, 2021
1 parent 2795f66 commit 30d18b8
Show file tree
Hide file tree
Showing 9 changed files with 381 additions and 136 deletions.
6 changes: 3 additions & 3 deletions docs/generators.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Below is a list of additional options available for a given generator.
| `modelType` | String | It indicates which model type should be rendered for the `object` type. Its value can be either `interface` or `class`. | `class` |
| `namingConvention` | Object | Options for naming conventions. | - |
| `namingConvention.type` | Function | A function that returns the format of the type. | _Returns pascal cased name, and ensures that reserved keywords are never rendered__ |
| `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_ |
| `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_ |

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

Expand All @@ -55,8 +55,8 @@ Below is a list of additional options available for a given generator.
| Option | Type | Description | Default value |
|---|---|---|---|
| `namingConvention` | Object | Options for naming conventions. | - |
| `namingConvention.type` | Function | A function that returns the format of the type. | _Returns pascal cased name_ |
| `namingConvention.property` | Function | A function that returns the format of the property. | _Returns camel cased name_ |
| `namingConvention.type` | Function | A function that returns the format of the type. | _Returns pascal cased name, and ensures that reserved keywords are never rendered_ |
| `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_ |

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

Expand Down
55 changes: 55 additions & 0 deletions src/generators/java/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
export const RESERVED_JAVA_KEYWORDS = [
'abstract',
'continue',
'for',
'new',
'switch assert',
'default',
'goto',
'package',
'synchronized',
'boolean',
'do',
'if',
'private',
'this',
'break',
'double',
'implements',
'protected',
'throw',
'byte',
'else',
'import',
'public',
'throws',
'case',
'enum',
'instanceof',
'return',
'transient',
'catch',
'extends',
'int',
'short',
'try',
'char',
'final',
'interface',
'static',
'void',
'class',
'finally',
'long',
'strictfp',
'volatile',
'const',
'float',
'native',
'super',
'while'
];

export function isReservedJavaKeyword(word: string): boolean {
return RESERVED_JAVA_KEYWORDS.includes(word);
}
64 changes: 3 additions & 61 deletions src/generators/java/JavaRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,7 @@ import { AbstractRenderer } from '../AbstractRenderer';
import { JavaGenerator, JavaOptions } from './JavaGenerator';
import { CommonModel, CommonInputModel, Preset } from '../../models';
import { FormatHelpers, ModelKind, TypeHelpers } from '../../helpers';

/**
* List of reserved java keywords that may not be rendered as is.
*/
export const ReservedJavaKeywordList = [
'abstract',
'continue',
'for',
'new',
'switch assert',
'default',
'goto',
'package',
'synchronized',
'boolean',
'do',
'if',
'private',
'this',
'break',
'double',
'implements',
'protected',
'throw',
'byte',
'else',
'import',
'public',
'throws',
'case',
'enum',
'instanceof',
'return',
'transient',
'catch',
'extends',
'int',
'short',
'try',
'char',
'final',
'interface',
'static',
'void',
'class',
'finally',
'long',
'strictfp',
'volatile',
'const',
'float',
'native',
'super',
'while'
];
import { isReservedJavaKeyword } from './Constants';

/**
* Common renderer for Java types
Expand All @@ -73,10 +19,6 @@ export abstract class JavaRenderer extends AbstractRenderer<JavaOptions, JavaGen
) {
super(options, generator, presets, model, inputModel);
}

static isReservedJavaKeyword(word: string): boolean {
return ReservedJavaKeywordList.includes(word);
}

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

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

Expand Down
199 changes: 199 additions & 0 deletions src/generators/javascript/Constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
//Based on https://www.w3schools.com/js/js_reserved.asp
export const RESERVED_JAVASCRIPT_KEYWORDS = [
//Standard reserved keywords
'abstract',
'arguments',
'await',
'boolean',
'break',
'byte',
'case',
'catch',
'char',
'class',
'const',
'continue',
'debugger',
'default',
'delete',
'do',
'double',
'else',
'enum',
'eval',
'export',
'extends',
'false',
'final',
'finally',
'float',
'for',
'function',
'goto',
'if',
'implements',
'import',
'in',
'instanceof',
'int',
'interface',
'let',
'long',
'native',
'new',
'null',
'package',
'private',
'protected',
'public',
'return',
'short',
'static',
'super',
'switch',
'synchronized',
'this',
'throw',
'throws',
'transient',
'true',
'try',
'typeof',
'var',
'void',
'volatile',
'while',
'with',
'yield',
// Reserved for > ECMAScript 5/6
'abstract',
'boolean',
'byte',
'char',
'double',
'final',
'float',
'goto',
'int',
'long',
'native',
'short',
'synchronized',
'throws',
'transient',
'volatile',
//Reserved built-in objects, properties and methods
'hasOwnProperty',
'Infinity',
'isFinite',
'isNaN',
'isPrototypeOf',
'length',
'Math',
'NaN',
'name',
'Number',
'Object',
'prototype',
'String',
'toString',
'undefined',
'valueOf',
//Java reserved words
'getClass',
'java',
'JavaArray',
'javaClass',
'JavaObject',
'JavaPackage',
//Other reserved words
'alert',
'all',
'anchor',
'anchors',
'area',
'assign',
'blur',
'button',
'checkbox',
'clearInterval',
'clearTimeout',
'clientInformation',
'close',
'closed',
'confirm',
'constructor',
'crypto',
'decodeURI',
'decodeURIComponent',
'defaultStatus',
'document',
'element',
'elements',
'embed',
'embeds',
'encodeURI',
'encodeURIComponent',
'escape',
'event',
'fileUpload',
'focus',
'form',
'forms',
'frame',
'innerHeight',
'innerWidth',
'layer',
'layers',
'link',
'location',
'mimeTypes',
'navigate',
'navigator',
'frames',
'frameRate',
'hidden',
'history',
'image',
'images',
'offscreenBuffering',
'open',
'opener',
'option',
'outerHeight',
'outerWidth',
'packages',
'pageXOffset',
'pageYOffset',
'parent',
'parseFloat',
'parseInt',
'password',
'pkcs11',
'plugin',
'prompt',
'propertyIsEnum',
'radio',
'reset',
'screenX',
'screenY',
'scroll',
'secure',
'select',
'self',
'setInterval',
'setTimeout',
'status',
'submit',
'taint',
'text',
'textarea',
'top',
'unescape',
'untaint',
'window'
];

export function isReservedJavaScriptKeyword(word: string): boolean {
return RESERVED_JAVASCRIPT_KEYWORDS.includes(word);
}
6 changes: 3 additions & 3 deletions src/generators/javascript/JavaScriptRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AbstractRenderer } from '../AbstractRenderer';
import { JavaScriptGenerator, JavaScriptOptions } from './JavaScriptGenerator';

import { getUniquePropertyName, FormatHelpers, DefaultPropertyNames } from '../../helpers';
import { CommonModel, CommonInputModel, Preset, PropertyType } from '../../models';
import { isReservedJavaScriptKeyword } from './Constants';

/**
* Common renderer for JavaScript types
Expand Down Expand Up @@ -30,7 +30,7 @@ export abstract class JavaScriptRenderer extends AbstractRenderer<JavaScriptOpti
*/
nameType(name: string | undefined, model?: CommonModel): string {
return this.options?.namingConvention?.type
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel })
? this.options.namingConvention.type(name, { model: model || this.model, inputModel: this.inputModel, isReservedKeyword: isReservedJavaScriptKeyword(`${name}`) })
: name || '';
}

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

Expand Down
Loading

0 comments on commit 30d18b8

Please sign in to comment.