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

Add SolutionBuilderHostBase.getCustomTransformers to be used when emitting. #44489

Merged
merged 1 commit into from Jun 7, 2021
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
7 changes: 4 additions & 3 deletions src/compiler/tsbuildPublic.ts
Expand Up @@ -85,6 +85,7 @@ namespace ts {
* writeFileCallback
*/
writeFile?(path: string, data: string, writeByteOrderMark?: boolean): void;
getCustomTransformers?: (project: string) => CustomTransformers | undefined;

getModifiedTime(fileName: string): Date | undefined;
setModifiedTime(fileName: string, date: Date): void;
Expand Down Expand Up @@ -785,7 +786,7 @@ namespace ts {
emit: (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => {
if (targetSourceFile || emitOnlyDtsFiles) {
return withProgramOrUndefined(
program => program.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers)
program => program.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers || state.host.getCustomTransformers?.(project))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without knowing anything about custom transformers or how they're actually used, this seems a little strange to me. If there's a way to pass them to emit, why doesn't the consumer do that, rather than embed them in the host? Why is it (potentially) called separately for each emit call? Can they change over time? If not, should they be stored somewhere? And why ||, rather than (e.g) ?.concat()?

I'm pretty sure this change accomplishes what it needs to accomplish, but it feels a little bit hastily done (assuming any of my intuition about what it's supposed to do is correct). Do you have a better understanding, @andrewbranch?

);
}
executeSteps(BuildStep.SemanticDiagnostics, cancellationToken);
Expand Down Expand Up @@ -921,7 +922,7 @@ namespace ts {
(name, text, writeByteOrderMark) => outputFiles.push({ name, text, writeByteOrderMark }),
cancellationToken,
/*emitOnlyDts*/ false,
customTransformers
customTransformers || state.host.getCustomTransformers?.(project)
);
// Don't emit .d.ts if there are decl file errors
if (declDiagnostics) {
Expand Down Expand Up @@ -1060,7 +1061,7 @@ namespace ts {
const refName = resolveProjectName(state, ref.path);
return parseConfigFile(state, refName, toResolvedConfigFilePath(state, refName));
},
customTransformers
customTransformers || state.host.getCustomTransformers?.(project)
);

if (isString(outputFiles)) {
Expand Down
1 change: 1 addition & 0 deletions src/testRunner/tsconfig.json
Expand Up @@ -141,6 +141,7 @@
"unittests/tsbuildWatch/noEmit.ts",
"unittests/tsbuildWatch/noEmitOnError.ts",
"unittests/tsbuildWatch/programUpdates.ts",
"unittests/tsbuildWatch/publicApi.ts",
"unittests/tsbuildWatch/reexport.ts",
"unittests/tsbuildWatch/watchEnvironment.ts",
"unittests/tsc/composite.ts",
Expand Down
115 changes: 115 additions & 0 deletions src/testRunner/unittests/tsbuildWatch/publicApi.ts
@@ -0,0 +1,115 @@
namespace ts.tscWatch {
it("unittests:: tsbuildWatch:: watchMode:: Public API with custom transformers", () => {
const solution: File = {
path: `${projectRoot}/tsconfig.json`,
content: JSON.stringify({
references: [
{ path: "./shared/tsconfig.json" },
{ path: "./webpack/tsconfig.json" }
],
files: []
})
};
const sharedConfig: File = {
path: `${projectRoot}/shared/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { composite: true },
})
};
const sharedIndex: File = {
path: `${projectRoot}/shared/index.ts`,
content: `export function f1() { }
export class c { }
export enum e { }
// leading
export function f2() { } // trailing`
};
const webpackConfig: File = {
path: `${projectRoot}/webpack/tsconfig.json`,
content: JSON.stringify({
compilerOptions: { composite: true, },
references: [{ path: "../shared/tsconfig.json" }]
})
};
const webpackIndex: File = {
path: `${projectRoot}/webpack/index.ts`,
content: `export function f2() { }
export class c2 { }
export enum e2 { }
// leading
export function f22() { } // trailing`
};
const commandLineArgs = ["--b", "--w"];
const { sys, baseline, oldSnap } = createBaseline(createWatchedSystem([libFile, solution, sharedConfig, sharedIndex, webpackConfig, webpackIndex], { currentDirectory: projectRoot }));
const { cb, getPrograms } = commandLineCallbacks(sys);
const buildHost = createSolutionBuilderWithWatchHost(
sys,
/*createProgram*/ undefined,
createDiagnosticReporter(sys, /*pretty*/ true),
createBuilderStatusReporter(sys, /*pretty*/ true),
createWatchStatusReporter(sys, /*pretty*/ true)
);
buildHost.afterProgramEmitAndDiagnostics = cb;
buildHost.afterEmitBundle = cb;
buildHost.getCustomTransformers = getCustomTransformers;
const builder = createSolutionBuilderWithWatch(buildHost, [solution.path], { verbose: true });
builder.build();
runWatchBaseline({
scenario: "publicApi",
subScenario: "with custom transformers",
commandLineArgs,
sys,
baseline,
oldSnap,
getPrograms,
changes: [
{
caption: "change to shared",
change: sys => sys.prependFile(sharedIndex.path, "export function fooBar() {}"),
timeouts: sys => {
sys.checkTimeoutQueueLengthAndRun(1); // Shared
sys.checkTimeoutQueueLengthAndRun(1); // webpack
sys.checkTimeoutQueueLengthAndRun(1); // solution
sys.checkTimeoutQueueLength(0);
}
}
],
watchOrSolution: builder
});

function getCustomTransformers(project: string): CustomTransformers {
const before: TransformerFactory<SourceFile> = context => {
return file => visitEachChild(file, visit, context);
function visit(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.FunctionDeclaration:
return visitFunction(node as FunctionDeclaration);
default:
return visitEachChild(node, visit, context);
}
}
function visitFunction(node: FunctionDeclaration) {
addSyntheticLeadingComment(node, SyntaxKind.MultiLineCommentTrivia, `@before${project}`, /*hasTrailingNewLine*/ true);
return node;
}
};

const after: TransformerFactory<SourceFile> = context => {
return file => visitEachChild(file, visit, context);
function visit(node: Node): VisitResult<Node> {
switch (node.kind) {
case SyntaxKind.VariableStatement:
return visitVariableStatement(node as VariableStatement);
default:
return visitEachChild(node, visit, context);
}
}
function visitVariableStatement(node: VariableStatement) {
addSyntheticLeadingComment(node, SyntaxKind.SingleLineCommentTrivia, `@after${project}`);
return node;
}
};
return { before: [before], after: [after] };
}
});
}
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Expand Up @@ -5240,6 +5240,7 @@ declare namespace ts {
* writeFileCallback
*/
writeFile?(path: string, data: string, writeByteOrderMark?: boolean): void;
getCustomTransformers?: (project: string) => CustomTransformers | undefined;
getModifiedTime(fileName: string): Date | undefined;
setModifiedTime(fileName: string, date: Date): void;
deleteFile(fileName: string): void;
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Expand Up @@ -5240,6 +5240,7 @@ declare namespace ts {
* writeFileCallback
*/
writeFile?(path: string, data: string, writeByteOrderMark?: boolean): void;
getCustomTransformers?: (project: string) => CustomTransformers | undefined;
getModifiedTime(fileName: string): Date | undefined;
setModifiedTime(fileName: string, date: Date): void;
deleteFile(fileName: string): void;
Expand Down