Skip to content

Commit f2d4923

Browse files
authored
Docs: Add React example (#152)
* Docs: Add React example * Docs: Ignore lint errors in examples * Docs: Test examples report errors
1 parent eb66833 commit f2d4923

File tree

8 files changed

+128
-1
lines changed

8 files changed

+128
-1
lines changed

Diff for: .eslintrc.js

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ module.exports = {
2828

2929
"extends": "eslint",
3030

31+
"ignorePatterns": ["examples"],
32+
3133
"overrides": [
3234
{
3335
"files": ["**/*.md"],

Diff for: .github/workflows/ci.yml

+5
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,10 @@ jobs:
3939
run: npm install
4040
env:
4141
CI: true
42+
- name: Install examples/react Packages
43+
working-directory: ./examples/react
44+
run: npm install
45+
env:
46+
CI: true
4247
- name: Test
4348
run: npm run test-cov

Diff for: examples/react/.eslintrc.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"use strict";
2+
3+
module.exports = {
4+
root: true,
5+
extends: [
6+
"eslint:recommended",
7+
"plugin:markdown/recommended",
8+
"plugin:react/recommended",
9+
],
10+
settings: {
11+
react: {
12+
version: "16.8.0"
13+
}
14+
},
15+
parserOptions: {
16+
ecmaFeatures: {
17+
jsx: true
18+
},
19+
ecmaVersion: 2015,
20+
sourceType: "module"
21+
},
22+
env: {
23+
browser: true,
24+
es6: true
25+
},
26+
overrides: [
27+
{
28+
files: [".eslintrc.js"],
29+
env: {
30+
node: true
31+
}
32+
},
33+
{
34+
files: ["**/*.md/*.jsx"],
35+
globals: {
36+
// For code examples, `import React from "react";` at the top
37+
// of every code block is distracting, so pre-define the
38+
// `React` global.
39+
React: false
40+
},
41+
}
42+
]
43+
};

Diff for: examples/react/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package-lock = false

Diff for: examples/react/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# React Example
2+
3+
```jsx
4+
function App({ name }) {
5+
return (
6+
<div>
7+
<p>Hello, {name}!</p>
8+
</div>
9+
);
10+
}
11+
```
12+
13+
```sh
14+
$ git clone https://github.com/eslint/eslint-plugin-markdown.git
15+
$ cd eslint-plugin-markdown/examples/react
16+
$ npm install
17+
$ npm test
18+
19+
eslint-plugin-markdown/examples/react/README.md
20+
4:10 error 'App' is defined but never used no-unused-vars
21+
4:16 error 'name' is missing in props validation react/prop-types
22+
23+
✖ 2 problems (2 errors, 0 warnings)
24+
```

Diff for: examples/react/package.json

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"private": true,
3+
"scripts": {
4+
"test": "eslint ."
5+
},
6+
"devDependencies": {
7+
"eslint": "^7.5.0",
8+
"eslint-plugin-markdown": "file:../..",
9+
"eslint-plugin-react": "^7.20.3"
10+
}
11+
}

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"scripts": {
2323
"lint": "eslint --ext js,md .",
2424
"test": "npm run lint && npm run test-cov",
25-
"test-cov": "nyc _mocha -- -c tests/lib/**/*.js",
25+
"test-cov": "nyc _mocha -- -c tests/{examples,lib}/**/*.js",
2626
"generate-release": "eslint-generate-release",
2727
"generate-alpharelease": "eslint-generate-prerelease alpha",
2828
"generate-betarelease": "eslint-generate-prerelease beta",

Diff for: tests/examples/all.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"use strict";
2+
3+
const assert = require("chai").assert;
4+
const execSync = require("child_process").execSync;
5+
const fs = require("fs");
6+
const path = require("path");
7+
const semver = require("semver");
8+
9+
const examples = path.resolve(__dirname, "../../examples/");
10+
for (const example of fs.readdirSync(examples)) {
11+
const cwd = path.join(examples, example);
12+
13+
// The plugin officially supports ESLint as early as v6, but the examples
14+
// use ESLint v7, which has a higher minimum Node.js version than does v6.
15+
// Only exercise the example if the running Node.js version satisfies the
16+
// minimum version constraint. In CI, this will skip these tests in Node.js
17+
// v8 and run them on all other Node.js versions.
18+
const eslintPackageJsonPath = require.resolve("eslint/package.json", {
19+
paths: [cwd]
20+
});
21+
const eslintPackageJson = require(eslintPackageJsonPath);
22+
if (semver.satisfies(process.version, eslintPackageJson.engines.node)) {
23+
describe("examples", function () {
24+
describe(example, () => {
25+
it("reports errors on code blocks in .md files", async () => {
26+
const { ESLint } = require(
27+
require.resolve("eslint", { paths: [cwd] })
28+
);
29+
const eslint = new ESLint({ cwd });
30+
31+
const results = await eslint.lintFiles(["."]);
32+
const readme = results.find(result =>
33+
path.basename(result.filePath) == "README.md"
34+
);
35+
assert.isNotNull(readme);
36+
assert.isAbove(readme.messages.length, 0);
37+
});
38+
});
39+
});
40+
}
41+
}

0 commit comments

Comments
 (0)