|
1 | 1 | import Vue, { ComponentOptions } from 'vue'
|
2 | 2 | import { VueClass, DecoratedClass } from './declarations'
|
3 | 3 | import { collectDataFromConstructor } from './data'
|
| 4 | +import { hasProto, isPrimitive, warn } from './util' |
4 | 5 |
|
5 | 6 | export const $internalHooks = [
|
6 | 7 | 'data',
|
@@ -68,12 +69,85 @@ export function componentFactory (
|
68 | 69 | : Vue
|
69 | 70 | const Extended = Super.extend(options)
|
70 | 71 |
|
71 |
| - Object.getOwnPropertyNames(Component).forEach(key => { |
72 |
| - if (key !== 'prototype') { |
73 |
| - const descriptor = Object.getOwnPropertyDescriptor(Component, key)! |
74 |
| - Object.defineProperty(Extended, key, descriptor) |
75 |
| - } |
76 |
| - }) |
| 72 | + forwardStaticMembers(Extended, Component, Super) |
77 | 73 |
|
78 | 74 | return Extended
|
79 | 75 | }
|
| 76 | + |
| 77 | +const reservedPropertyNames = [ |
| 78 | + // Unique id |
| 79 | + 'cid', |
| 80 | + |
| 81 | + // Super Vue constructor |
| 82 | + 'super', |
| 83 | + |
| 84 | + // Component options that will be used by the component |
| 85 | + 'options', |
| 86 | + 'superOptions', |
| 87 | + 'extendOptions', |
| 88 | + 'sealedOptions', |
| 89 | + |
| 90 | + // Private assets |
| 91 | + 'component', |
| 92 | + 'directive', |
| 93 | + 'filter' |
| 94 | +] |
| 95 | + |
| 96 | +function forwardStaticMembers (Extended: typeof Vue, Original: typeof Vue, Super: typeof Vue): void { |
| 97 | + // We have to use getOwnPropertyNames since Babel registers methods as non-enumerable |
| 98 | + Object.getOwnPropertyNames(Original).forEach(key => { |
| 99 | + // `prototype` should not be overwritten |
| 100 | + if (key === 'prototype') { |
| 101 | + return |
| 102 | + } |
| 103 | + |
| 104 | + // Some browsers does not allow reconfigure built-in properties |
| 105 | + const extendedDescriptor = Object.getOwnPropertyDescriptor(Extended, key) |
| 106 | + if (extendedDescriptor && !extendedDescriptor.configurable) { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + const descriptor = Object.getOwnPropertyDescriptor(Original, key)! |
| 111 | + |
| 112 | + // If the user agent does not support `__proto__` or its family (IE <= 10), |
| 113 | + // the sub class properties may be inherited properties from the super class in TypeScript. |
| 114 | + // We need to exclude such properties to prevent to overwrite |
| 115 | + // the component options object which stored on the extended constructor (See #192). |
| 116 | + // If the value is a referenced value (object or function), |
| 117 | + // we can check equality of them and exclude it if they have the same reference. |
| 118 | + // If it is a primitive value, it will be forwarded for safety. |
| 119 | + if (!hasProto) { |
| 120 | + |
| 121 | + // Only `cid` is explicitly exluded from property forwarding |
| 122 | + // because we cannot detect whether it is a inherited property or not |
| 123 | + // on the no `__proto__` environment even though the property is reserved. |
| 124 | + if (key === 'cid') { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + const superDescriptor = Object.getOwnPropertyDescriptor(Super, key) |
| 129 | + |
| 130 | + if ( |
| 131 | + !isPrimitive(descriptor.value) |
| 132 | + && superDescriptor |
| 133 | + && superDescriptor.value === descriptor.value |
| 134 | + ) { |
| 135 | + return |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + // Warn if the users manually declare reserved properties |
| 140 | + if ( |
| 141 | + process.env.NODE_ENV !== 'production' |
| 142 | + && reservedPropertyNames.indexOf(key) >= 0 |
| 143 | + ) { |
| 144 | + warn( |
| 145 | + `Static property name '${key}' declared on class '${Original.name}' ` + |
| 146 | + 'conflicts with reserved property name of Vue internal. ' + |
| 147 | + 'It may cause unexpected behavior of the component. Consider renaming the property.' |
| 148 | + ) |
| 149 | + } |
| 150 | + |
| 151 | + Object.defineProperty(Extended, key, descriptor) |
| 152 | + }) |
| 153 | +} |
0 commit comments