mirror of
https://github.com/angular/angular.git
synced 2026-12-06 13:19:15 -05:00
The `@babel/core` package provides the functionality of multiple other babel packages without the need to directly depend or import the other babel packages. Since the `@babel/core` package is already used and imported in the locations that previously used the other babel packages, an overall reduction in both imports and dependencies is possible. Six babel related packages were able to be removed from the root `package.json` and one (also present in the aforementioned six) was removed as a dependency from the `@angular/localize` package. Unfortunately, the functionality used from the `@babel/generator` package is not provided by `@babel/core` and is still present. Further refactoring may allow its removal as well in the future. The following packages were removed: * @babel/parser * @babel/template * @babel/traverse * @babel/types * @types/babel__template * @types/babel__traverse PR Close #53441
124 lines
4.6 KiB
TypeScript
124 lines
4.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
|
|
*/
|
|
|
|
import {NodePath, parse, traverse, types as t} from '@babel/core';
|
|
|
|
import {BabelDeclarationScope} from '../src/babel_declaration_scope';
|
|
|
|
describe('BabelDeclarationScope', () => {
|
|
describe('getConstantScopeRef()', () => {
|
|
it('should return a path to the ES module where the expression was imported', () => {
|
|
const ast = parse(
|
|
[
|
|
'import * as core from \'@angular/core\';',
|
|
'function foo() {',
|
|
' var TEST = core;',
|
|
'}',
|
|
].join('\n'),
|
|
{sourceType: 'module'}) as t.File;
|
|
const nodePath = findVarDeclaration(ast, 'TEST');
|
|
const scope = new BabelDeclarationScope(nodePath.scope);
|
|
const constantScope = scope.getConstantScopeRef(nodePath.get('init').node);
|
|
expect(constantScope).not.toBe(null);
|
|
expect(constantScope!.node).toBe(ast.program);
|
|
});
|
|
|
|
it('should return a path to the ES Module where the expression is declared', () => {
|
|
const ast = parse(
|
|
[
|
|
'var core;',
|
|
'export function foo() {',
|
|
' var TEST = core;',
|
|
'}',
|
|
].join('\n'),
|
|
{sourceType: 'module'}) as t.File;
|
|
const nodePath = findVarDeclaration(ast, 'TEST');
|
|
const scope = new BabelDeclarationScope(nodePath.scope);
|
|
const constantScope = scope.getConstantScopeRef(nodePath.get('init').node);
|
|
expect(constantScope).not.toBe(null);
|
|
expect(constantScope!.node).toBe(ast.program);
|
|
});
|
|
|
|
it('should return null if the file is not an ES module', () => {
|
|
const ast = parse(
|
|
[
|
|
'var core;',
|
|
'function foo() {',
|
|
' var TEST = core;',
|
|
'}',
|
|
].join('\n'),
|
|
{sourceType: 'script'}) as t.File;
|
|
const nodePath = findVarDeclaration(ast, 'TEST');
|
|
const scope = new BabelDeclarationScope(nodePath.scope);
|
|
const constantScope = scope.getConstantScopeRef(nodePath.get('init').node);
|
|
expect(constantScope).toBe(null);
|
|
});
|
|
|
|
it('should return the IIFE factory function where the expression is a parameter', () => {
|
|
const ast = parse(
|
|
[
|
|
'var core;',
|
|
'(function(core) {',
|
|
' var BLOCK = \'block\';',
|
|
' function foo() {',
|
|
' var TEST = core;',
|
|
' }',
|
|
'})(core);',
|
|
].join('\n'),
|
|
{sourceType: 'script'}) as t.File;
|
|
const nodePath = findVarDeclaration(ast, 'TEST');
|
|
const fnPath = findFirstFunction(ast);
|
|
const scope = new BabelDeclarationScope(nodePath.scope);
|
|
const constantScope = scope.getConstantScopeRef(nodePath.get('init').node);
|
|
expect(constantScope).not.toBe(null);
|
|
expect(constantScope!.isFunction()).toBe(true);
|
|
expect(constantScope!.node).toEqual(fnPath.node as t.FunctionDeclaration);
|
|
});
|
|
});
|
|
});
|
|
|
|
/**
|
|
* The type of a variable declarator that is known to have an initializer.
|
|
*
|
|
* Note: the `init` property is explicitly omitted to workaround a performance cliff in the
|
|
* TypeScript type checker.
|
|
*/
|
|
type InitializedVariableDeclarator = Omit<t.VariableDeclarator, 'init'>&{init: t.Expression};
|
|
|
|
function findVarDeclaration(
|
|
file: t.File, varName: string): NodePath<InitializedVariableDeclarator> {
|
|
let varDecl: NodePath<t.VariableDeclarator>|undefined = undefined;
|
|
traverse(file, {
|
|
VariableDeclarator: (path: NodePath<t.VariableDeclarator>) => {
|
|
const id = path.get('id');
|
|
if (id.isIdentifier() && id.node.name === varName && path.get('init') !== null) {
|
|
varDecl = path;
|
|
path.stop();
|
|
}
|
|
}
|
|
});
|
|
if (varDecl === undefined) {
|
|
throw new Error(`TEST BUG: expected to find variable declaration for ${varName}.`);
|
|
}
|
|
return varDecl;
|
|
}
|
|
|
|
function findFirstFunction(file: t.File): NodePath<t.Function> {
|
|
let fn: NodePath<t.Function>|undefined = undefined;
|
|
traverse(file, {
|
|
Function: (path) => {
|
|
fn = path;
|
|
path.stop();
|
|
}
|
|
});
|
|
if (fn === undefined) {
|
|
throw new Error(`TEST BUG: expected to find a function.`);
|
|
}
|
|
return fn;
|
|
}
|