From e50beb18eac0d2e3b973551d2d536ff4bd45eb69 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 4 Dec 2023 12:38:50 -0800 Subject: [PATCH 01/11] Update basics.md Updates typos and inaccuracies in the Creating TypeSpec libraries doc --- docs/extending-typespec/basics.md | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index ad4ece1872..ac17ff747d 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -118,7 +118,7 @@ export { $lib } from "./lib.js"; ### 6. Build TypeScript -TypeSpec can only import JavaScript files, so any time changes are made to TypeScript sources, they need to be compiled before they are visible to TypeSpec. To do so, run `npx tsc -p .` in your library's root directory. You can also run `npx tsc -p --watch` if you would like to re-run the TypeScript compiler whenever files are changed. +TypeSpec can only import JavaScript files, so any time changes are made to TypeScript sources, they need to be compiled before they are visible to TypeSpec. To do so, run `npx tsc -p .` in your library's root directory. You can also run `npx tsc -p . --watch` if you would like to re-run the TypeScript compiler whenever files are changed. ### 7. Add your main TypeSpec file @@ -205,13 +205,31 @@ TypeSpec libraries can contain more than just types. Read the subsequent topics TypeSpec provides a testing framework to help testing libraries. Examples here are shown using `mocha` but any other JS test framework can be used. +### Add devDependencies + +Verify that you have the following in your `package.json`: + +``` + ..., + "devDependencies": { + "@types/node": "~18.11.9", + "@types/mocha": "~10.0.1", // if you want to include the mocha test framework + ... + } + ... +``` + ### Define the testing library -First step is to define how your library can be loaded from the test framework. This will let your library to be reused by other library test. +The first step is to define how your library can be loaded from the test framework. This will let your library to be reused by other library test. 1. Create a new file `./src/testing/index.ts` with the following content ```ts +import { resolvePath } from "@typespec/compiler"; +import { createTestLibrary, TypeSpecTestLibrary } from "@typespec/compiler/testing"; +import { fileURLToPath } from "url"; + export const MyTestLibrary = createTestLibrary({ name: "", // Set this to the absolute path to the root of the package. (e.g. in this case this file would be compiled to ./dist/src/testing/index.js) @@ -240,9 +258,9 @@ export const MyTestLibrary = createTestLibrary({ ### Define the test host and test runner for your library -Define some of the test framework base pieces that will be used in the tests. There is 2 functions: +Define some of the test framework base pieces that will be used in the tests. There are 2 functions: -- `createTestHost`: This is a lower level api that provide a virtual file system. +- `createTestHost`: This is a lower level API that provides a virtual file system. - `createTestRunner`: This is a wrapper on top of the test host that will automatically add a `main.tsp` file and automatically import libraries. Create a new file `test/test-host.js` (change `test` to be your test folder) @@ -258,7 +276,7 @@ export async function createMyTestHost() { }); } export async function createMyTestRunner() { - const host = await createOpenAPITestHost(); + const host = await createMyTestHost(); return createTestWrapper(host, { autoUsings: ["My"] }); } ``` From ea01089b771c3206a5b34833dbea66b642c1c572 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 4 Dec 2023 12:51:44 -0800 Subject: [PATCH 02/11] Code review feedback. --- docs/extending-typespec/basics.md | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index ac17ff747d..16e24acec8 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -60,7 +60,7 @@ See [dependency section](#defining-dependencies) for information on how to defin Your package.json needs to refer to two main files: your node module main file, and your TypeSpec main. The node module main file is the `"main"` key in your package.json file, and defines the entrypoint for your library when consumed as a node library, and must reference a js file. The TypeSpec main defines the entrypoint for your library when consumed from a TypeSpec program, and may reference either a js file (when your library doesn't contain any typespec types) or a TypeSpec file. ```json - "main": "dist/index.js", + "main": "dist/src/index.js", "tspMain": "lib/main.tsp" ``` @@ -79,7 +79,7 @@ This will create `tsconfig.json`. But we need to make a couple changes to this. "module": "Node16", // This and next setting tells TypeScript to use the new ESM import system to resolve types. "moduleResolution": "Node16", "target": "es2019", - "rootDir": "./src", + "rootDir": ".", "outDir": "./dist", ``` @@ -213,7 +213,12 @@ Verify that you have the following in your `package.json`: ..., "devDependencies": { "@types/node": "~18.11.9", - "@types/mocha": "~10.0.1", // if you want to include the mocha test framework + ..., + // if you want to include the mocha test framework + "@types/mocha": "~10.0.1", + "mocha": "~10.2.0", + "mocha-junit-reporter": "~2.2.1", + "mocha-multi-reporters": "~1.5.1", ... } ... @@ -227,7 +232,7 @@ The first step is to define how your library can be loaded from the test framewo ```ts import { resolvePath } from "@typespec/compiler"; -import { createTestLibrary, TypeSpecTestLibrary } from "@typespec/compiler/testing"; +import { createTestLibrary } from "@typespec/compiler/testing"; import { fileURLToPath } from "url"; export const MyTestLibrary = createTestLibrary({ From 489c28191136f5c41d8506f67967fb472ad14ae6 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 4 Dec 2023 13:19:38 -0800 Subject: [PATCH 03/11] Fix async references. --- docs/extending-typespec/basics.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index 16e24acec8..4288541eb1 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -301,16 +301,16 @@ describe("my library", () => { }); // Check everything works fine - it("does this", () => { - const { Foo } = runner.compile(` + it("does this", async () => { + const { Foo } = await runner.compile(` @test model Foo {} `); strictEqual(Foo.kind, "Model"); }); // Check diagnostics are emitted - it("errors", () => { - const diagnostics = runner.diagnose(` + it("errors", async () => { + const diagnostics = await runner.diagnose(` model Bar {} `); expectDiagnostics(diagnostics, { code: "...", message: "..." }); @@ -324,7 +324,7 @@ The `@test` decorator is a decorator loaded in the test environment. It can be u When using the `compile` method it will return a `Record` which is a map of all the types annoted with the `@test` decorator. ```ts -const { Foo, CustomName } = runner.compile(` +const { Foo, CustomName } = await runner.compile(` @test model Foo {} model Bar { From 2ba027a95c0ff8d05ae307ee330397571df00ead Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 4 Dec 2023 13:58:53 -0800 Subject: [PATCH 04/11] Fix tsconfig.json settings --- docs/extending-typespec/basics.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index 4288541eb1..45ec921c67 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -76,11 +76,11 @@ npx tsc --init --strict This will create `tsconfig.json`. But we need to make a couple changes to this. Open `tsconfig.json` and set the following settings: ```json - "module": "Node16", // This and next setting tells TypeScript to use the new ESM import system to resolve types. - "moduleResolution": "Node16", - "target": "es2019", - "rootDir": ".", - "outDir": "./dist", +"module": "Node16", // This and next setting tells TypeScript to use the new ESM import system to resolve types. +"moduleResolution": "Node16", +"target": "es2019", +"rootDir": ".", +"outDir": "./dist", ``` ### 4. Create `lib.ts` From 942cbccb173951c40b7c7f658c291f9bdf7d66a3 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 4 Dec 2023 14:20:02 -0800 Subject: [PATCH 05/11] Update json to jsonc --- docs/extending-typespec/basics.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index 45ec921c67..75811fb948 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -39,7 +39,7 @@ After filling out the wizard, you will have a package.json file that defines you Unlike node libraries which support CommonJS (cjs), TypeSpec libraries must be Ecmascript Modules. So open your `package.json` and add the following top-level configuration key: -```json +```jsonc "type": "module" ``` @@ -59,7 +59,7 @@ See [dependency section](#defining-dependencies) for information on how to defin Your package.json needs to refer to two main files: your node module main file, and your TypeSpec main. The node module main file is the `"main"` key in your package.json file, and defines the entrypoint for your library when consumed as a node library, and must reference a js file. The TypeSpec main defines the entrypoint for your library when consumed from a TypeSpec program, and may reference either a js file (when your library doesn't contain any typespec types) or a TypeSpec file. -```json +```jsonc "main": "dist/src/index.js", "tspMain": "lib/main.tsp" ``` @@ -75,7 +75,7 @@ npx tsc --init --strict This will create `tsconfig.json`. But we need to make a couple changes to this. Open `tsconfig.json` and set the following settings: -```json +```jsonc "module": "Node16", // This and next setting tells TypeScript to use the new ESM import system to resolve types. "moduleResolution": "Node16", "target": "es2019", @@ -244,7 +244,7 @@ export const MyTestLibrary = createTestLibrary({ 2. Add an `exports` for the `testing` endpoint to `package.json` (update with correct paths) -```json +```jsonc { // ... "main": "dist/src/index.js", From 576cef0166bc64f7861cf649ed5637259b060d75 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 4 Dec 2023 16:02:18 -0800 Subject: [PATCH 06/11] More updates. --- docs/extending-typespec/basics.md | 140 +++++++++++++++++------------- 1 file changed, 81 insertions(+), 59 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index 75811fb948..1e858d3d69 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -23,9 +23,9 @@ The following is a high level overview of the contents of a TypeSpec package. Th - **src/lib.ts** - the TypeSpec library definition file - **package.json** - metadata about your TypeSpec package -## Initial setup +## 1 - Initial setup -### 1. Initialize your package directory & package.json +### a. Initialize your package directory & package.json Run the following commands: @@ -37,13 +37,13 @@ Run the following commands: After filling out the wizard, you will have a package.json file that defines your typespec library. -Unlike node libraries which support CommonJS (cjs), TypeSpec libraries must be Ecmascript Modules. So open your `package.json` and add the following top-level configuration key: +Unlike node libraries which support CommonJS (cjs), TypeSpec libraries must be Ecmascript Modules. Open your `package.json` and add the following top-level configuration key: ```jsonc "type": "module" ``` -### 2. Install TypeSpec dependencies +### b. Install TypeSpec dependencies Run the following command: @@ -51,11 +51,11 @@ Run the following command: npm install --save-peer @typespec/compiler ``` -You may have need of other dependencies in the TypeSpec standard library depending on what you are doing. E.g. if you want to use the metadata found in `@typespec/openapi` you will need to install that as well. +You may have need of other dependencies in the TypeSpec standard library depending on what you are doing (e.g. if you want to use the metadata found in `@typespec/openapi` you will need to install that as well). See [dependency section](#defining-dependencies) for information on how to define your dependencies. -### 2. Define your main files +### c. Define your main files Your package.json needs to refer to two main files: your node module main file, and your TypeSpec main. The node module main file is the `"main"` key in your package.json file, and defines the entrypoint for your library when consumed as a node library, and must reference a js file. The TypeSpec main defines the entrypoint for your library when consumed from a TypeSpec program, and may reference either a js file (when your library doesn't contain any typespec types) or a TypeSpec file. @@ -64,7 +64,7 @@ Your package.json needs to refer to two main files: your node module main file, "tspMain": "lib/main.tsp" ``` -### 3. Install and initialize TypeScript +### d. Install and initialize TypeScript Run the following commands: @@ -81,9 +81,10 @@ This will create `tsconfig.json`. But we need to make a couple changes to this. "target": "es2019", "rootDir": ".", "outDir": "./dist", +"sourceMap": true, ``` -### 4. Create `lib.ts` +### e. Create `lib.ts` Open `./src/lib.ts` and create your library definition that registers your library with the TypeSpec compiler and defines any diagnostics your library will emit. Make sure to export the library definition as `$lib`. @@ -107,7 +108,7 @@ export const { reportDiagnostic, createDiagnostic, createStateSymbol } = $lib; Diagnostics are used for linters and decorators which are covered in subsequent topics. -### 5. Create `index.ts` +### f. Create `index.ts` Open `./src/index.ts` and import your library definition: @@ -116,11 +117,24 @@ Open `./src/index.ts` and import your library definition: export { $lib } from "./lib.js"; ``` -### 6. Build TypeScript +### g. Build TypeScript TypeSpec can only import JavaScript files, so any time changes are made to TypeScript sources, they need to be compiled before they are visible to TypeSpec. To do so, run `npx tsc -p .` in your library's root directory. You can also run `npx tsc -p . --watch` if you would like to re-run the TypeScript compiler whenever files are changed. -### 7. Add your main TypeSpec file +Alternatively, you can add these as scripts in your `package.json` to make them easier to invoke. Consider adding the following: + +```jsonc + "scripts": { + "clean": "rimraf ./dist ./temp", + "build": "tsc -p .", + "watch": "tsc -p . --watch", + "test": "mocha" + } +``` + +You can then run `npm run build` or `npm run watch` to build or watch your library. + +### h. Add your main TypeSpec file Open `./lib/main.tsp` and import your JS entrypoint. This ensures that when typespec imports your library, the code to define the library is run. In later topics when we add decorators, this import will ensure those get exposed as well. @@ -128,7 +142,7 @@ Open `./lib/main.tsp` and import your JS entrypoint. This ensures that when type import "../dist/index.js"; ``` -## Adding TypeSpec types to your library +## 2. Adding TypeSpec types to your library Open `./lib/main.tsp` and add any types you want to be available when users import this library. It is also strongly recommended you put these types in a namespace that corresponds with the library name. For example, your `./lib/main.tsp` file might look like: @@ -142,7 +156,7 @@ model Person { } ``` -## Defining Dependencies +## 3. Defining Dependencies Defining dependencies in a TypeSpec library should be following these rules: @@ -174,57 +188,24 @@ TypeSpec libraries are defined using `peerDependencies` so we don't end-up with } ``` -## Publishing your TypeSpec library - -To publish to the public npm registry, follow [their documentation](https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages). - -## Importing your TypeSpec library - -Once your TypeSpec library is published, your users can install and use it just like any of the TypeSpec standard libraries. First, they have to install it: - -```bash -npm install $packageName -``` - -Next, they import it into their TypeSpec program and use the namespace (if desired): - -```typespec -import "MyLibrary"; -using MyLibrary; - -model Employee extends Person { - job: string; -} -``` - -## Next steps - -TypeSpec libraries can contain more than just types. Read the subsequent topics for more details on how to write [decorators](./create-decorators.md), [emitters](./emitters-basics.md) and [linters](./linters.md). - -## Testing +## 4. Testing your TypeSpec library TypeSpec provides a testing framework to help testing libraries. Examples here are shown using `mocha` but any other JS test framework can be used. -### Add devDependencies +### a. Add devDependencies Verify that you have the following in your `package.json`: ``` - ..., - "devDependencies": { - "@types/node": "~18.11.9", - ..., - // if you want to include the mocha test framework - "@types/mocha": "~10.0.1", - "mocha": "~10.2.0", - "mocha-junit-reporter": "~2.2.1", - "mocha-multi-reporters": "~1.5.1", - ... - } - ... +"devDependencies": { + "@types/node": "~18.11.9", + "@types/mocha": "~10.0.1", + "mocha": "~10.2.0", + "source-map-support": "^0.5.21" +} ``` -### Define the testing library +### b. Define the testing library The first step is to define how your library can be loaded from the test framework. This will let your library to be reused by other library test. @@ -261,7 +242,7 @@ export const MyTestLibrary = createTestLibrary({ } ``` -### Define the test host and test runner for your library +### c. Define the test host and test runner for your library Define some of the test framework base pieces that will be used in the tests. There are 2 functions: @@ -286,9 +267,9 @@ export async function createMyTestRunner() { } ``` -### Write tests +### d. Write tests -After setting up that infrastructure you can start writing tests. +After setting up that infrastructure you can start writing tests. For tests to be recognized by mocha the file names must follow the following format: `.test.ts` ```ts import { createMyTestRunner } from "./test-host.js"; @@ -318,7 +299,7 @@ describe("my library", () => { }); ``` -#### `@test` decorator +#### e. `@test` decorator The `@test` decorator is a decorator loaded in the test environment. It can be used to collect any decorable type. When using the `compile` method it will return a `Record` which is a map of all the types annoted with the `@test` decorator. @@ -335,3 +316,44 @@ const { Foo, CustomName } = await runner.compile(` Foo; // type of: model Foo {} CustomName; // type of : Bar.name ``` + +#### f. Install the mocha test explorer for VSCode (optional) + +If you are using VSCode, you can install the [mocha test explorer](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-mocha-test-adapter) to run your tests from the editor. This will also allow you easily debug into your tests. + +After installing the extension, you need to add a `.mocharc.yaml` file at the root of your project. + +```yaml +timeout: 5000 +require: source-map-support/register +spec: "dist/test/**/*.test.js" +``` + +You should now be able to discover, run and debug into your tests from the test explorer. + +## 5. Publishing your TypeSpec library + +To publish to the public npm registry, follow [their documentation](https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages). + +## 6. Importing your TypeSpec library + +Once your TypeSpec library is published, your users can install and use it just like any of the TypeSpec standard libraries. First, they have to install it: + +```bash +npm install $packageName +``` + +Next, they import it into their TypeSpec program and use the namespace (if desired): + +```typespec +import "MyLibrary"; +using MyLibrary; + +model Employee extends Person { + job: string; +} +``` + +## 7. Next steps + +TypeSpec libraries can contain more than just types. Read the subsequent topics for more details on how to write [decorators](./create-decorators.md), [emitters](./emitters-basics.md) and [linters](./linters.md). From 71a704cf27c3a6d4ebc9a05f7d20e7262154a2e6 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 5 Dec 2023 09:32:23 -0800 Subject: [PATCH 07/11] Fix incorrect method name. --- cspell.yaml | 1 + docs/extending-typespec/emitter-framework.md | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/cspell.yaml b/cspell.yaml index 4268461c4d..8414054d8a 100644 --- a/cspell.yaml +++ b/cspell.yaml @@ -34,6 +34,7 @@ words: - jsyaml - keyer - lzutf + - mocharc - msbuild - MSRC - multis diff --git a/docs/extending-typespec/emitter-framework.md b/docs/extending-typespec/emitter-framework.md index 3fa78f5ebc..596991bdd5 100644 --- a/docs/extending-typespec/emitter-framework.md +++ b/docs/extending-typespec/emitter-framework.md @@ -24,11 +24,11 @@ Let's walk through each of these types in turn. The asset emitter is responsible for driving the emit process. It has methods for taking TypeSpec types to emit, and maintains the state of your current emit process including the declarations you've accumulated, current emit context, and converting your emitted content into files on disk. -To create your asset emitter, call `createAssetEmitter` on your emit context in `$onEmit`. It takes the TypeEmitter which is covered in the next section. Once created, you can call `emitProgram()` to emit every type in the TypeSpec graph. Otherwise, you can call `emitType(someType)` to emit specific types instead. +To create your asset emitter, call `getAssetEmitter` on your emit context in `$onEmit`. It takes the TypeEmitter which is covered in the next section. Once created, you can call `emitProgram()` to emit every type in the TypeSpec graph. Otherwise, you can call `emitType(someType)` to emit specific types instead. ```typescript export async function $onEmit(context: EmitContext) { - const assetEmitter = context.createAssetEmitter(MyTypeEmitter); + const assetEmitter = context.getAssetEmitter(MyTypeEmitter); // emit my entire typespec program assetEmitter.emitProgram(); @@ -59,7 +59,7 @@ class MyCodeEmitter extends CodeTypeEmitter { } ``` -Passing this to `createAssetEmitter` and calling `assetEmitter.emitProgram()` will console.log all the models in the program. +Passing this to `getAssetEmitter` and calling `assetEmitter.emitProgram()` will console.log all the models in the program. #### EmitterOutput From 2f7ef5c32f750860355f9d86547f1db43b3478bc Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Tue, 5 Dec 2023 14:09:43 -0800 Subject: [PATCH 08/11] Code review feedback. --- docs/extending-typespec/basics.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/extending-typespec/basics.md b/docs/extending-typespec/basics.md index 1e858d3d69..e69f03fa6e 100644 --- a/docs/extending-typespec/basics.md +++ b/docs/extending-typespec/basics.md @@ -205,6 +205,14 @@ Verify that you have the following in your `package.json`: } ``` +Also add a `.mocharc.yaml` file at the root of your project. + +```yaml +timeout: 5000 +require: source-map-support/register +spec: "dist/test/**/*.test.js" +``` + ### b. Define the testing library The first step is to define how your library can be loaded from the test framework. This will let your library to be reused by other library test. @@ -321,14 +329,6 @@ CustomName; // type of : Bar.name If you are using VSCode, you can install the [mocha test explorer](https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-mocha-test-adapter) to run your tests from the editor. This will also allow you easily debug into your tests. -After installing the extension, you need to add a `.mocharc.yaml` file at the root of your project. - -```yaml -timeout: 5000 -require: source-map-support/register -spec: "dist/test/**/*.test.js" -``` - You should now be able to discover, run and debug into your tests from the test explorer. ## 5. Publishing your TypeSpec library From 9e224f085c8b0a51a5136f47798c1246f0167881 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Wed, 6 Dec 2023 12:33:39 -0800 Subject: [PATCH 09/11] Update emitters-basics.md --- docs/extending-typespec/emitters-basics.md | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/extending-typespec/emitters-basics.md b/docs/extending-typespec/emitters-basics.md index 8056229d5e..e4bdc2c7d4 100644 --- a/docs/extending-typespec/emitters-basics.md +++ b/docs/extending-typespec/emitters-basics.md @@ -21,12 +21,13 @@ A TypeSpec emitter exports a function named `$onEmit` from its main entrypoint. For example, the following will write a text file to the output directory: ```typescript -import { EmitContext } from "@typespec/compiler"; -import Path from "path"; +import { EmitContext, emitFile, resolvePath } from "@typespec/compiler"; export async function $onEmit(context: EmitContext) { - const outputDir = Path.join(context.emitterOutputDir, "hello.txt"); - await context.program.host.writeFile(outputDir, "hello world!"); + await emitFile(context.program, { + path: resolvePath(context.emitterOutputDir, "hello.txt"), + content: "Hello world\n", + }); } ``` From a71da5d11566d6529189f3cbe9bc8c1a0d58b5fb Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Wed, 6 Dec 2023 12:38:24 -0800 Subject: [PATCH 10/11] Update emitters-basics.md --- docs/extending-typespec/emitters-basics.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/docs/extending-typespec/emitters-basics.md b/docs/extending-typespec/emitters-basics.md index e4bdc2c7d4..783fe860e3 100644 --- a/docs/extending-typespec/emitters-basics.md +++ b/docs/extending-typespec/emitters-basics.md @@ -24,10 +24,12 @@ For example, the following will write a text file to the output directory: import { EmitContext, emitFile, resolvePath } from "@typespec/compiler"; export async function $onEmit(context: EmitContext) { - await emitFile(context.program, { - path: resolvePath(context.emitterOutputDir, "hello.txt"), - content: "Hello world\n", - }); + if (context.program.compilerOptions.noEmit) { + await emitFile(context.program, { + path: resolvePath(context.emitterOutputDir, "hello.txt"), + content: "Hello world\n", + }); + } } ``` From 8d07dee112946512e7b22fa98d957cbd32c808d8 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Wed, 6 Dec 2023 12:39:12 -0800 Subject: [PATCH 11/11] Update emitters-basics.md --- docs/extending-typespec/emitters-basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/extending-typespec/emitters-basics.md b/docs/extending-typespec/emitters-basics.md index 783fe860e3..7ea757c94a 100644 --- a/docs/extending-typespec/emitters-basics.md +++ b/docs/extending-typespec/emitters-basics.md @@ -24,7 +24,7 @@ For example, the following will write a text file to the output directory: import { EmitContext, emitFile, resolvePath } from "@typespec/compiler"; export async function $onEmit(context: EmitContext) { - if (context.program.compilerOptions.noEmit) { + if (!context.program.compilerOptions.noEmit) { await emitFile(context.program, { path: resolvePath(context.emitterOutputDir, "hello.txt"), content: "Hello world\n",