|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, salesforce.com, inc. |
| 3 | + * All rights reserved. |
| 4 | + * SPDX-License-Identifier: MIT |
| 5 | + * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT |
| 6 | + */ |
| 7 | +import { |
| 8 | + ArrayPush, |
| 9 | + ArraySplice, |
| 10 | + ArrayUnshift, |
| 11 | + ArrayCopyWithin, |
| 12 | + ArrayFill, |
| 13 | + ArrayPop, |
| 14 | + ArrayShift, |
| 15 | + ArraySort, |
| 16 | + ArrayReverse, |
| 17 | + defineProperty, |
| 18 | + getOwnPropertyDescriptor, |
| 19 | + isUndefined, |
| 20 | +} from '@lwc/shared'; |
| 21 | +import { logError } from '../shared/logger'; |
| 22 | +import { Template } from './template'; |
| 23 | +import { TemplateStylesheetFactories } from './stylesheet'; |
| 24 | + |
| 25 | +// See @lwc/engine-core/src/framework/template.ts |
| 26 | +const TEMPLATE_PROPS = ['slots', 'stylesheetToken', 'stylesheets', 'renderMode'] as const; |
| 27 | + |
| 28 | +// Via https://www.npmjs.com/package/object-observer |
| 29 | +const ARRAY_MUTATION_METHODS = [ |
| 30 | + 'pop', |
| 31 | + 'push', |
| 32 | + 'shift', |
| 33 | + 'unshift', |
| 34 | + 'reverse', |
| 35 | + 'sort', |
| 36 | + 'fill', |
| 37 | + 'splice', |
| 38 | + 'copyWithin', |
| 39 | +] as const; |
| 40 | + |
| 41 | +function getOriginalArrayMethod(prop: typeof ARRAY_MUTATION_METHODS[number]) { |
| 42 | + switch (prop) { |
| 43 | + case 'pop': |
| 44 | + return ArrayPop; |
| 45 | + case 'push': |
| 46 | + return ArrayPush; |
| 47 | + case 'shift': |
| 48 | + return ArrayShift; |
| 49 | + case 'unshift': |
| 50 | + return ArrayUnshift; |
| 51 | + case 'reverse': |
| 52 | + return ArrayReverse; |
| 53 | + case 'sort': |
| 54 | + return ArraySort; |
| 55 | + case 'fill': |
| 56 | + return ArrayFill; |
| 57 | + case 'splice': |
| 58 | + return ArraySplice; |
| 59 | + case 'copyWithin': |
| 60 | + return ArrayCopyWithin; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +let mutationWarningsSilenced = false; |
| 65 | + |
| 66 | +// Warn if the user tries to mutate tmpl.stylesheets, e.g.: |
| 67 | +// `tmpl.stylesheets.push(someStylesheetFunction)` |
| 68 | +function warnOnArrayMutation(stylesheets: TemplateStylesheetFactories) { |
| 69 | + // We can't handle users calling Array.prototype.slice.call(tmpl.stylesheets), but |
| 70 | + // we can at least warn when they use the most common mutation methods. |
| 71 | + for (const prop of ARRAY_MUTATION_METHODS) { |
| 72 | + const originalArrayMethod = getOriginalArrayMethod(prop); |
| 73 | + stylesheets[prop] = function arrayMutationWarningWrapper() { |
| 74 | + logError( |
| 75 | + `Mutating the "stylesheets" array on a template function ` + |
| 76 | + `is deprecated and may be removed in a future version of LWC.` |
| 77 | + ); |
| 78 | + // @ts-ignore |
| 79 | + return originalArrayMethod.apply(this, arguments); |
| 80 | + }; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +// TODO [#2782]: eventually freezeTemplate() will _actually_ freeze the tmpl object. Today it |
| 85 | +// just warns on mutation. |
| 86 | +export function freezeTemplate(tmpl: Template) { |
| 87 | + if (process.env.NODE_ENV !== 'production') { |
| 88 | + if (!isUndefined(tmpl.stylesheets)) { |
| 89 | + warnOnArrayMutation(tmpl.stylesheets); |
| 90 | + } |
| 91 | + for (const prop of TEMPLATE_PROPS) { |
| 92 | + let value = tmpl[prop]; |
| 93 | + defineProperty(tmpl, prop, { |
| 94 | + enumerable: true, |
| 95 | + configurable: true, |
| 96 | + get() { |
| 97 | + return value; |
| 98 | + }, |
| 99 | + set(newValue) { |
| 100 | + if (!mutationWarningsSilenced) { |
| 101 | + logError( |
| 102 | + `Dynamically setting the "${prop}" property on a template function ` + |
| 103 | + `is deprecated and may be removed in a future version of LWC.` |
| 104 | + ); |
| 105 | + } |
| 106 | + value = newValue; |
| 107 | + }, |
| 108 | + }); |
| 109 | + } |
| 110 | + |
| 111 | + const originalDescriptor = getOwnPropertyDescriptor(tmpl, 'stylesheetTokens'); |
| 112 | + defineProperty(tmpl, 'stylesheetTokens', { |
| 113 | + enumerable: true, |
| 114 | + configurable: true, |
| 115 | + get: originalDescriptor!.get, |
| 116 | + set(value) { |
| 117 | + logError( |
| 118 | + `Dynamically setting the "stylesheetTokens" property on a template function ` + |
| 119 | + `is deprecated and may be removed in a future version of LWC.` |
| 120 | + ); |
| 121 | + // Avoid logging twice (for both stylesheetToken and stylesheetTokens) |
| 122 | + mutationWarningsSilenced = true; |
| 123 | + originalDescriptor!.set!.call(this, value); |
| 124 | + mutationWarningsSilenced = false; |
| 125 | + }, |
| 126 | + }); |
| 127 | + } |
| 128 | +} |
0 commit comments