refactor(ngcc): let `isWithinPackage` operate on paths instead of source files (#37596)
Changes `isWithinPackage` to take an `AbsoluteFsPath` instead of `ts.SourceFile`, to allow for an upcoming change to use it when no `ts.SourceFile` is available, but just a path. PR Close #37596
This commit is contained in:
parent
c29f830c4b
commit
6b565ba8f2
|
@ -12,7 +12,7 @@ import {ParsedConfiguration} from '../../..';
|
||||||
import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, ReferencesRegistry, ResourceLoader} from '../../../src/ngtsc/annotations';
|
import {ComponentDecoratorHandler, DirectiveDecoratorHandler, InjectableDecoratorHandler, NgModuleDecoratorHandler, PipeDecoratorHandler, ReferencesRegistry, ResourceLoader} from '../../../src/ngtsc/annotations';
|
||||||
import {CycleAnalyzer, ImportGraph} from '../../../src/ngtsc/cycles';
|
import {CycleAnalyzer, ImportGraph} from '../../../src/ngtsc/cycles';
|
||||||
import {isFatalDiagnosticError} from '../../../src/ngtsc/diagnostics';
|
import {isFatalDiagnosticError} from '../../../src/ngtsc/diagnostics';
|
||||||
import {absoluteFrom, dirname, FileSystem, LogicalFileSystem, resolve} from '../../../src/ngtsc/file_system';
|
import {absoluteFrom, absoluteFromSourceFile, dirname, FileSystem, LogicalFileSystem, resolve} from '../../../src/ngtsc/file_system';
|
||||||
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, NOOP_DEFAULT_IMPORT_RECORDER, PrivateExportAliasingHost, Reexport, ReferenceEmitter} from '../../../src/ngtsc/imports';
|
import {AbsoluteModuleStrategy, LocalIdentifierStrategy, LogicalProjectStrategy, ModuleResolver, NOOP_DEFAULT_IMPORT_RECORDER, PrivateExportAliasingHost, Reexport, ReferenceEmitter} from '../../../src/ngtsc/imports';
|
||||||
import {CompoundMetadataReader, CompoundMetadataRegistry, DtsMetadataReader, InjectableClassRegistry, LocalMetadataRegistry} from '../../../src/ngtsc/metadata';
|
import {CompoundMetadataReader, CompoundMetadataRegistry, DtsMetadataReader, InjectableClassRegistry, LocalMetadataRegistry} from '../../../src/ngtsc/metadata';
|
||||||
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
||||||
|
@ -148,7 +148,8 @@ export class DecorationAnalyzer {
|
||||||
*/
|
*/
|
||||||
analyzeProgram(): DecorationAnalyses {
|
analyzeProgram(): DecorationAnalyses {
|
||||||
for (const sourceFile of this.program.getSourceFiles()) {
|
for (const sourceFile of this.program.getSourceFiles()) {
|
||||||
if (!sourceFile.isDeclarationFile && isWithinPackage(this.packagePath, sourceFile)) {
|
if (!sourceFile.isDeclarationFile &&
|
||||||
|
isWithinPackage(this.packagePath, absoluteFromSourceFile(sourceFile))) {
|
||||||
this.compiler.analyzeFile(sourceFile);
|
this.compiler.analyzeFile(sourceFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
*/
|
*/
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
|
||||||
import {AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
import {absoluteFromSourceFile, AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
||||||
import {MetadataReader} from '../../../src/ngtsc/metadata';
|
import {MetadataReader} from '../../../src/ngtsc/metadata';
|
||||||
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
import {PartialEvaluator} from '../../../src/ngtsc/partial_evaluator';
|
||||||
import {ClassDeclaration, Decorator} from '../../../src/ngtsc/reflection';
|
import {ClassDeclaration, Decorator} from '../../../src/ngtsc/reflection';
|
||||||
|
@ -44,7 +44,7 @@ export class DefaultMigrationHost implements MigrationHost {
|
||||||
}
|
}
|
||||||
|
|
||||||
isInScope(clazz: ClassDeclaration): boolean {
|
isInScope(clazz: ClassDeclaration): boolean {
|
||||||
return isWithinPackage(this.entryPointPath, clazz.getSourceFile());
|
return isWithinPackage(this.entryPointPath, absoluteFromSourceFile(clazz.getSourceFile()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
* found in the LICENSE file at https://angular.io/license
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
import {AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
import {absoluteFromSourceFile, AbsoluteFsPath} from '../../../src/ngtsc/file_system';
|
||||||
import {NgccReflectionHost, SwitchableVariableDeclaration} from '../host/ngcc_host';
|
import {NgccReflectionHost, SwitchableVariableDeclaration} from '../host/ngcc_host';
|
||||||
import {isWithinPackage} from './util';
|
import {isWithinPackage} from './util';
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@ export class SwitchMarkerAnalyzer {
|
||||||
analyzeProgram(program: ts.Program): SwitchMarkerAnalyses {
|
analyzeProgram(program: ts.Program): SwitchMarkerAnalyses {
|
||||||
const analyzedFiles = new SwitchMarkerAnalyses();
|
const analyzedFiles = new SwitchMarkerAnalyses();
|
||||||
program.getSourceFiles()
|
program.getSourceFiles()
|
||||||
.filter(sourceFile => isWithinPackage(this.packagePath, sourceFile))
|
.filter(sourceFile => isWithinPackage(this.packagePath, absoluteFromSourceFile(sourceFile)))
|
||||||
.forEach(sourceFile => {
|
.forEach(sourceFile => {
|
||||||
const declarations = this.host.getSwitchableDeclarations(sourceFile);
|
const declarations = this.host.getSwitchableDeclarations(sourceFile);
|
||||||
if (declarations.length) {
|
if (declarations.length) {
|
||||||
|
|
|
@ -5,13 +5,11 @@
|
||||||
* Use of this source code is governed by an MIT-style license that can be
|
* 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
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import * as ts from 'typescript';
|
import {AbsoluteFsPath, relative} from '../../../src/ngtsc/file_system';
|
||||||
|
|
||||||
import {absoluteFromSourceFile, AbsoluteFsPath, relative} from '../../../src/ngtsc/file_system';
|
|
||||||
import {DependencyTracker} from '../../../src/ngtsc/incremental/api';
|
import {DependencyTracker} from '../../../src/ngtsc/incremental/api';
|
||||||
|
|
||||||
export function isWithinPackage(packagePath: AbsoluteFsPath, sourceFile: ts.SourceFile): boolean {
|
export function isWithinPackage(packagePath: AbsoluteFsPath, filePath: AbsoluteFsPath): boolean {
|
||||||
const relativePath = relative(packagePath, absoluteFromSourceFile(sourceFile));
|
const relativePath = relative(packagePath, filePath);
|
||||||
return !relativePath.startsWith('..') && !relativePath.startsWith('node_modules/');
|
return !relativePath.startsWith('..') && !relativePath.startsWith('node_modules/');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as ts from 'typescript';
|
import * as ts from 'typescript';
|
||||||
|
import {absoluteFromSourceFile} from '../../../src/ngtsc/file_system';
|
||||||
|
|
||||||
import {Logger} from '../../../src/ngtsc/logging';
|
import {Logger} from '../../../src/ngtsc/logging';
|
||||||
import {ClassDeclaration, ClassMember, ClassMemberKind, CtorParameter, Declaration, Decorator, EnumMember, isDecoratorIdentifier, isNamedClassDeclaration, isNamedFunctionDeclaration, isNamedVariableDeclaration, KnownDeclaration, reflectObjectLiteral, SpecialDeclarationKind, TypeScriptReflectionHost, TypeValueReference} from '../../../src/ngtsc/reflection';
|
import {ClassDeclaration, ClassMember, ClassMemberKind, CtorParameter, Declaration, Decorator, EnumMember, isDecoratorIdentifier, isNamedClassDeclaration, isNamedFunctionDeclaration, isNamedVariableDeclaration, KnownDeclaration, reflectObjectLiteral, SpecialDeclarationKind, TypeScriptReflectionHost, TypeValueReference} from '../../../src/ngtsc/reflection';
|
||||||
|
@ -2525,7 +2526,7 @@ function getRootFileOrFail(bundle: BundleProgram): ts.SourceFile {
|
||||||
function getNonRootPackageFiles(bundle: BundleProgram): ts.SourceFile[] {
|
function getNonRootPackageFiles(bundle: BundleProgram): ts.SourceFile[] {
|
||||||
const rootFile = bundle.program.getSourceFile(bundle.path);
|
const rootFile = bundle.program.getSourceFile(bundle.path);
|
||||||
return bundle.program.getSourceFiles().filter(
|
return bundle.program.getSourceFiles().filter(
|
||||||
f => (f !== rootFile) && isWithinPackage(bundle.package, f));
|
f => (f !== rootFile) && isWithinPackage(bundle.package, absoluteFromSourceFile(f)));
|
||||||
}
|
}
|
||||||
|
|
||||||
function isTopLevel(node: ts.Node): boolean {
|
function isTopLevel(node: ts.Node): boolean {
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
* Use of this source code is governed by an MIT-style license that can be
|
* 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
|
* found in the LICENSE file at https://angular.io/license
|
||||||
*/
|
*/
|
||||||
import * as ts from 'typescript';
|
|
||||||
import {absoluteFrom} from '../../../src/ngtsc/file_system';
|
import {absoluteFrom} from '../../../src/ngtsc/file_system';
|
||||||
import {runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
|
import {runInEachFileSystem} from '../../../src/ngtsc/file_system/testing';
|
||||||
import {isWithinPackage} from '../../src/analysis/util';
|
import {isWithinPackage} from '../../src/analysis/util';
|
||||||
|
@ -18,15 +17,13 @@ runInEachFileSystem(() => {
|
||||||
|
|
||||||
it('should return true if the source-file is contained in the package', () => {
|
it('should return true if the source-file is contained in the package', () => {
|
||||||
const packagePath = _('/node_modules/test');
|
const packagePath = _('/node_modules/test');
|
||||||
const file =
|
const file = _('/node_modules/test/src/index.js');
|
||||||
ts.createSourceFile(_('/node_modules/test/src/index.js'), '', ts.ScriptTarget.ES2015);
|
|
||||||
expect(isWithinPackage(packagePath, file)).toBe(true);
|
expect(isWithinPackage(packagePath, file)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return false if the source-file is not contained in the package', () => {
|
it('should return false if the source-file is not contained in the package', () => {
|
||||||
const packagePath = _('/node_modules/test');
|
const packagePath = _('/node_modules/test');
|
||||||
const file =
|
const file = _('/node_modules/other/src/index.js');
|
||||||
ts.createSourceFile(_('/node_modules/other/src/index.js'), '', ts.ScriptTarget.ES2015);
|
|
||||||
expect(isWithinPackage(packagePath, file)).toBe(false);
|
expect(isWithinPackage(packagePath, file)).toBe(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -34,13 +31,11 @@ runInEachFileSystem(() => {
|
||||||
const packagePath = _('/node_modules/test');
|
const packagePath = _('/node_modules/test');
|
||||||
|
|
||||||
// An external file inside the package's `node_modules/`.
|
// An external file inside the package's `node_modules/`.
|
||||||
const file1 = ts.createSourceFile(
|
const file1 = _('/node_modules/test/node_modules/other/src/index.js');
|
||||||
_('/node_modules/test/node_modules/other/src/index.js'), '', ts.ScriptTarget.ES2015);
|
|
||||||
expect(isWithinPackage(packagePath, file1)).toBe(false);
|
expect(isWithinPackage(packagePath, file1)).toBe(false);
|
||||||
|
|
||||||
// An internal file starting with `node_modules`.
|
// An internal file starting with `node_modules`.
|
||||||
const file2 = ts.createSourceFile(
|
const file2 = _('/node_modules/test/node_modules_optimizer.js');
|
||||||
_('/node_modules/test/node_modules_optimizer.js'), '', ts.ScriptTarget.ES2015);
|
|
||||||
expect(isWithinPackage(packagePath, file2)).toBe(true);
|
expect(isWithinPackage(packagePath, file2)).toBe(true);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue