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

Migration to TypeScript for specs #44

Merged
merged 21 commits into from
Mar 13, 2021
Merged

Migration to TypeScript for specs #44

merged 21 commits into from
Mar 13, 2021

Conversation

QuiiBz
Copy link
Contributor

@QuiiBz QuiiBz commented Mar 12, 2021

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
As in #43, I started a PR to add TypeScript support to Fig.

What is the current behavior? (You can also link to an open issue here)
Hard to write error-prone specs without IDEs auto-completion.

What is the new behavior (if this is a feature change)?
Provide types for the specs, allowing for awesome auto-completion and faster development of new specs.

Additional info:
I'm still working on it, so expect type bugs for now.

@QuiiBz
Copy link
Contributor Author

QuiiBz commented Mar 12, 2021

I've added TODO comments to remove lines/files when the migration to TS is ready.

The specs now compile to the dist folder, so I changed the copy.sh to include files in it. I've allowed JS along with TS, so JS specs are still working great.

The new command to "lint" the specs is typecheck. The old one test is still working, but only on .js files, instead of the new one which works on both JS and TS specs.

You can take a look at how to write specs in TS with the types here: 3b3d751#diff-6f6f4fc563a4439827032c3278ba487e9d4fa6c24a7ec4c6e41ca21005c726f4 Has you can see, it's very simple:

  • Rename the extension to .ts
  • Change the var completionSpec to const <specName>: Fig.Spec: This prevent errors from the TypeScript compiler to to find many variables with the same name (completionSpec).

And now you will get auto-completion and errors warning, thanks to the added type Spec from the Fig namespace.

I've also make sure that the existing JS specs still work great, along with added support to new/migrated specs in TS.

package.json Outdated Show resolved Hide resolved
schemas/fig.d.ts Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
@QuiiBz QuiiBz changed the title TypeScript Migration to TypeScript for specs Mar 12, 2021
@QuiiBz
Copy link
Contributor Author

QuiiBz commented Mar 12, 2021

The generated specs written TS are not working in fig: I found that changing the name of the spec (initially to completionSpec, changed to the name of the spec) make fig not using theses specs. I tried manually changing back to completionSpec and it work properly:
cd spec generated from TS, with name set to cd, not working:

var cd = {
    name: "cd",
    description: "Change the shell working directory",
    args: {
        template: "folders"
    }
};

After setting the name back to completionSpec, it work:

var completionSpec = {
    name: "cd",
    description: "Change the shell working directory",
    args: {
        template: "folders"
    }
};

We have two solutions:

  • Change the fig app to allow any variable name for the spec
  • Use the TypeScript compiler API to change the variable name back to completionSpec when compiling from TS to JS

EDIT
I wrote a basic transformer that successfully replaces the variable name with completionSpec:
Screenshot 2021-03-12 at 11 21 48
Let me know if you want to continue with this solution, or if you prefer changing the fig app to allow any variable name for the spec object.

EDIT
I just pushed the compiler, here is the commit: 66b3d28

compiler/compiler.ts Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
compiler/log.ts Outdated Show resolved Hide resolved
compiler/transformer.ts Outdated Show resolved Hide resolved
schemas/fig.d.ts Show resolved Hide resolved
schemas/fig.d.ts Outdated Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
compiler/compiler.ts Outdated Show resolved Hide resolved
@cstrnt cstrnt merged commit 26ce27b into withfig:master Mar 13, 2021
@QuiiBz QuiiBz mentioned this pull request Mar 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants