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

fix: Improve fig spec with better generators #3762

Open
wants to merge 14 commits 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
3 changes: 3 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ["@fig/autocomplete"],
};
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ docs/registry.md
src/assets/bash_zsh_support/
tasks.md
tmp
xtasks/fig/src/mise.ts
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ function cliReference(commands: { [key: string]: Command }) {
x.collapsed = true;
x.items = Object.keys(command.subcommands)
.filter(
(subcommand) => command.subcommands![subcommand].hide !== true,
(subcommand) => command.subcommands![subcommand].hide !== true
Copy link
Owner

Choose a reason for hiding this comment

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

I'd prefer if we didn't need to enable es5 style everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point, will investigate if I can use package.json hierarchy to handle this

)
.map((subcommand) => ({
text: `mise ${name} ${subcommand}`,
Expand Down
2 changes: 1 addition & 1 deletion docs/settings.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default {
settings: [],
};
settings[key].settings.push(
buildElement(`${key}.${subkey}`, props[subkey]),
buildElement(`${key}.${subkey}`, props[subkey])
);
}
}
Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "echo \"Error: no test specified\" && exit 1",
"lint-fig": "eslint 'xtasks/fig/**/*.ts' && npx prettier --check 'xtasks/fig/**/*.ts' --parser typescript",
"lint-fig:fix": "eslint 'xtasks/fig/**/*.ts' --fix && npx prettier --write 'xtasks/fig/**/*.ts' --parser typescript"
},
"prettier": {
"trailingComma": "es5",
"printWidth": 80
},
"author": "",
"license": "ISC",
"devDependencies": {
"@fig/eslint-config-autocomplete": "^2.0.0",
"@tsconfig/node18": "^18.2.4",
"@withfig/autocomplete-tools": "^2.10.0",
"@withfig/autocomplete-types": "^1.31.0",
"eslint": "^8.57.0",
"toml": "^3.0.0",
"ts-pattern": "^5.4.0",
"tsx": "^4.19.1",
Expand Down
1 change: 1 addition & 0 deletions tasks.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ wait_for = ["render:completions"]
run = [
"usage generate fig --file mise.usage.kdl --out-file xtasks/fig/src/mise.ts",
"tsx xtasks/fig/addCustomGenerators.ts xtasks/fig/src/mise.ts xtasks/fig/src/mise.ts",
"bun run lint-fig:fix",
]
depends = ['docs:setup']

Expand Down
55 changes: 51 additions & 4 deletions xtasks/fig/addCustomGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ const customGenerators: GeneratorIdentifier[] = [
},
];

const get_object_literal_name = (node: ts.Node): string => {
if (node.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
throw "Not an Object Literal Expr";
}

const objectLiteralExpr = node as ts.ObjectLiteralExpression;
let name = "";

const properties = objectLiteralExpr.properties;
properties.forEach((p) => {
if (ts.isPropertyAssignment(p) && p.name.getText() == '"name"') {
const value = p.getChildAt(2);
name = value.getText().replaceAll('"', "");
}
});

return name;
};

const get_identifier = (node: ts.Node): ts.Identifier | undefined => {
let name = "";

Expand All @@ -79,6 +98,18 @@ const get_identifier = (node: ts.Node): ts.Identifier | undefined => {
return;
};

const has_property = (node: ts.Node, propertyName: string): boolean => {
if (node.kind !== ts.SyntaxKind.ObjectLiteralExpression) {
return false;
}

const objectLiteralExpr = node as ts.ObjectLiteralExpression;
const properties = objectLiteralExpr.properties;
return properties.some(
(p) => ts.isPropertyAssignment(p) && p.name.getText() === propertyName
);
};

function transformer<T extends ts.Node>(context: ts.TransformationContext) {
return (rootNode: T) => {
function visit(node: ts.Node): ts.Node {
Expand All @@ -91,7 +122,23 @@ function transformer<T extends ts.Node>(context: ts.TransformationContext) {
return ts.factory.updatePropertyAssignment(node, node.name, id);
}
}
return ts.visitEachChild(node, visit, context);
const newNode = ts.visitEachChild(node, visit, context);
if (
newNode &&
has_property(newNode, '"generators"') &&
!has_property(newNode, '"debounce"')
) {
const objLiteralExpr = newNode as ts.ObjectLiteralExpression;
const debounceProperty = ts.factory.createPropertyAssignment(
'"debounce"',
ts.factory.createIdentifier("true")
);
return ts.factory.updateObjectLiteralExpression(objLiteralExpr, [
...objLiteralExpr.properties,
debounceProperty,
]);
}
return newNode;
}
return ts.visitNode(rootNode, visit);
};
Expand All @@ -107,7 +154,7 @@ const main = async (fileName: string, outFile?: string) => {
"example.ts",
contents,
ts.ScriptTarget.Latest,
true,
true
);
const result = ts.transform(sourceFile, [transformer]);
const transformedSourceFile = result.transformed[0];
Expand All @@ -116,12 +163,12 @@ const main = async (fileName: string, outFile?: string) => {
const output = printer.printNode(
ts.EmitHint.Unspecified,
transformedSourceFile,
sourceFile,
sourceFile
);

fsAsync.writeFile(
outFile ?? `${fileName.replace(".ts", "")}.out.ts`,
generatorFileContents + "\n" + output,
generatorFileContents + "\n" + output
);
} catch (e) {
console.error(e);
Expand Down
Loading
Loading