Skip to content

Commit

Permalink
Docs: Add TypeScript example (#155)
Browse files Browse the repository at this point in the history
* Docs: Add TypeScript example

* WIP: Add tsconfig.json for type-aware linting

It works on the first block but not the second. See diff for additional
context. Any ideas?

* Docs: Remove tsconfig.json and add type-aware rule note
  • Loading branch information
btmills committed Aug 15, 2020
1 parent d80be9e commit bf7648f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Expand Up @@ -44,5 +44,10 @@ jobs:
run: npm install
env:
CI: true
- name: Install examples/typescript Packages
working-directory: ./examples/typescript
run: npm install
env:
CI: true
- name: Test
run: npm run test-cov
22 changes: 22 additions & 0 deletions examples/typescript/.eslintrc.js
@@ -0,0 +1,22 @@
"use strict";

module.exports = {
root: true,
extends: [
"eslint:recommended",
"plugin:markdown/recommended",
],
overrides: [
{
files: [".eslintrc.js"],
env: {
node: true
}
},
{
files: ["*.ts"],
parser: "@typescript-eslint/parser",
extends: ["plugin:@typescript-eslint/recommended"]
},
]
};
1 change: 1 addition & 0 deletions examples/typescript/.npmrc
@@ -0,0 +1 @@
package-lock = false
25 changes: 25 additions & 0 deletions examples/typescript/README.md
@@ -0,0 +1,25 @@
# React Example

The `@typescript-eslint` parser and the `recommended` config's rules will work in `ts` code blocks. However, [type-aware rules](https://github.com/typescript-eslint/typescript-eslint/blob/master/docs/getting-started/linting/TYPED_LINTING.md) will not work because the code blocks are not part of a compilable `tsconfig.json` project.

```ts
function hello(name: String) {
console.log(`Hello, ${name}!`);
}

hello(42 as any);
```

```sh
$ git clone https://github.com/eslint/eslint-plugin-markdown.git
$ cd eslint-plugin-markdown/examples/typescript
$ npm install
$ npm test

eslint-plugin-markdown/examples/typescript/README.md
6:22 error Don’t use `String` as a type. Use string instead @typescript-eslint/ban-types
10:13 warning Unexpected any. Specify a different type @typescript-eslint/no-explicit-any

✖ 2 problems (1 error, 1 warning)
1 error and 0 warnings potentially fixable with the `--fix` option.
```
13 changes: 13 additions & 0 deletions examples/typescript/package.json
@@ -0,0 +1,13 @@
{
"private": true,
"scripts": {
"test": "eslint ."
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^3.6.1",
"@typescript-eslint/parser": "^3.6.1",
"eslint": "^7.5.0",
"eslint-plugin-markdown": "file:../..",
"typescript": "^3.9.7"
}
}

0 comments on commit bf7648f

Please sign in to comment.