From ab9ee98e28ba82455f08c3f1ebc290797ad18506 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 10 Mar 2021 19:22:29 +0300 Subject: [PATCH 1/7] add util fn to check if string is valid prop name --- npm/ng-packs/packages/schematics/src/utils/index.ts | 1 + npm/ng-packs/packages/schematics/src/utils/prop.ts | 9 +++++++++ 2 files changed, 10 insertions(+) create mode 100644 npm/ng-packs/packages/schematics/src/utils/prop.ts diff --git a/npm/ng-packs/packages/schematics/src/utils/index.ts b/npm/ng-packs/packages/schematics/src/utils/index.ts index e6df05b2e2..a33218d1ef 100644 --- a/npm/ng-packs/packages/schematics/src/utils/index.ts +++ b/npm/ng-packs/packages/schematics/src/utils/index.ts @@ -10,6 +10,7 @@ export * from './import'; export * from './model'; export * from './namespace'; export * from './path'; +export * from './prop'; export * from './rule'; export * from './service'; export * from './source'; diff --git a/npm/ng-packs/packages/schematics/src/utils/prop.ts b/npm/ng-packs/packages/schematics/src/utils/prop.ts new file mode 100644 index 0000000000..5109fcb721 --- /dev/null +++ b/npm/ng-packs/packages/schematics/src/utils/prop.ts @@ -0,0 +1,9 @@ +export const isValidProp = (name: string) => { + try { + new Function(name, 'var ' + name); + } catch (_) { + return false; + } + + return true; +}; From 45dcefc6b2fbba09b75c8aa2c85f1bcf00e2045b Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 10 Mar 2021 19:23:34 +0300 Subject: [PATCH 2/7] quote enum member keys if not a valid prop name --- npm/ng-packs/packages/schematics/src/utils/enum.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/schematics/src/utils/enum.ts b/npm/ng-packs/packages/schematics/src/utils/enum.ts index c01b086ec5..5ff644db82 100644 --- a/npm/ng-packs/packages/schematics/src/utils/enum.ts +++ b/npm/ng-packs/packages/schematics/src/utils/enum.ts @@ -3,6 +3,7 @@ import { Exception } from '../enums'; import { Type } from '../models'; import { interpolate } from './common'; import { parseNamespace } from './namespace'; +import { isValidProp } from './prop'; export interface EnumGeneratorParams { targetPath: string; @@ -32,7 +33,10 @@ export function createImportRefToEnumMapper({ solution, types }: EnumGeneratorPa throw new SchematicsException(interpolate(Exception.NoTypeDefinition, ref)); const namespace = parseNamespace(solution, ref); - const members = enumNames!.map((key, i) => ({ key, value: enumValues[i] })); + const members = enumNames!.map((key, i) => ({ + key: isValidProp(key) ? key : `'${key}'`, + value: enumValues[i], + })); return { namespace, From d46397630d75aac6f5d90cbaf5e753be3c6ebaa5 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 10 Mar 2021 19:24:40 +0300 Subject: [PATCH 3/7] quote interface keys if not valid a property name --- npm/ng-packs/packages/schematics/src/utils/model.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/schematics/src/utils/model.ts b/npm/ng-packs/packages/schematics/src/utils/model.ts index 9c2b7cfae8..96856fdbe4 100644 --- a/npm/ng-packs/packages/schematics/src/utils/model.ts +++ b/npm/ng-packs/packages/schematics/src/utils/model.ts @@ -8,6 +8,7 @@ import { } from './generics'; import { parseNamespace } from './namespace'; import { relativePathToModel } from './path'; +import { isValidProp } from './prop'; import { camel } from './text'; import { parseGenerics } from './tree'; import { @@ -134,7 +135,8 @@ export function createImportRefToInterfaceReducerCreator(params: ModelGeneratorP genericsCollector.reset(); typeDef.properties?.forEach(prop => { - const name = prop.jsonName || camel(prop.name); + let name = prop.jsonName || camel(prop.name); + name = isValidProp(name) ? name : `'${name}'`; const type = simplifyType(prop.typeSimple); const refs = parseType(prop.type).reduce( (acc: string[], r) => acc.concat(parseGenerics(r).toGenerics()), From 5080d94c72b027c12958335e192ebd5ceed55291 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 10 Mar 2021 19:31:13 +0300 Subject: [PATCH 4/7] use bracket notation on decriptors for invalid keys --- npm/ng-packs/packages/schematics/src/models/method.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/npm/ng-packs/packages/schematics/src/models/method.ts b/npm/ng-packs/packages/schematics/src/models/method.ts index 782128540c..5a15a3770e 100644 --- a/npm/ng-packs/packages/schematics/src/models/method.ts +++ b/npm/ng-packs/packages/schematics/src/models/method.ts @@ -1,5 +1,6 @@ import { eBindingSourceId, eMethodModifier } from '../enums'; -import { camel } from '../utils'; +import { isValidProp } from '../utils/prop'; +import { camel } from '../utils/text'; import { ParameterInBody } from './api-definition'; import { Property } from './model'; import { Omissible } from './util'; @@ -44,7 +45,11 @@ export class Body { const { bindingSourceId, descriptorName, jsonName, name, nameOnMethod } = param; const camelName = camel(name); const paramName = jsonName || camelName; - const value = descriptorName ? `${descriptorName}.${paramName}` : nameOnMethod; + const value = descriptorName + ? isValidProp(paramName) + ? `${descriptorName}.${paramName}` + : `${descriptorName}['${paramName}']` + : nameOnMethod; switch (bindingSourceId) { case eBindingSourceId.Model: From 126cdc2a760b3c0e5844a62c56e2a899ece00b38 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 10 Mar 2021 19:33:32 +0300 Subject: [PATCH 5/7] set url quotes based on template literal placeholder use --- .../__namespace@dir__/__name@kebab__.service.ts.template | 2 +- npm/ng-packs/packages/schematics/src/models/method.ts | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template b/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template index 1d77599e7e..78fb27c33d 100644 --- a/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template +++ b/npm/ng-packs/packages/schematics/src/commands/api/files-service/proxy/__namespace@dir__/__name@kebab__.service.ts.template @@ -13,7 +13,7 @@ export class <%= name %>Service { method: '<%= body.method %>',<% if (body.responseType === 'string') { %> responseType: 'text',<% } %> - url: `/<%= body.url %>`,<% + url: <%= body.url %>,<% if (body.params.length) { %> params: { <%= body.params.join(', ') %> },<% } if (body.body) { %> diff --git a/npm/ng-packs/packages/schematics/src/models/method.ts b/npm/ng-packs/packages/schematics/src/models/method.ts index 5a15a3770e..d52e8d5e4f 100644 --- a/npm/ng-packs/packages/schematics/src/models/method.ts +++ b/npm/ng-packs/packages/schematics/src/models/method.ts @@ -70,6 +70,11 @@ export class Body { constructor(options: BodyOptions) { Object.assign(this, options); + this.setUrlQuotes(); + } + + private setUrlQuotes() { + this.url = /{/.test(this.url) ? `\`/${this.url}\`` : `'/${this.url}'`; } } From 059d0aaf05cc01ec769671b781e52cd577455500 Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Wed, 10 Mar 2021 19:35:20 +0300 Subject: [PATCH 6/7] use object literal shorthand on params if available --- npm/ng-packs/packages/schematics/src/models/method.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/schematics/src/models/method.ts b/npm/ng-packs/packages/schematics/src/models/method.ts index d52e8d5e4f..96db013c8f 100644 --- a/npm/ng-packs/packages/schematics/src/models/method.ts +++ b/npm/ng-packs/packages/schematics/src/models/method.ts @@ -54,7 +54,7 @@ export class Body { switch (bindingSourceId) { case eBindingSourceId.Model: case eBindingSourceId.Query: - this.params.push(`${paramName}: ${value}`); + this.params.push(paramName === value ? value : `${paramName}: ${value}`); break; case eBindingSourceId.Body: this.body = value; From b36247be163b95d1a9be9443b15ae3948dc862de Mon Sep 17 00:00:00 2001 From: Arman Ozak Date: Thu, 11 Mar 2021 12:39:03 +0300 Subject: [PATCH 7/7] modify isValidProp to allow reserved words too --- npm/ng-packs/packages/schematics/src/utils/prop.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/npm/ng-packs/packages/schematics/src/utils/prop.ts b/npm/ng-packs/packages/schematics/src/utils/prop.ts index 5109fcb721..0c78de7b14 100644 --- a/npm/ng-packs/packages/schematics/src/utils/prop.ts +++ b/npm/ng-packs/packages/schematics/src/utils/prop.ts @@ -1,6 +1,6 @@ export const isValidProp = (name: string) => { try { - new Function(name, 'var ' + name); + new Function('return {}.' + name); } catch (_) { return false; }