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

Merged
merged 20 commits into from
Sep 20, 2024
Merged

[dev-tool] Add snippets extraction #31143

merged 20 commits into from
Sep 20, 2024

Conversation

mpodwysocki
Copy link
Member

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.

@mpodwysocki mpodwysocki requested a review from a team as a code owner September 20, 2024 16:31
Copy link
Member

@jeremymeng jeremymeng left a comment

Choose a reason for hiding this comment

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

Looks great for a v1!

Can you please log issue for potential improvements? For example,

  • centralized location to get the logger sample (maybe an option to specify package path override?)
  • move linting overrides to our eslint-plugin for the snippets file
  • possible to centralize the exclusion of snippets file in dev-tool/centralized config?

sdk/template/template/package.json Outdated Show resolved Hide resolved
Copy link
Member

@maorleger maorleger left a comment

Choose a reason for hiding this comment

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

Most of the changes look great and I love the improvement. I want to avoid ts-ignore everywhere if at all possible and I would love to be able to name the tests whatever I want. To that end, I suggested an alternative in how we define snippet blocks - curious what you think!

@mpodwysocki mpodwysocki merged commit 8597959 into main Sep 20, 2024
44 checks passed
@mpodwysocki mpodwysocki deleted the feat/snippets-extractor branch September 20, 2024 23:05
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.

4 participants