-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] }; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?