mirror of
https://github.com/github/codeql-action.git
synced 2025-12-27 17:50:07 +08:00
Co-authored-by: Andrew Eisenberg <aeisenberg@github.com> Co-authored-by: Henry Mercer <henrymercer@github.com>
57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.MaybeOutput = exports.ConditionalOutput = void 0;
|
|
const Node_1 = require("./Node");
|
|
/**
|
|
* Helps output conditional helper methods.
|
|
*
|
|
* The `ConditionalOutput` concept is split into a usage site and a declaration
|
|
* site, i.e. declaring a `function someHelper() { ... }`, and calling it
|
|
* like `someHelper()`.
|
|
*
|
|
* While generating code, you can make usage says by using `someHelper` as
|
|
* a placeholder, and then output the declaration with `someHelper.ifUsed`
|
|
* to output the declaration conditionally only if `someHelper` has been
|
|
* seen in the tree.
|
|
*
|
|
* ```typescript
|
|
* const someHelper = conditionalOutput(
|
|
* "someHelper",
|
|
* code`function someHelper() { return 1 } `
|
|
* );
|
|
*
|
|
* const code = code`
|
|
* ${someHelper}
|
|
*
|
|
* ${someHelper.ifUsed}
|
|
* `
|
|
* ```
|
|
*/
|
|
class ConditionalOutput extends Node_1.Node {
|
|
// A given ConditionalOutput const could be used in multiple code
|
|
// parents, and so we don't want to use instance state to store
|
|
// "should I be output or not", b/c it depends on the containing tree.
|
|
constructor(usageSiteName, declarationSiteCode) {
|
|
super();
|
|
this.usageSiteName = usageSiteName;
|
|
this.declarationSiteCode = declarationSiteCode;
|
|
}
|
|
get childNodes() {
|
|
return [this.declarationSiteCode];
|
|
}
|
|
toCodeString() {
|
|
return this.usageSiteName;
|
|
}
|
|
get ifUsed() {
|
|
return new MaybeOutput(this, this.declarationSiteCode);
|
|
}
|
|
}
|
|
exports.ConditionalOutput = ConditionalOutput;
|
|
class MaybeOutput {
|
|
constructor(parent, code) {
|
|
this.parent = parent;
|
|
this.code = code;
|
|
}
|
|
}
|
|
exports.MaybeOutput = MaybeOutput;
|