JoostK 9186f1feea feat(compiler-cli): JIT compilation of directive declarations (#40101)
The `ɵɵngDeclareDirective` calls are designed to be translated to fully
AOT compiled code during a build transform, but in cases this is not
done it is still possible to compile the declaration object in the
browser using the JIT compiler. This commit adds a runtime
implementation of `ɵɵngDeclareDirective` which invokes the JIT compiler
using the declaration object, such that a compiled directive definition
is made available to the Ivy runtime.

PR Close #40101
2020-12-23 09:52:19 -08:00

57 lines
1.6 KiB
TypeScript

/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/**
* Jasmine matcher to verify that a function contains the provided code fragments.
*/
export function functionContaining(expectedFragments: Array<string|RegExp>):
jasmine.AsymmetricMatcher<Function> {
let _actual: Function|null = null;
const matches = (code: string, fragment: string|RegExp): boolean => {
if (typeof fragment === 'string') {
return code.includes(fragment);
} else {
return fragment.test(code);
}
};
return {
asymmetricMatch(actual: Function): boolean {
_actual = actual;
if (typeof actual !== 'function') {
return false;
}
const code = actual.toString();
for (const fragment of expectedFragments) {
if (!matches(code, fragment)) {
return false;
}
}
return true;
},
jasmineToString(): string {
if (typeof _actual !== 'function') {
return `Expected function to contain code fragments ${
jasmine.pp(expectedFragments)} but got ${jasmine.pp(_actual)}`;
}
const errors: string[] = [];
const code = _actual.toString();
errors.push(
`The actual function with code:\n${code}\n\ndid not contain the following fragments:`);
for (const fragment of expectedFragments) {
if (!matches(code, fragment)) {
errors.push(`- ${fragment}`);
}
}
return errors.join('\n');
}
};
}