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

feat(generators): add .generatorTag symbol to Generators #82

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions generators/src/filepaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ function filepathsFn(options: FilepathsOptions = {}): Fig.Generator {
};

return {
[Symbol.generatorTag]: "filepathsTemplate",
Copy link
Contributor Author

@fedeci fedeci Oct 28, 2022

Choose a reason for hiding this comment

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

NOTE: adding symbols properties to our objects makes a strong requirement for those creating tools relying on specs (e.g. AE, Dashboard) and running generators. They are basically a protocol that should be implemented.
e.g.

const generator: Fig.Generator = {
  [Symbol.generatorTag]: "name"
}
// if `Symbol.generatorTag` is not defined in the environment (see the ref PR in AE to understand how it is defined)
// the objects assumes the following shape
const generator: Fig.Generator = {
  undefined: "name"
}
// which means that if I try to access `undefined` I get "name"
generator[undefined] // "name"
generator["undefPropertyName"] // "name"


trigger: (oldToken, newToken) => {
const oldLastSlashIndex = oldToken.lastIndexOf("/");
const newLastSlashIndex = newToken.lastIndexOf("/");
Expand Down Expand Up @@ -189,8 +191,12 @@ function filepathsFn(options: FilepathsOptions = {}): Fig.Generator {
};
}

export const folders = Object.assign(
() => filepathsFn({ showFolders: "only" }),
Object.freeze(filepathsFn({ showFolders: "only" }))
);
function foldersFn(): Fig.Generator {
return {
...filepathsFn({ showFolders: "only" }),
[Symbol.generatorTag]: "foldersTemplate",
};
}

export const folders = Object.assign(foldersFn, Object.freeze(foldersFn()));
export const filepaths = Object.assign(filepathsFn, Object.freeze(filepathsFn()));
27 changes: 27 additions & 0 deletions types/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,16 @@
"hasDocComment": true,
"extends": [],
"members": [
{
"name": "[Symbol.generatorTag]",
"excluded": true,
"summary": "Assign a tag to generators so we can identify them",
"parameters": [],
"optional": true,
"declaration": "[Symbol.generatorTag]?: string",
"examples": [],
"hasDocComment": true
},
{
"name": "template",
"excluded": false,
Expand Down Expand Up @@ -1243,6 +1253,23 @@
}
],
"inheritedMembers": []
},
{
"name": "SymbolConstructor",
"hasDocComment": false,
"extends": [],
"members": [
{
"name": "generatorTag",
"excluded": false,
"parameters": [],
"optional": false,
"declaration": "readonly generatorTag: unique symbol",
"examples": [],
"hasDocComment": false
}
],
"inheritedMembers": []
}
],
"typeAliases": [
Expand Down
9 changes: 9 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,11 @@ declare namespace Fig {
*
*/
interface Generator {
/**
* Assign a tag to generators so we can identify them
* @excluded
*/
[Symbol.generatorTag]?: string;
/**
* A template which is a single `TemplateString` or an array of `TemplateStrings`.
*
Expand Down Expand Up @@ -1211,3 +1216,7 @@ declare namespace Fig {
cache?: Cache;
}
}

interface SymbolConstructor {
readonly generatorTag: unique symbol;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

An alternative name may be this one:

Suggested change
readonly generatorTag: unique symbol;
readonly generatorName: unique symbol;

}