Browse Source

rename prop key checker utility as shouldQuoteProp

pull/8029/head
Arman Ozak 6 years ago
parent
commit
0c11c13d9d
  1. 8
      npm/ng-packs/packages/schematics/src/models/method.ts
  2. 4
      npm/ng-packs/packages/schematics/src/utils/enum.ts
  3. 4
      npm/ng-packs/packages/schematics/src/utils/model.ts
  4. 8
      npm/ng-packs/packages/schematics/src/utils/prop.ts

8
npm/ng-packs/packages/schematics/src/models/method.ts

@ -1,5 +1,5 @@
import { eBindingSourceId, eMethodModifier } from '../enums'; import { eBindingSourceId, eMethodModifier } from '../enums';
import { isValidProp } from '../utils/prop'; import { shouldQuoteProp } from '../utils/prop';
import { camel } from '../utils/text'; import { camel } from '../utils/text';
import { ParameterInBody } from './api-definition'; import { ParameterInBody } from './api-definition';
import { Property } from './model'; import { Property } from './model';
@ -46,9 +46,9 @@ export class Body {
const camelName = camel(name); const camelName = camel(name);
const paramName = jsonName || camelName; const paramName = jsonName || camelName;
const value = descriptorName const value = descriptorName
? isValidProp(paramName) ? shouldQuoteProp(paramName)
? `${descriptorName}.${paramName}` ? `${descriptorName}['${paramName}']`
: `${descriptorName}['${paramName}']` : `${descriptorName}.${paramName}`
: nameOnMethod; : nameOnMethod;
switch (bindingSourceId) { switch (bindingSourceId) {

4
npm/ng-packs/packages/schematics/src/utils/enum.ts

@ -3,7 +3,7 @@ import { Exception } from '../enums';
import { Type } from '../models'; import { Type } from '../models';
import { interpolate } from './common'; import { interpolate } from './common';
import { parseNamespace } from './namespace'; import { parseNamespace } from './namespace';
import { isValidProp } from './prop'; import { shouldQuoteProp } from './prop';
export interface EnumGeneratorParams { export interface EnumGeneratorParams {
targetPath: string; targetPath: string;
@ -34,7 +34,7 @@ export function createImportRefToEnumMapper({ solution, types }: EnumGeneratorPa
const namespace = parseNamespace(solution, ref); const namespace = parseNamespace(solution, ref);
const members = enumNames!.map((key, i) => ({ const members = enumNames!.map((key, i) => ({
key: isValidProp(key) ? key : `'${key}'`, key: shouldQuoteProp(key) ? `'${key}'` : key,
value: enumValues[i], value: enumValues[i],
})); }));

4
npm/ng-packs/packages/schematics/src/utils/model.ts

@ -8,7 +8,7 @@ import {
} from './generics'; } from './generics';
import { parseNamespace } from './namespace'; import { parseNamespace } from './namespace';
import { relativePathToModel } from './path'; import { relativePathToModel } from './path';
import { isValidProp } from './prop'; import { shouldQuoteProp } from './prop';
import { camel } from './text'; import { camel } from './text';
import { parseGenerics } from './tree'; import { parseGenerics } from './tree';
import { import {
@ -136,7 +136,7 @@ export function createImportRefToInterfaceReducerCreator(params: ModelGeneratorP
typeDef.properties?.forEach(prop => { typeDef.properties?.forEach(prop => {
let name = prop.jsonName || camel(prop.name); let name = prop.jsonName || camel(prop.name);
name = isValidProp(name) ? name : `'${name}'`; name = shouldQuoteProp(name) ? `'${name}'` : name;
const type = simplifyType(prop.typeSimple); const type = simplifyType(prop.typeSimple);
const refs = parseType(prop.type).reduce( const refs = parseType(prop.type).reduce(
(acc: string[], r) => acc.concat(parseGenerics(r).toGenerics()), (acc: string[], r) => acc.concat(parseGenerics(r).toGenerics()),

8
npm/ng-packs/packages/schematics/src/utils/prop.ts

@ -1,9 +1,9 @@
export const isValidProp = (name: string) => { export const shouldQuoteProp = (key: string) => {
try { try {
new Function('return {}.' + name); new Function('return {}.' + key);
} catch (_) { } catch (_) {
return false; return true;
} }
return true; return false;
}; };

Loading…
Cancel
Save