From cd18de7a2167e0a81ac6dba03e586e030784d899 Mon Sep 17 00:00:00 2001 From: Marc Laval Date: Thu, 4 Aug 2016 19:35:41 +0200 Subject: [PATCH] refactor(compiler): use Object.keys instead of Object.getOwnPropertyNames (#10498) --- modules/@angular/common/src/directives/ng_plural.ts | 4 ++-- modules/@angular/common/src/pipes/i18n_plural_pipe.ts | 2 +- .../@angular/compiler/src/i18n/serializers/placeholder.ts | 5 ++--- modules/@angular/compiler/src/i18n/serializers/xmb.ts | 4 ++-- .../@angular/compiler/src/i18n/serializers/xml_helper.ts | 8 +++----- modules/@angular/compiler/src/i18n/serializers/xtb.ts | 2 +- modules/@angular/compiler/test/i18n/i18n_parser_spec.ts | 2 +- .../@angular/compiler/test/i18n/serializers/xmb_spec.ts | 2 +- .../@angular/compiler/test/i18n/serializers/xtb_spec.ts | 5 ++--- modules/@angular/compiler/test/schema/schema_extractor.ts | 4 ++-- modules/@angular/compiler/testing/metadata_overrider.ts | 4 ++-- 11 files changed, 19 insertions(+), 23 deletions(-) diff --git a/modules/@angular/common/src/directives/ng_plural.ts b/modules/@angular/common/src/directives/ng_plural.ts index a75590a2e1..476310f312 100644 --- a/modules/@angular/common/src/directives/ng_plural.ts +++ b/modules/@angular/common/src/directives/ng_plural.ts @@ -87,8 +87,8 @@ export class NgPlural { _updateView(): void { this._clearViews(); - var key = getPluralCategory( - this._switchValue, Object.getOwnPropertyNames(this._caseViews), this._localization); + var key = + getPluralCategory(this._switchValue, Object.keys(this._caseViews), this._localization); this._activateView(this._caseViews[key]); } diff --git a/modules/@angular/common/src/pipes/i18n_plural_pipe.ts b/modules/@angular/common/src/pipes/i18n_plural_pipe.ts index f1b7cfec9a..f8b5e9843f 100644 --- a/modules/@angular/common/src/pipes/i18n_plural_pipe.ts +++ b/modules/@angular/common/src/pipes/i18n_plural_pipe.ts @@ -68,7 +68,7 @@ export class I18nPluralPipe implements PipeTransform { throw new InvalidPipeArgumentException(I18nPluralPipe, pluralMap); } - const key = getPluralCategory(value, Object.getOwnPropertyNames(pluralMap), this._localization); + const key = getPluralCategory(value, Object.keys(pluralMap), this._localization); return StringWrapper.replaceAll(pluralMap[key], _INTERPOLATION_REGEXP, value.toString()); } diff --git a/modules/@angular/compiler/src/i18n/serializers/placeholder.ts b/modules/@angular/compiler/src/i18n/serializers/placeholder.ts index c772a519d3..0a388deb86 100644 --- a/modules/@angular/compiler/src/i18n/serializers/placeholder.ts +++ b/modules/@angular/compiler/src/i18n/serializers/placeholder.ts @@ -96,8 +96,7 @@ export class PlaceholderRegistry { // Generate a hash for a tag - does not take attribute order into account private _hashTag(tag: string, attrs: {[k: string]: string}, isVoid: boolean): string { const start = `<${tag}`; - const strAttrs = - Object.getOwnPropertyNames(attrs).sort().map((name) => ` ${name}=${attrs[name]}`).join(''); + const strAttrs = Object.keys(attrs).sort().map((name) => ` ${name}=${attrs[name]}`).join(''); const end = isVoid ? '/>' : `>`; return start + strAttrs + end; @@ -120,4 +119,4 @@ export class PlaceholderRegistry { return name; } -} \ No newline at end of file +} diff --git a/modules/@angular/compiler/src/i18n/serializers/xmb.ts b/modules/@angular/compiler/src/i18n/serializers/xmb.ts index 05976e7577..bed72a66dc 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xmb.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xmb.ts @@ -44,7 +44,7 @@ export class Xmb implements Serializer { let rootNode = new xml.Tag(_MESSAGES_TAG); rootNode.children.push(new xml.Text('\n')); - Object.getOwnPropertyNames(messageMap).forEach((id) => { + Object.keys(messageMap).forEach((id) => { const message = messageMap[id]; let attrs: {[k: string]: string} = {id}; @@ -88,7 +88,7 @@ class _Visitor implements i18n.Visitor { visitIcu(icu: i18n.Icu, context?: any): xml.Node[] { const nodes = [new xml.Text(`{${icu.expression}, ${icu.type}, `)]; - Object.getOwnPropertyNames(icu.cases).forEach((c: string) => { + Object.keys(icu.cases).forEach((c: string) => { nodes.push(new xml.Text(`${c} {`), ...icu.cases[c].visit(this), new xml.Text(`}`)); }); diff --git a/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts b/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts index 3adf5b7f36..5e0354520b 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xml_helper.ts @@ -32,9 +32,7 @@ class _Visitor implements IVisitor { } private _serializeAttributes(attrs: {[k: string]: string}) { - const strAttrs = Object.getOwnPropertyNames(attrs) - .map((name: string) => `${name}="${attrs[name]}"`) - .join(' '); + const strAttrs = Object.keys(attrs).map((name: string) => `${name}="${attrs[name]}"`).join(' '); return strAttrs.length > 0 ? ' ' + strAttrs : ''; } @@ -55,7 +53,7 @@ export class Declaration implements Node { public attrs: {[k: string]: string} = {}; constructor(unescapedAttrs: {[k: string]: string}) { - Object.getOwnPropertyNames(unescapedAttrs).forEach((k: string) => { + Object.keys(unescapedAttrs).forEach((k: string) => { this.attrs[k] = _escapeXml(unescapedAttrs[k]); }); } @@ -75,7 +73,7 @@ export class Tag implements Node { constructor( public name: string, unescapedAttrs: {[k: string]: string} = {}, public children: Node[] = []) { - Object.getOwnPropertyNames(unescapedAttrs).forEach((k: string) => { + Object.keys(unescapedAttrs).forEach((k: string) => { this.attrs[k] = _escapeXml(unescapedAttrs[k]); }); } diff --git a/modules/@angular/compiler/src/i18n/serializers/xtb.ts b/modules/@angular/compiler/src/i18n/serializers/xtb.ts index bc4ee68ef4..406e313787 100644 --- a/modules/@angular/compiler/src/i18n/serializers/xtb.ts +++ b/modules/@angular/compiler/src/i18n/serializers/xtb.ts @@ -46,7 +46,7 @@ export class Xtb implements Serializer { let messageMap: {[id: string]: xml.Node[]} = {}; let parseErrors: ParseError[] = []; - Object.getOwnPropertyNames(messages).forEach((id) => { + Object.keys(messages).forEach((id) => { const res = this._htmlParser.parse(messages[id], url, true, this._interpolationConfig); parseErrors.push(...res.errors); messageMap[id] = res.rootNodes; diff --git a/modules/@angular/compiler/test/i18n/i18n_parser_spec.ts b/modules/@angular/compiler/test/i18n/i18n_parser_spec.ts index 43af3b7ff6..de6bfbda92 100644 --- a/modules/@angular/compiler/test/i18n/i18n_parser_spec.ts +++ b/modules/@angular/compiler/test/i18n/i18n_parser_spec.ts @@ -299,7 +299,7 @@ function _humanizePlaceholders( // clang-format off // https://github.com/angular/clang-format/issues/35 return _extractMessages(html, implicitTags, implicitAttrs).map( - msg => Object.getOwnPropertyNames(msg.placeholders).map((name) => `${name}=${msg.placeholders[name]}`).join(', ')); + msg => Object.keys(msg.placeholders).map((name) => `${name}=${msg.placeholders[name]}`).join(', ')); // clang-format on } diff --git a/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts b/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts index 9c35f75fea..7a5c6f0960 100644 --- a/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts +++ b/modules/@angular/compiler/test/i18n/serializers/xmb_spec.ts @@ -69,4 +69,4 @@ function toXmb(html: string): string { catalog.updateFromTemplate(html, '', DEFAULT_INTERPOLATION_CONFIG); return catalog.write(serializer); -} \ No newline at end of file +} diff --git a/modules/@angular/compiler/test/i18n/serializers/xtb_spec.ts b/modules/@angular/compiler/test/i18n/serializers/xtb_spec.ts index 372d22b345..fa1bdfee93 100644 --- a/modules/@angular/compiler/test/i18n/serializers/xtb_spec.ts +++ b/modules/@angular/compiler/test/i18n/serializers/xtb_spec.ts @@ -22,8 +22,7 @@ export function main(): void { {[id: string]: string} { const asAst = serializer.load(content, 'url', placeholders); let asText: {[id: string]: string} = {}; - Object.getOwnPropertyNames(asAst).forEach( - id => { asText[id] = serializeAst(asAst[id]).join(''); }); + Object.keys(asAst).forEach(id => { asText[id] = serializeAst(asAst[id]).join(''); }); return asText; } @@ -178,4 +177,4 @@ export function main(): void { it('should throw when trying to save an xmb file', () => { expect(() => { serializer.write({}); }).toThrow(); }); }); -} \ No newline at end of file +} diff --git a/modules/@angular/compiler/test/schema/schema_extractor.ts b/modules/@angular/compiler/test/schema/schema_extractor.ts index bd62e9a985..07d79ff75e 100644 --- a/modules/@angular/compiler/test/schema/schema_extractor.ts +++ b/modules/@angular/compiler/test/schema/schema_extractor.ts @@ -55,7 +55,7 @@ export function extractSchema(): Map { extractProperties( SVGTextPositioningElement, svgText, visited, descMap, SVG_PREFIX + 'textPositioning', SVG_PREFIX + 'textContent'); - var keys = Object.getOwnPropertyNames(window).filter( + var keys = Object.keys(window).filter( k => k.endsWith('Element') && (k.startsWith('HTML') || k.startsWith('SVG'))); keys.sort(); keys.forEach( @@ -97,7 +97,7 @@ function extractProperties( const fullName = name + (superName ? '^' + superName : ''); let props: string[] = descMap.has(fullName) ? descMap.get(fullName) : []; var prototype = type.prototype; - var keys = Object.getOwnPropertyNames(prototype); + var keys = Object.keys(prototype); keys.sort(); keys.forEach((n) => { if (n.startsWith('on')) { diff --git a/modules/@angular/compiler/testing/metadata_overrider.ts b/modules/@angular/compiler/testing/metadata_overrider.ts index 6f4d2260a8..e0cccf5865 100644 --- a/modules/@angular/compiler/testing/metadata_overrider.ts +++ b/modules/@angular/compiler/testing/metadata_overrider.ts @@ -114,14 +114,14 @@ function _serializeReference(ref: any, references: Map): string { function _valueProps(obj: any): string[] { const props: string[] = []; // regular public props - Object.getOwnPropertyNames(obj).forEach((prop) => { + Object.keys(obj).forEach((prop) => { if (!prop.startsWith('_')) { props.push(prop); } }); // getters const proto = Object.getPrototypeOf(obj); - Object.getOwnPropertyNames(proto).forEach((protoProp) => { + Object.keys(proto).forEach((protoProp) => { var desc = Object.getOwnPropertyDescriptor(proto, protoProp); if (!protoProp.startsWith('_') && desc && 'get' in desc) { props.push(protoProp);