Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
node_modules
dist
*ignoreme*
**ignoreme*
coverage

# ignore non-pnpm lockfiles
package-lock.json
yarn.lock

*.tgz
136 changes: 76 additions & 60 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ Here's an example of it being used along with VSCode's eslint plugin, with auto-
- [Setup](#setup)
- [Usage with eslint-plugin-markdown](#usage-with-eslint-plugin-markdown)
- [Presets](#presets)
- [monorepoTOC](#monorepotoc)
- [barrel](#barrel)
- [custom](#custom)
- [markdownFromJsdoc](#markdownfromjsdoc)
- [monorepoTOC](#monorepotoc)
- [markdownFromJsdoc](#markdownfromjsdoc-1)
- [markdownTOC](#markdowntoc)
- [markdownFromTests](#markdownfromtests)
- [labeler](#labeler)
- [custom](#custom)
- [Customisation](#customisation)
<!-- codegen:end -->

Expand Down Expand Up @@ -140,33 +141,6 @@ module.exports = {
```

### Presets

<!-- codegen:start {preset: markdownFromJsdoc, source: src/presets/monorepo-toc.ts, export: monorepoTOC} -->
#### [monorepoTOC](./src/presets/monorepo-toc.ts#L30)

Generate a table of contents for a monorepo.

##### Example (basic)

`<!-- codegen:start {preset: monorepoTOC} -->`

##### Example (using config options)

`<!-- codegen:start {preset: monorepoTOC, repoRoot: .., workspaces: lerna, filter: {package.name: foo}, sort: -readme.length} -->`

##### Params

|name |description |
|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|repoRoot|[optional] the relative path to the root of the git repository. By default, searches parent directories for a package.json to find the "root". |
|filter |[optional] a dictionary of filter rules to whitelist packages. Filters can be applied based on package.json keys,<br />e.g. `filter: { package.name: someRegex, path: some/relative/path }`|
|sort |[optional] sort based on package properties (see `filter`), or readme length. Use `-` as a prefix to sort descending.<br />e.g. `sort: -readme.length` |
<!-- codegen:end -->

##### Demo

![](./gifs/monorepoTOC.gif)

<!-- codegen:start {preset: markdownFromJsdoc, source: src/presets/barrel.ts, export: barrel} -->
#### [barrel](./src/presets/barrel.ts#L38)

Expand Down Expand Up @@ -197,6 +171,36 @@ export * from './some/path/module-c'

![](./gifs/barrel.gif)

<!-- codegen:start {preset: markdownFromJsdoc, source: src/presets/custom.ts, export: custom} -->
#### [custom](./src/presets/custom.ts#L32)

Define your own codegen function, which will receive all options specified. Import the `Preset` type from this library to define a strongly-typed preset function:

##### Example

```typescript
import {Preset} from 'eslint-plugin-codegen'

export const jsonPrinter: Preset<{myCustomProp: string}> = ({meta, options}) => {
const components = meta.glob('**\/*.tsx') // uses 'globSync' from glob package
return `filename: ${meta.filename}\ncustom prop: ${options.myCustomProp}\nComponent paths: ${components.join(', ')}`
}
```

This can be used with:

`<!-- codegen:start {preset: custom, source: ./lib/my-custom-preset.js, export: jsonPrinter, myCustomProp: hello}` Note that a `glob` helper method is passed to the preset via `meta`. This uses the `globSync` method of https://npm.im/glob. There are also `fs` and `path` helpers passed, corresponding to those node modules respectively. These can be useful to allow access to those libraries without them being production dependencies.

##### Params

|name |description |
|-------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|source |Relative path to the module containing the custom preset. Default: the file being linted. |
|export |The name of the export. If omitted, the module's default export should be a preset function. |
|require|A module to load before `source`. If not set, defaults to `ts-node/register/transpile-only` for typescript sources. |
|dev |Set to `true` to clear the require cache for `source` before loading. Allows editing the function without requiring an IDE reload. Default false if the `CI` enviornment variable is set, true otherwise.|
<!-- codegen:end -->

<!-- codegen:start {preset: markdownFromJsdoc, source: src/presets/markdown-from-jsdoc.ts, export: markdownFromJsdoc} -->
#### [markdownFromJsdoc](./src/presets/markdown-from-jsdoc.ts#L17)

Expand All @@ -214,6 +218,48 @@ Convert jsdoc for an es export from a javascript/typescript file to markdown.
|export|{string} the name of the export |
<!-- codegen:end -->

<!-- codegen:start {preset: markdownFromJsdoc, source: src/presets/monorepo-toc.ts, export: monorepoTOC} -->
#### [monorepoTOC](./src/presets/monorepo-toc.ts#L30)

Generate a table of contents for a monorepo.

##### Example (basic)

`<!-- codegen:start {preset: monorepoTOC} -->`

##### Example (using config options)

`<!-- codegen:start {preset: monorepoTOC, repoRoot: .., workspaces: lerna, filter: {package.name: foo}, sort: -readme.length} -->`

##### Params

|name |description |
|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|repoRoot|[optional] the relative path to the root of the git repository. By default, searches parent directories for a package.json to find the "root". |
|filter |[optional] a dictionary of filter rules to whitelist packages. Filters can be applied based on package.json keys,<br />e.g. `filter: { package.name: someRegex, path: some/relative/path }`|
|sort |[optional] sort based on package properties (see `filter`), or readme length. Use `-` as a prefix to sort descending.<br />e.g. `sort: -readme.length` |
<!-- codegen:end -->

##### Demo

![](./gifs/monorepoTOC.gif)

#### [markdownFromJsdoc](./src/presets/markdown-from-jsdoc.ts#L17)

Convert jsdoc for an es export from a javascript/typescript file to markdown.

##### Example

`<!-- codegen:start {preset: markdownFromJsdoc, source: src/foo.ts, export: bar} -->`

##### Params

|name |description |
|------|----------------------------------------------------------------------------------------------|
|source|{string} relative file path containing the export with jsdoc that should be copied to markdown|
|export|{string} the name of the export |
<!-- codegen:end -->

##### Demo

![](./gifs/markdownFromJsdoc.gif)
Expand Down Expand Up @@ -287,36 +333,6 @@ Generates a yaml config for the [GitHub Pull Request Labeler Action](https://git

![](./gifs/labeler.gif)

<!-- codegen:start {preset: markdownFromJsdoc, source: src/presets/custom.ts, export: custom} -->
#### [custom](./src/presets/custom.ts#L32)

Define your own codegen function, which will receive all options specified. Import the `Preset` type from this library to define a strongly-typed preset function:

##### Example

```typescript
import {Preset} from 'eslint-plugin-codegen'

export const jsonPrinter: Preset<{myCustomProp: string}> = ({meta, options}) => {
const components = meta.glob('**\/*.tsx') // uses 'globSync' from glob package
return `filename: ${meta.filename}\ncustom prop: ${options.myCustomProp}\nComponent paths: ${components.join(', ')}`
}
```

This can be used with:

`<!-- codegen:start {preset: custom, source: ./lib/my-custom-preset.js, export: jsonPrinter, myCustomProp: hello}` Note that a `glob` helper method is passed to the preset via `meta`. This uses the `globSync` method of https://npm.im/glob. There are also `fs` and `path` helpers passed, corresponding to those node modules respectively. These can be useful to allow access to those libraries without them being production dependencies.

##### Params

|name |description |
|-------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|source |Relative path to the module containing the custom preset. Default: the file being linted. |
|export |The name of the export. If omitted, the module's default export should be a preset function. |
|require|A module to load before `source`. If not set, defaults to `ts-node/register/transpile-only` for typescript sources. |
|dev |Set to `true` to clear the require cache for `source` before loading. Allows editing the function without requiring an IDE reload. Default false if the `CI` enviornment variable is set, true otherwise.|
<!-- codegen:end -->

##### Demo

![](./gifs/custom.gif)
Expand Down Expand Up @@ -414,4 +430,4 @@ _Rendered_:

___

The code in this repository was moved from https://github.com/mmkal/ts
<sub>The code in this repository was moved from https://github.com/mmkal/ts</sub>
13 changes: 9 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-codegen",
"version": "0.19.0",
"version": "0.20.0-0",
"engines": {
"node": ">=12.0.0"
},
Expand Down Expand Up @@ -41,6 +41,14 @@
"@babel/generator": "~7.12.0",
"@babel/parser": "^7.11.5",
"@babel/traverse": "^7.11.5",
"@types/dedent": "0.7.0",
"@types/eslint": "^8.44.7",
"@types/glob": "7.1.3",
"@types/jest": "29.0.0",
"@types/js-yaml": "3.12.5",
"@types/lodash": "^4.14.202",
"@types/node": "^14.0.0",
"dedent": "^1.5.1",
"eslint-plugin-markdown": "^3.0.1",
"expect": "^29.7.0",
"fp-ts": "^2.1.0",
Expand All @@ -57,14 +65,11 @@
"@types/babel__generator": "7.6.2",
"@types/babel__traverse": "7.11.0",
"@types/dedent": "0.7.0",
"@types/eslint": "^8.44.7",
"@types/glob": "7.1.3",
"@types/jest": "29.5.8",
"@types/js-yaml": "3.12.5",
"@types/lodash": "4.14.201",
"@types/minimatch": "3.0.3",
"@types/node": "^20.0.0",
"dedent": "0.7.0",
"eslint": "8.54.0",
"eslint-plugin-mmkal": "0.2.0",
"expect-type2": "npm:expect-type@0.14.0",
Expand Down
Loading