Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(@angular-devkit/build-optimizer): verify Program for type-checking passes #18481

Merged
merged 1 commit into from Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions etc/api/angular_devkit/build_optimizer/src/_golden-api.d.ts
Expand Up @@ -12,9 +12,9 @@ export declare function getPrefixClassesTransformer(): ts.TransformerFactory<ts.

export declare function getPrefixFunctionsTransformer(): ts.TransformerFactory<ts.SourceFile>;

export declare function getScrubFileTransformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
export declare function getScrubFileTransformer(program?: ts.Program): ts.TransformerFactory<ts.SourceFile>;

export declare function getScrubFileTransformerForCore(program: ts.Program): ts.TransformerFactory<ts.SourceFile>;
export declare function getScrubFileTransformerForCore(program?: ts.Program): ts.TransformerFactory<ts.SourceFile>;

export declare function getWrapEnumsTransformer(): ts.TransformerFactory<ts.SourceFile>;

Expand Down
Expand Up @@ -9,6 +9,7 @@ import { readFileSync } from 'fs';
import {
TransformJavascriptOptions,
TransformJavascriptOutput,
TransformerFactoryCreator,
transformJavascript,
} from '../helpers/transform-javascript';
import { getPrefixClassesTransformer, testPrefixClasses } from '../transforms/prefix-classes';
Expand Down Expand Up @@ -109,7 +110,7 @@ export function buildOptimizer(options: BuildOptimizerOptions): TransformJavascr
}

// Determine which transforms to apply.
const getTransforms = [];
const getTransforms: TransformerFactoryCreator[] = [];

let typeCheck = false;
if (options.isSideEffectFree || originalFilePath && isKnownSideEffectFree(originalFilePath)) {
Expand Down
Expand Up @@ -8,6 +8,9 @@
import { RawSourceMap } from 'source-map';
import * as ts from '../../third_party/github.com/Microsoft/TypeScript/lib/typescript';

export type TransformerFactoryCreator = (
program?: ts.Program,
) => ts.TransformerFactory<ts.SourceFile>;

export interface TransformJavascriptOptions {
content: string;
Expand All @@ -16,7 +19,7 @@ export interface TransformJavascriptOptions {
emitSourceMap?: boolean;
strict?: boolean;
typeCheck?: boolean;
getTransforms: Array<(program?: ts.Program) => ts.TransformerFactory<ts.SourceFile>>;
getTransforms: TransformerFactoryCreator[];
}

export interface TransformJavascriptOutput {
Expand Down Expand Up @@ -110,7 +113,8 @@ export function transformJavascript(
};
}

const transforms = getTransforms.map((getTf) => getTf(undefined));
// All fast path transformers do not use a program
const transforms = getTransforms.map((getTf) => getTf(/* program */ undefined));

const result = ts.transform(tempSourceFile, transforms, tsOptions);
if (result.transformed.length === 0 || result.transformed[0] === tempSourceFile) {
Expand Down
Expand Up @@ -20,17 +20,22 @@ export function testScrubFile(content: string) {
return markers.some((marker) => content.indexOf(marker) !== -1);
}

export function getScrubFileTransformer(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
return scrubFileTransformer(program.getTypeChecker(), false);
export function getScrubFileTransformer(program?: ts.Program): ts.TransformerFactory<ts.SourceFile> {
return scrubFileTransformer(program, false);
}

export function getScrubFileTransformerForCore(
program: ts.Program,
program?: ts.Program,
): ts.TransformerFactory<ts.SourceFile> {
return scrubFileTransformer(program.getTypeChecker(), true);
return scrubFileTransformer(program, true);
}

function scrubFileTransformer(checker: ts.TypeChecker, isAngularCoreFile: boolean) {
function scrubFileTransformer(program: ts.Program | undefined, isAngularCoreFile: boolean) {
if (!program) {
throw new Error('scrubFileTransformer requires a TypeScript Program.');
}
const checker = program.getTypeChecker();

return (context: ts.TransformationContext): ts.Transformer<ts.SourceFile> => {

const transformer: ts.Transformer<ts.SourceFile> = (sf: ts.SourceFile) => {
Expand Down