2020-10-03 14:59:53 -04:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
import * as o from '@angular/compiler/src/output/output_ast';
|
|
|
|
import * as ts from 'typescript';
|
|
|
|
|
2020-12-21 05:06:23 -05:00
|
|
|
import {MockFileSystemNative} from '../../../src/ngtsc/file_system/testing';
|
|
|
|
import {MockLogger} from '../../../src/ngtsc/logging/testing';
|
2020-10-03 14:59:53 -04:00
|
|
|
import {TypeScriptAstFactory} from '../../../src/ngtsc/translator';
|
|
|
|
import {AstHost} from '../../src/ast/ast_host';
|
|
|
|
import {TypeScriptAstHost} from '../../src/ast/typescript/typescript_ast_host';
|
|
|
|
import {DeclarationScope} from '../../src/file_linker/declaration_scope';
|
|
|
|
import {FileLinker} from '../../src/file_linker/file_linker';
|
|
|
|
import {LinkerEnvironment} from '../../src/file_linker/linker_environment';
|
|
|
|
import {DEFAULT_LINKER_OPTIONS} from '../../src/file_linker/linker_options';
|
|
|
|
import {PartialDirectiveLinkerVersion1} from '../../src/file_linker/partial_linkers/partial_directive_linker_1';
|
2020-12-21 05:06:23 -05:00
|
|
|
|
2020-10-03 14:59:53 -04:00
|
|
|
import {generate} from './helpers';
|
|
|
|
|
|
|
|
describe('FileLinker', () => {
|
|
|
|
let factory: TypeScriptAstFactory;
|
2021-02-26 14:55:19 -05:00
|
|
|
beforeEach(() => factory = new TypeScriptAstFactory(/* annotateForClosureCompiler */ false));
|
2020-10-03 14:59:53 -04:00
|
|
|
|
|
|
|
describe('isPartialDeclaration()', () => {
|
|
|
|
it('should return true if the callee is recognized', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
2020-11-03 15:33:57 -05:00
|
|
|
expect(fileLinker.isPartialDeclaration('ɵɵngDeclareDirective')).toBe(true);
|
|
|
|
expect(fileLinker.isPartialDeclaration('ɵɵngDeclareComponent')).toBe(true);
|
2020-10-03 14:59:53 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should return false if the callee is not recognized', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
|
|
|
expect(fileLinker.isPartialDeclaration('$foo')).toBe(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('linkPartialDeclaration()', () => {
|
|
|
|
it('should throw an error if the function name is not recognised', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
2020-11-25 16:03:02 -05:00
|
|
|
const version = factory.createLiteral('0.0.0-PLACEHOLDER');
|
2020-10-03 14:59:53 -04:00
|
|
|
const ngImport = factory.createIdentifier('core');
|
|
|
|
const declarationArg = factory.createObjectLiteral([
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
{propertyName: 'minVersion', quoted: false, value: version},
|
2020-10-03 14:59:53 -04:00
|
|
|
{propertyName: 'version', quoted: false, value: version},
|
|
|
|
{propertyName: 'ngImport', quoted: false, value: ngImport},
|
|
|
|
]);
|
|
|
|
expect(
|
|
|
|
() => fileLinker.linkPartialDeclaration(
|
|
|
|
'foo', [declarationArg], new MockDeclarationScope()))
|
|
|
|
.toThrowError('Unknown partial declaration function foo.');
|
|
|
|
});
|
|
|
|
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
it('should throw an error if the metadata object does not have a `minVersion` property', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
|
|
|
const version = factory.createLiteral('0.0.0-PLACEHOLDER');
|
|
|
|
const ngImport = factory.createIdentifier('core');
|
|
|
|
const declarationArg = factory.createObjectLiteral([
|
|
|
|
{propertyName: 'version', quoted: false, value: version},
|
|
|
|
{propertyName: 'ngImport', quoted: false, value: ngImport},
|
|
|
|
]);
|
|
|
|
expect(
|
|
|
|
() => fileLinker.linkPartialDeclaration(
|
|
|
|
'ɵɵngDeclareDirective', [declarationArg], new MockDeclarationScope()))
|
|
|
|
.toThrowError(`Expected property 'minVersion' to be present.`);
|
|
|
|
});
|
|
|
|
|
2020-10-03 14:59:53 -04:00
|
|
|
it('should throw an error if the metadata object does not have a `version` property', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
const version = factory.createLiteral('0.0.0-PLACEHOLDER');
|
2020-10-03 14:59:53 -04:00
|
|
|
const ngImport = factory.createIdentifier('core');
|
|
|
|
const declarationArg = factory.createObjectLiteral([
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
{propertyName: 'minVersion', quoted: false, value: version},
|
2020-10-03 14:59:53 -04:00
|
|
|
{propertyName: 'ngImport', quoted: false, value: ngImport},
|
|
|
|
]);
|
|
|
|
expect(
|
|
|
|
() => fileLinker.linkPartialDeclaration(
|
2020-11-03 15:33:57 -05:00
|
|
|
'ɵɵngDeclareDirective', [declarationArg], new MockDeclarationScope()))
|
2020-10-03 14:59:53 -04:00
|
|
|
.toThrowError(`Expected property 'version' to be present.`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should throw an error if the metadata object does not have a `ngImport` property', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
const version = factory.createLiteral('0.0.0-PLACEHOLDER');
|
2020-10-03 14:59:53 -04:00
|
|
|
const declarationArg = factory.createObjectLiteral([
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
{propertyName: 'minVersion', quoted: false, value: version},
|
|
|
|
{propertyName: 'version', quoted: false, value: version},
|
2020-10-03 14:59:53 -04:00
|
|
|
]);
|
|
|
|
expect(
|
|
|
|
() => fileLinker.linkPartialDeclaration(
|
2020-11-03 15:33:57 -05:00
|
|
|
'ɵɵngDeclareDirective', [declarationArg], new MockDeclarationScope()))
|
2020-10-03 14:59:53 -04:00
|
|
|
.toThrowError(`Expected property 'ngImport' to be present.`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should call `linkPartialDeclaration()` on the appropriate partial compiler', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
|
|
|
const compileSpy = spyOn(PartialDirectiveLinkerVersion1.prototype, 'linkPartialDeclaration')
|
|
|
|
.and.returnValue(o.literal('compilation result'));
|
|
|
|
|
|
|
|
const ngImport = factory.createIdentifier('core');
|
2020-11-25 16:03:02 -05:00
|
|
|
const version = factory.createLiteral('0.0.0-PLACEHOLDER');
|
2020-10-03 14:59:53 -04:00
|
|
|
const declarationArg = factory.createObjectLiteral([
|
|
|
|
{propertyName: 'ngImport', quoted: false, value: ngImport},
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
{propertyName: 'minVersion', quoted: false, value: version},
|
2020-10-03 14:59:53 -04:00
|
|
|
{propertyName: 'version', quoted: false, value: version},
|
|
|
|
]);
|
|
|
|
|
|
|
|
const compilationResult = fileLinker.linkPartialDeclaration(
|
2020-11-03 15:33:57 -05:00
|
|
|
'ɵɵngDeclareDirective', [declarationArg], new MockDeclarationScope());
|
2020-10-03 14:59:53 -04:00
|
|
|
|
|
|
|
expect(compilationResult).toEqual(factory.createLiteral('compilation result'));
|
|
|
|
expect(compileSpy).toHaveBeenCalled();
|
2020-12-21 05:06:23 -05:00
|
|
|
expect(compileSpy.calls.mostRecent().args[1].getNode('ngImport')).toBe(ngImport);
|
2020-10-03 14:59:53 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('getConstantStatements()', () => {
|
|
|
|
it('should capture shared constant values', () => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
|
|
|
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
|
|
|
|
|
|
|
|
// Here we use the `core` idenfifier for `ngImport` to trigger the use of a shared scope for
|
|
|
|
// constant statements.
|
|
|
|
const declarationArg = factory.createObjectLiteral([
|
|
|
|
{propertyName: 'ngImport', quoted: false, value: factory.createIdentifier('core')},
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
{
|
|
|
|
propertyName: 'minVersion',
|
|
|
|
quoted: false,
|
|
|
|
value: factory.createLiteral('0.0.0-PLACEHOLDER')
|
|
|
|
},
|
2020-11-25 16:03:02 -05:00
|
|
|
{propertyName: 'version', quoted: false, value: factory.createLiteral('0.0.0-PLACEHOLDER')},
|
2020-10-03 14:59:53 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const replacement = fileLinker.linkPartialDeclaration(
|
2020-11-03 15:33:57 -05:00
|
|
|
'ɵɵngDeclareDirective', [declarationArg], new MockDeclarationScope());
|
2020-10-03 14:59:53 -04:00
|
|
|
expect(generate(replacement)).toEqual('"REPLACEMENT"');
|
|
|
|
|
|
|
|
const results = fileLinker.getConstantStatements();
|
|
|
|
expect(results.length).toEqual(1);
|
|
|
|
const {constantScope, statements} = results[0];
|
|
|
|
expect(constantScope).toBe(MockConstantScopeRef.singleton);
|
|
|
|
expect(statements.map(generate)).toEqual(['const _c0 = [1];']);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should be no shared constant statements to capture when they are emitted into the replacement IIFE',
|
|
|
|
() => {
|
|
|
|
const {fileLinker} = createFileLinker();
|
|
|
|
spyOnLinkPartialDeclarationWithConstants(o.literal('REPLACEMENT'));
|
|
|
|
|
|
|
|
// Here we use a string literal `"not-a-module"` for `ngImport` to cause constant
|
|
|
|
// statements to be emitted in an IIFE rather than added to the shared constant scope.
|
|
|
|
const declarationArg = factory.createObjectLiteral([
|
|
|
|
{propertyName: 'ngImport', quoted: false, value: factory.createLiteral('not-a-module')},
|
refactor(compiler-cli): change how partial-linkers are matched to declaration versions (#41578)
Previously, it was not possible to block a partial-linker from trying to
process a declaration that was defined in a newer version of Angular than
that of the partial-linker. For example, if a partial-linker was published as
part of version 12.0.0, there was no way for a partially-compiled declaration
compiled via version 13.0.0 to tell the 12.0.0 linker that it would be invalid
to attempt to process it.
This commit adds a new `minVersion` property to partial-declarations, which is
interpreted as the "minimum partial-linker version" that can process this
declaration. When selecting a partial-linker for such a declaration, the known
linker version ranges are checked to find the most recent linker whose version
range has an overlap with the interpreted declaration range.
This approach allows us to set a minimum version for a declaration, which
can inform an old partial-linker that will it not be able to accurately
process the declaration.
Note that any pre-release part to versions are ignored in this selection
process.
The file-linker can be configured, via the `unknownDeclarationVersionHandling`
property of `LinkerOptions`, to handle such a situation in one of three ways:
- `error` - the version mismatch is a fatal error
- `warn` - a warning is sent to the logger but the most recent partial-linker
will attempt to process the declaration anyway.
- `ignore` - the most recent partial-linker will, silently, attempt to process
the declaration.
The default is to throw an error.
Closes #41497
PR Close #41578
2021-04-12 07:30:20 -04:00
|
|
|
{
|
|
|
|
propertyName: 'minVersion',
|
|
|
|
quoted: false,
|
|
|
|
value: factory.createLiteral('0.0.0-PLACEHOLDER')
|
|
|
|
},
|
2020-11-25 16:03:02 -05:00
|
|
|
{
|
|
|
|
propertyName: 'version',
|
|
|
|
quoted: false,
|
|
|
|
value: factory.createLiteral('0.0.0-PLACEHOLDER')
|
|
|
|
},
|
2020-10-03 14:59:53 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
const replacement = fileLinker.linkPartialDeclaration(
|
2020-11-03 15:33:57 -05:00
|
|
|
'ɵɵngDeclareDirective', [declarationArg], new MockDeclarationScope());
|
2020-10-03 14:59:53 -04:00
|
|
|
expect(generate(replacement))
|
|
|
|
.toEqual('function () { const _c0 = [1]; return "REPLACEMENT"; }()');
|
|
|
|
|
|
|
|
const results = fileLinker.getConstantStatements();
|
|
|
|
expect(results.length).toEqual(0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function createFileLinker(): {
|
|
|
|
host: AstHost<ts.Expression>,
|
|
|
|
fileLinker: FileLinker<MockConstantScopeRef, ts.Statement, ts.Expression>
|
|
|
|
} {
|
2020-12-21 05:06:23 -05:00
|
|
|
const fs = new MockFileSystemNative();
|
|
|
|
const logger = new MockLogger();
|
2020-10-03 14:59:53 -04:00
|
|
|
const linkerEnvironment = LinkerEnvironment.create<ts.Statement, ts.Expression>(
|
2021-02-26 14:55:19 -05:00
|
|
|
fs, logger, new TypeScriptAstHost(),
|
|
|
|
new TypeScriptAstFactory(/* annotateForClosureCompiler */ false), DEFAULT_LINKER_OPTIONS);
|
2020-10-03 14:59:53 -04:00
|
|
|
const fileLinker = new FileLinker<MockConstantScopeRef, ts.Statement, ts.Expression>(
|
2020-12-21 05:06:23 -05:00
|
|
|
linkerEnvironment, fs.resolve('/test.js'), '// test code');
|
2020-10-03 14:59:53 -04:00
|
|
|
return {host: linkerEnvironment.host, fileLinker};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This mock implementation of `DeclarationScope` will return a singleton instance of
|
|
|
|
* `MockConstantScopeRef` if the expression is an identifier, or `null` otherwise.
|
|
|
|
*
|
|
|
|
* This way we can simulate whether the constants will be shared or inlined into an IIFE.
|
|
|
|
*/
|
|
|
|
class MockDeclarationScope implements DeclarationScope<MockConstantScopeRef, ts.Expression> {
|
|
|
|
getConstantScopeRef(expression: ts.Expression): MockConstantScopeRef|null {
|
|
|
|
if (ts.isIdentifier(expression)) {
|
|
|
|
return MockConstantScopeRef.singleton;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockConstantScopeRef {
|
|
|
|
private constructor() {}
|
|
|
|
static singleton = new MockDeclarationScope();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Spy on the `PartialDirectiveLinkerVersion1.linkPartialDeclaration()` method, triggering
|
|
|
|
* shared constants to be created.
|
|
|
|
*/
|
|
|
|
function spyOnLinkPartialDeclarationWithConstants(replacement: o.Expression) {
|
|
|
|
let callCount = 0;
|
|
|
|
spyOn(PartialDirectiveLinkerVersion1.prototype, 'linkPartialDeclaration')
|
2020-12-21 05:06:23 -05:00
|
|
|
.and.callFake((constantPool => {
|
2020-10-03 14:59:53 -04:00
|
|
|
const constArray = o.literalArr([o.literal(++callCount)]);
|
|
|
|
// We have to add the constant twice or it will not create a shared statement
|
|
|
|
constantPool.getConstLiteral(constArray);
|
|
|
|
constantPool.getConstLiteral(constArray);
|
|
|
|
return replacement;
|
|
|
|
}) as typeof PartialDirectiveLinkerVersion1.prototype.linkPartialDeclaration);
|
|
|
|
}
|