Skip to content

Commit 9d3c6a4

Browse files
committed
refactor: extract and export getGlobals helper
improve documentation about remark-preset-prettier
1 parent ef2f448 commit 9d3c6a4

File tree

9 files changed

+205
-42
lines changed

9 files changed

+205
-42
lines changed

README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
> [ESLint][] Parser/Plugin for [MDX][], helps you lint all ES syntaxes excluding `code` block of course.
2626
> Work perfectly with `eslint-plugin-import`, `eslint-plugin-prettier` or any other eslint plugins.
27-
> And also can be integrated with [remark][] plugins to lint non ES syntaxes.
27+
> And also can be integrated with [remark-lint][] plugins to lint markdown syntaxes.
2828
2929
## TOC <!-- omit in toc -->
3030

@@ -38,6 +38,7 @@
3838
- [mdx/no-unescaped-entities](#mdxno-unescaped-entities)
3939
- [mdx/no-unused-expressions](#mdxno-unused-expressions)
4040
- [mdx/remark](#mdxremark)
41+
- [Prettier Integration](#prettier-integration)
4142
- [Changelog](#changelog)
4243
- [License](#license)
4344

@@ -72,10 +73,22 @@ npm i -D eslint-plugin-mdx
7273

7374
1. If you're using `eslint >= 6.0.0`, add:
7475

75-
```json
76+
```jsonc
7677
{
7778
"extends": ["plugin:mdx/recommended"],
7879
"overrides": [
80+
{
81+
"files": ["*.md"],
82+
"rules": {
83+
"prettier/prettier": [
84+
2,
85+
{
86+
// unnecessary if you're not using `eslint-plugin-prettier`, but required if you are
87+
"parser": "markdown"
88+
}
89+
]
90+
}
91+
},
7992
{
8093
"files": ["*.mdx"],
8194
"extends": ["plugin:mdx/overrides"]
@@ -92,6 +105,18 @@ npm i -D eslint-plugin-mdx
92105
module.exports = {
93106
extends: ['plugin:mdx/recommended'],
94107
overrides: [
108+
{
109+
files: ['*.md'],
110+
rules: {
111+
'prettier/prettier': [
112+
2,
113+
{
114+
// unnecessary if you're not using `eslint-plugin-prettier`, but required if you are
115+
parser: 'markdown',
116+
},
117+
],
118+
},
119+
},
95120
Object.assign(
96121
{
97122
files: ['*.mdx'],
@@ -143,7 +168,22 @@ Inline JSX like `Inline <Component />` is supported by [MDX][], but rule `react/
143168
144169
_possible fixable depends on your remark plugins_:
145170
146-
Integration with [remark][] plugins without [remark-lint][], it will read [remark's configuration](https://github.com/remarkjs/remark/tree/master/packages/remark-cli#remark-cli) automatically via [cosmiconfig][]. But `.remarkignore` will not be respected, you should use `.eslintignore` instead.
171+
Integration with [remark-lint][] plugins, it will read [remark's configuration](https://github.com/remarkjs/remark/tree/master/packages/remark-cli#remark-cli) automatically via [cosmiconfig][]. But `.remarkignore` will not be respected, you should use `.eslintignore` instead.
172+
173+
## Prettier Integration
174+
175+
If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier]_.
176+
177+
```json
178+
{
179+
"plugins": [
180+
"preset-lint-consistent",
181+
"preset-lint-recommended",
182+
"preset-lint-markdown-style-guide",
183+
"preset-prettier"
184+
]
185+
}
186+
```
147187
148188
## Changelog
149189
@@ -161,7 +201,8 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m
161201
[lerna]: https://github.com/lerna/lerna
162202
[mdx]: https://github.com/mdx-js/mdx
163203
[mit]: http://opensource.org/licenses/MIT
164-
[remark]: https://github.com/remarkjs/remark
204+
[prettier]: https://prettier.io
165205
[remark-lint]: https://github.com/remarkjs/remark-lint
206+
[remark-preset-prettier]: https://github.com/JounQin/remark-preset-prettier
166207
[vscode eslint]: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
167208
[vscode mdx]: https://github.com/rx-ts/vscode-mdx

package.json

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@
4545
"lint-staged": "^9.2.5",
4646
"prettier": "1.18.2",
4747
"react": "^16.9.0",
48+
"remark-preset-lint-consistent": "^2.0.3",
4849
"remark-preset-lint-markdown-style-guide": "^2.1.3",
4950
"remark-preset-lint-recommended": "^3.0.3",
51+
"remark-preset-prettier": "^0.1.1",
5052
"ts-jest": "^24.0.2",
5153
"ts-node": "^8.3.0",
5254
"type-coverage": "^2.2.0",
@@ -83,28 +85,10 @@
8385
"prettier": "@1stg/prettier-config",
8486
"remarkConfig": {
8587
"plugins": [
88+
"preset-lint-consistent",
8689
"preset-lint-recommended",
8790
"preset-lint-markdown-style-guide",
88-
[
89-
"lint-list-item-spacing",
90-
false
91-
],
92-
[
93-
"lint-list-item-indent",
94-
false
95-
],
96-
[
97-
"lint-ordered-list-marker-value",
98-
false
99-
],
100-
[
101-
"lint-emphasis-marker",
102-
"_"
103-
],
104-
[
105-
"lint-maximum-line-length",
106-
false
107-
]
91+
"preset-prettier"
10892
]
10993
},
11094
"renovate": {

packages/eslint-mdx/README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
> [ESLint][] Parser/Plugin for [MDX][], helps you lint all ES syntaxes excluding `code` block of course.
2626
> Work perfectly with `eslint-plugin-import`, `eslint-plugin-prettier` or any other eslint plugins.
27-
> And also can be integrated with [remark][] plugins to lint non ES syntaxes.
27+
> And also can be integrated with [remark-lint][] plugins to lint markdown syntaxes.
2828
2929
## TOC <!-- omit in toc -->
3030

@@ -38,6 +38,7 @@
3838
- [mdx/no-unescaped-entities](#mdxno-unescaped-entities)
3939
- [mdx/no-unused-expressions](#mdxno-unused-expressions)
4040
- [mdx/remark](#mdxremark)
41+
- [Prettier Integration](#prettier-integration)
4142
- [Changelog](#changelog)
4243
- [License](#license)
4344

@@ -72,10 +73,22 @@ npm i -D eslint-plugin-mdx
7273

7374
1. If you're using `eslint >= 6.0.0`, add:
7475

75-
```json
76+
```jsonc
7677
{
7778
"extends": ["plugin:mdx/recommended"],
7879
"overrides": [
80+
{
81+
"files": ["*.md"],
82+
"rules": {
83+
"prettier/prettier": [
84+
2,
85+
{
86+
// unnecessary if you're not using `eslint-plugin-prettier`, but required if you are
87+
"parser": "markdown"
88+
}
89+
]
90+
}
91+
},
7992
{
8093
"files": ["*.mdx"],
8194
"extends": ["plugin:mdx/overrides"]
@@ -92,6 +105,18 @@ npm i -D eslint-plugin-mdx
92105
module.exports = {
93106
extends: ['plugin:mdx/recommended'],
94107
overrides: [
108+
{
109+
files: ['*.md'],
110+
rules: {
111+
'prettier/prettier': [
112+
2,
113+
{
114+
// unnecessary if you're not using `eslint-plugin-prettier`, but required if you are
115+
parser: 'markdown',
116+
},
117+
],
118+
},
119+
},
95120
Object.assign(
96121
{
97122
files: ['*.mdx'],
@@ -143,7 +168,22 @@ Inline JSX like `Inline <Component />` is supported by [MDX][], but rule `react/
143168
144169
_possible fixable depends on your remark plugins_:
145170
146-
Integration with [remark][] plugins without [remark-lint][], it will read [remark's configuration](https://github.com/remarkjs/remark/tree/master/packages/remark-cli#remark-cli) automatically via [cosmiconfig][]. But `.remarkignore` will not be respected, you should use `.eslintignore` instead.
171+
Integration with [remark-lint][] plugins, it will read [remark's configuration](https://github.com/remarkjs/remark/tree/master/packages/remark-cli#remark-cli) automatically via [cosmiconfig][]. But `.remarkignore` will not be respected, you should use `.eslintignore` instead.
172+
173+
## Prettier Integration
174+
175+
If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier]_.
176+
177+
```json
178+
{
179+
"plugins": [
180+
"preset-lint-consistent",
181+
"preset-lint-recommended",
182+
"preset-lint-markdown-style-guide",
183+
"preset-prettier"
184+
]
185+
}
186+
```
147187
148188
## Changelog
149189
@@ -161,7 +201,8 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m
161201
[lerna]: https://github.com/lerna/lerna
162202
[mdx]: https://github.com/mdx-js/mdx
163203
[mit]: http://opensource.org/licenses/MIT
164-
[remark]: https://github.com/remarkjs/remark
204+
[prettier]: https://prettier.io
165205
[remark-lint]: https://github.com/remarkjs/remark-lint
206+
[remark-preset-prettier]: https://github.com/JounQin/remark-preset-prettier
166207
[vscode eslint]: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
167208
[vscode mdx]: https://github.com/rx-ts/vscode-mdx

packages/eslint-plugin-mdx/README.md

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
> [ESLint][] Parser/Plugin for [MDX][], helps you lint all ES syntaxes excluding `code` block of course.
2626
> Work perfectly with `eslint-plugin-import`, `eslint-plugin-prettier` or any other eslint plugins.
27-
> And also can be integrated with [remark][] plugins to lint non ES syntaxes.
27+
> And also can be integrated with [remark-lint][] plugins to lint markdown syntaxes.
2828
2929
## TOC <!-- omit in toc -->
3030

@@ -38,6 +38,7 @@
3838
- [mdx/no-unescaped-entities](#mdxno-unescaped-entities)
3939
- [mdx/no-unused-expressions](#mdxno-unused-expressions)
4040
- [mdx/remark](#mdxremark)
41+
- [Prettier Integration](#prettier-integration)
4142
- [Changelog](#changelog)
4243
- [License](#license)
4344

@@ -72,10 +73,22 @@ npm i -D eslint-plugin-mdx
7273

7374
1. If you're using `eslint >= 6.0.0`, add:
7475

75-
```json
76+
```jsonc
7677
{
7778
"extends": ["plugin:mdx/recommended"],
7879
"overrides": [
80+
{
81+
"files": ["*.md"],
82+
"rules": {
83+
"prettier/prettier": [
84+
2,
85+
{
86+
// unnecessary if you're not using `eslint-plugin-prettier`, but required if you are
87+
"parser": "markdown"
88+
}
89+
]
90+
}
91+
},
7992
{
8093
"files": ["*.mdx"],
8194
"extends": ["plugin:mdx/overrides"]
@@ -92,6 +105,18 @@ npm i -D eslint-plugin-mdx
92105
module.exports = {
93106
extends: ['plugin:mdx/recommended'],
94107
overrides: [
108+
{
109+
files: ['*.md'],
110+
rules: {
111+
'prettier/prettier': [
112+
2,
113+
{
114+
// unnecessary if you're not using `eslint-plugin-prettier`, but required if you are
115+
parser: 'markdown',
116+
},
117+
],
118+
},
119+
},
95120
Object.assign(
96121
{
97122
files: ['*.mdx'],
@@ -143,7 +168,22 @@ Inline JSX like `Inline <Component />` is supported by [MDX][], but rule `react/
143168
144169
_possible fixable depends on your remark plugins_:
145170
146-
Integration with [remark][] plugins without [remark-lint][], it will read [remark's configuration](https://github.com/remarkjs/remark/tree/master/packages/remark-cli#remark-cli) automatically via [cosmiconfig][]. But `.remarkignore` will not be respected, you should use `.eslintignore` instead.
171+
Integration with [remark-lint][] plugins, it will read [remark's configuration](https://github.com/remarkjs/remark/tree/master/packages/remark-cli#remark-cli) automatically via [cosmiconfig][]. But `.remarkignore` will not be respected, you should use `.eslintignore` instead.
172+
173+
## Prettier Integration
174+
175+
If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier]_.
176+
177+
```json
178+
{
179+
"plugins": [
180+
"preset-lint-consistent",
181+
"preset-lint-recommended",
182+
"preset-lint-markdown-style-guide",
183+
"preset-prettier"
184+
]
185+
}
186+
```
147187
148188
## Changelog
149189
@@ -161,7 +201,8 @@ Detailed changes for each release are documented in [CHANGELOG.md](./CHANGELOG.m
161201
[lerna]: https://github.com/lerna/lerna
162202
[mdx]: https://github.com/mdx-js/mdx
163203
[mit]: http://opensource.org/licenses/MIT
164-
[remark]: https://github.com/remarkjs/remark
204+
[prettier]: https://prettier.io
165205
[remark-lint]: https://github.com/remarkjs/remark-lint
206+
[remark-preset-prettier]: https://github.com/JounQin/remark-preset-prettier
166207
[vscode eslint]: https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint
167208
[vscode mdx]: https://github.com/rx-ts/vscode-mdx
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const getGlobals = <
2+
T extends Record<string, unknown> | string[],
3+
G extends {}
4+
>(
5+
sources: T,
6+
initialGlobals: G = {} as G,
7+
): Record<keyof G | (T extends Array<infer R> ? R : keyof T), false> =>
8+
(Array.isArray(sources) ? sources : Object.keys(sources)).reduce(
9+
(globals, source) =>
10+
Object.assign(globals, {
11+
[source]: false,
12+
}),
13+
// FIXME: find a better solution
14+
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
15+
// @ts-ignore
16+
initialGlobals,
17+
)

packages/eslint-plugin-mdx/src/configs/overrides.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
import { base } from './base'
2+
import { getGlobals } from './helper'
23

34
import * as rebass from 'rebass'
45

56
export const overrides = {
67
...base,
7-
globals: Object.keys(rebass).reduce<Record<string, false>>(
8-
(globals, Component) =>
9-
Object.assign(globals, {
10-
[Component]: false,
11-
}),
12-
{
13-
React: false,
14-
},
15-
),
8+
globals: getGlobals(rebass, {
9+
React: false,
10+
}),
1611
rules: {
1712
'lines-between-class-members': 0, // See https://github.com/mdx-js/mdx/issues/195
1813
'react/jsx-no-undef': [

packages/eslint-plugin-mdx/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ import * as configs from './configs'
22

33
export { configs }
44

5+
export * from './configs/helper'
56
export * from './rules'

test/helper.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import path from 'path'
22

3-
import { requirePkg } from 'eslint-plugin-mdx'
3+
import { getGlobals, requirePkg } from 'eslint-plugin-mdx'
44

55
describe('Helpers', () => {
6+
it('should resolve globals correctly', () => {
7+
expect(getGlobals({})).toEqual({})
8+
expect(getGlobals(['a', 'b'])).toEqual({
9+
a: false,
10+
b: false,
11+
})
12+
})
13+
614
it('should resolve package correctly', () => {
715
expect(requirePkg('@1stg/config', 'husky')).toBeDefined()
816
expect(requirePkg('lint', 'remark')).toBeDefined()

0 commit comments

Comments
 (0)