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

[dev-tool] Add snippets extraction #31143

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

mpodwysocki
Copy link
Contributor

Packages impacted by this PR

  • @azure/dev-tool

Issues associated with this PR

Describe the problem that is addressed by this PR

This PR implements a new dev-tool command: dev-tool run update-snippets.

This command looks for code fences in markdown files and JSDoc comments, and updates them with the contents of test methods in a file named snippets.spec.ts.

For example, the following fence indicates that the contents of a test named "new_configurationclient" should be used:

```js snippet:new_configurationclient
```

After running dev-tool run update-snippets, the contents of the snippet will be populated:

```js snippet:new_configurationclient
import { ConfigurationClient } from "@azure/template";
import { DefaultAzureCredential } from "@azure/identity";

const client = new ConfigurationClient(
  "<app configuration endpoint>",
  new DefaultAzureCredential()
);
```

To accomplish this, the command uses the TypeScript compiler API to extract and transpile snippets from snippets.spec.ts. Snippets are the contents of calls to the it function. If syntax with the shape it(<literal string>, <function with block>) appears in snippets.spec.ts, it will be considered a snippet that is valid for injection.

("Function with block" means either a function () { ... } expression or an arrow function with a block on the arrow side (() => { ... }). An arrow function that has an expression on the right hand side (() => (...)) will not be recognized.)

For example:

  it("new_configurationclient", function () {
    // @ts-ignore
    const client = new ConfigurationClient(
      process.env.ENDPOINT ?? "<app configuration endpoint>",
      new DefaultAzureCredential()
    );
  });

The transpiler automatically "cleans" and validates the snippet using similar techniques as the sample transpiler. As a result, it enforces the same syntactic rules that the sample transpiler does. In addition to those, it removes references to process.env (if an alternative is specified), removes compiler pragmas like // @ts-ignore, and automatically inserts imports for symbols that the snippet uses. So in the above snippet, imports for ConfigurationClient and DefaultAzureCredential are required, automatically detected, and injected into the resulting snippet.

Snippets without snippet:${name} tags are errors when using this command, so a package must be fully migrated to use it.

What are the possible designs available to address the problem? If there are more than one possible design, why was the one in this PR chosen?

Are there test cases added in this PR? (If not, why?)

Provide a list of related PRs (if any)

Command used to generate this PR:**(Applicable only to SDK release request PRs)

Checklists

  • Added impacted package name to the issue description
  • Does this PR needs any fixes in the SDK Generator?** (If so, create an Issue in the Autorest/typescript repository and link it here)
  • Added a changelog (if necessary)

@github-actions github-actions bot added dev-tool Issues related to the Azure SDK for JS dev-tool EngSys This issue is impacting the engineering system. labels Sep 17, 2024
@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dev-tool Issues related to the Azure SDK for JS dev-tool EngSys This issue is impacting the engineering system.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants