/
environment.ts
132 lines (105 loc) · 3.78 KB
/
environment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { ENV } from '@ember/-internals/environment';
import { _getProp, _setProp, get, set } from '@ember/-internals/metal';
import { Owner } from '@ember/-internals/owner';
import { getDebugName } from '@ember/-internals/utils';
import { constructStyleDeprecationMessage } from '@ember/-internals/views';
import { assert, deprecate, warn } from '@ember/debug';
import { DeprecationOptions } from '@ember/debug/lib/deprecate';
import { _backburner, schedule } from '@ember/runloop';
import { DEBUG } from '@glimmer/env';
import setGlobalContext from '@glimmer/global-context';
import { EnvironmentDelegate } from '@glimmer/runtime';
import { setTrackingTransactionEnv } from '@glimmer/validator';
import toIterator from './utils/iterator';
import { isHTMLSafe } from './utils/string';
import toBool from './utils/to-bool';
///////////
// Setup global context
setGlobalContext({
scheduleRevalidate() {
_backburner.ensureInstance();
},
toBool,
toIterator,
getProp: _getProp,
setProp: _setProp,
getPath: get,
setPath: set,
scheduleDestroy(destroyable, destructor) {
schedule('actions', null, destructor, destroyable);
},
scheduleDestroyed(finalizeDestructor) {
schedule('destroy', null, finalizeDestructor);
},
warnIfStyleNotTrusted(value: unknown) {
warn(
constructStyleDeprecationMessage(value),
(() => {
if (value === null || value === undefined || isHTMLSafe(value)) {
return true;
}
return false;
})(),
{ id: 'ember-htmlbars.style-xss-warning' }
);
},
assert(test: unknown, msg: string, options?: { id: string }) {
if (DEBUG) {
let id = options?.id;
let override = VM_ASSERTION_OVERRIDES.filter((o) => o.id === id)[0];
assert(override?.message ?? msg, test);
}
},
deprecate(msg: string, test: unknown, options: { id: string }) {
if (DEBUG) {
let { id } = options;
if (id === 'argument-less-helper-paren-less-invocation') {
throw new Error(
`A resolved helper cannot be passed as a named argument as the syntax is ` +
`ambiguously a pass-by-reference or invocation. Use the ` +
`\`{{helper 'foo-helper}}\` helper to pass by reference or explicitly ` +
`invoke the helper with parens: \`{{(fooHelper)}}\`.`
);
}
let override = VM_DEPRECATION_OVERRIDES.filter((o) => o.id === id)[0];
if (!override) throw new Error(`deprecation override for ${id} not found`);
// allow deprecations to be disabled in the VM_DEPRECATION_OVERRIDES array below
if (!override.disabled) {
deprecate(override.message ?? msg, Boolean(test), override);
}
}
},
});
if (DEBUG) {
setTrackingTransactionEnv?.({
debugMessage(obj, keyName) {
let dirtyString = keyName
? `\`${keyName}\` on \`${getDebugName?.(obj)}\``
: `\`${getDebugName?.(obj)}\``;
return `You attempted to update ${dirtyString}, but it had already been used previously in the same computation. Attempting to update a value after using it in a computation can cause logical errors, infinite revalidation bugs, and performance issues, and is not supported.`;
},
});
}
///////////
// VM Assertion/Deprecation overrides
const VM_DEPRECATION_OVERRIDES: (DeprecationOptions & {
disabled?: boolean;
message?: string;
})[] = [
{
id: 'setting-on-hash',
until: '4.4.0',
for: 'ember-source',
since: {
enabled: '3.28.0',
},
},
];
const VM_ASSERTION_OVERRIDES: { id: string; message: string }[] = [];
///////////
// Define environment delegate
export class EmberEnvironmentDelegate implements EnvironmentDelegate {
public enableDebugTooling: boolean = ENV._DEBUG_RENDER_TREE;
constructor(public owner: Owner, public isInteractive: boolean) {}
onTransactionCommit(): void {}
}