Skip to content

Commit 3ea2d99

Browse files
authored
fix: use <$> instead of <> to wrap jsx blocks for parsing (#41)
1 parent 14f7711 commit 3ea2d99

File tree

5 files changed

+36
-30
lines changed

5 files changed

+36
-30
lines changed

.eslintrc.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { all } = require('eslint-config-1stg/overrides')
1+
const { overrides } = require('eslint-config-1stg/overrides')
22

33
require('ts-node').register({
44
transpileOnly: true,
@@ -8,16 +8,12 @@ module.exports = {
88
root: true,
99
extends: ['1stg'],
1010
overrides: [
11-
...all,
11+
...overrides,
1212
{
1313
files: '*.ts',
1414
rules: {
1515
'@typescript-eslint/unbound-method': 0, // See https://github.com/typescript-eslint/typescript-eslint/issues/636
1616
},
1717
},
18-
{
19-
files: '*.test.ts',
20-
extends: ['plugin:jest/recommended'],
21-
},
2218
],
2319
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
"babel-eslint": "^10.0.2",
2828
"commitlint": "^8.1.0",
2929
"eslint": "^6.1.0",
30-
"eslint-config-1stg": "^5.7.0",
30+
"eslint-config-1stg": "^5.8.1",
3131
"eslint-formatter-friendly": "^7.0.0",
3232
"eslint-mdx": "link:packages/eslint-mdx/src",
33-
"eslint-plugin-jest": "^22.15.0",
3433
"husky": "^3.0.3",
3534
"jest": "^24.8.0",
3635
"lerna": "^3.16.4",

packages/eslint-mdx/src/parser.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,18 +198,18 @@ export class Parser {
198198
private _normalizeJsxNodes(node: Node): Node | Node[] {
199199
const value = node.value as string
200200

201-
// wrap into single Fragment, so that it won't break on adjacent JSX nodes
202201
let program: AST.Program
203202

204203
try {
205-
program = this._eslintParse(`<>${value}</>`).ast
204+
// wrap into single element which is valid jsx but not valid jsx in mdx, so that it won't break on adjacent JSX nodes
205+
program = this._eslintParse(`<$>${value}</$>`).ast
206206
} catch (e) {
207207
if (hasProperties<LocationError>(e, LOC_ERROR_PROPERTIES)) {
208208
const {
209209
position: { start },
210210
} = node
211211

212-
e.index += start.offset - 2
212+
e.index += start.offset - 3
213213
e.column = e.lineNumber > 1 ? e.column : e.column + start.column - 3
214214
e.lineNumber += start.line - 1
215215

@@ -241,21 +241,21 @@ export class Parser {
241241
} = jsNode
242242
const startLine = line + start.line - 1
243243
const endLine = line + end.line - 1
244-
const startOffset = range[0] - 2
245-
const endOffset = range[1] - 2
244+
const startOffset = range[0] - 3
245+
const endOffset = range[1] - 3
246246
nodes.push({
247247
type: 'jsx',
248248
data: nodes.length ? null : node.data,
249249
value: value.slice(startOffset, endOffset),
250250
position: {
251251
start: {
252252
line: startLine,
253-
column: line === startLine ? start.column - 2 : start.column,
253+
column: line === startLine ? start.column - 3 : start.column,
254254
offset: offset + startOffset,
255255
},
256256
end: {
257257
line: endLine,
258-
column: line === startLine ? end.column - 2 : end.column,
258+
column: line === startLine ? end.column - 3 : end.column,
259259
offset: offset + endOffset,
260260
},
261261
},

test/parser.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe('parser', () => {
156156
"Expected corresponding JSX closing tag for 'h1'.",
157157
)
158158
expect(() => parser.parse('Header\n<>', parserOptions)).toThrow(
159-
'JSX fragment has no corresponding closing tag.',
159+
'Expected corresponding closing tag for JSX fragment.',
160160
)
161161
expect(() => parser.parse('<main><</main>', parserOptions)).toThrow(
162162
'Identifier expected.',
@@ -169,19 +169,22 @@ describe('parser', () => {
169169
).toThrow('Identifier expected.')
170170
})
171171

172-
it('should not throw on adjacent JSX nodes', () => {
172+
it('should not throw on adjacent JSX nodes', () =>
173173
expect(() =>
174174
parser.parse(
175175
'<header></header>\n<main><section>left</section><section>right<input name="name"/></section></main>',
176176
parserOptions,
177177
),
178-
).not.toThrow()
179-
})
178+
).not.toThrow())
180179

181-
it('should not throw on JSX with blank lines', () => {
180+
it('should not throw on JSX with blank lines', () =>
182181
expect(() =>
183182
parser.parse('<header>\n\nTitle\n\n</header>', parserOptions),
184-
).not.toThrow()
183+
).not.toThrow())
184+
185+
it("should not throw on <$> or </$> because it's not considered as jsx", () => {
186+
expect(() => parser.parse('<$>', parserOptions)).not.toThrow()
187+
expect(() => parser.parse('</$>', parserOptions)).not.toThrow()
185188
})
186189

187190
it('should be able to parse normal js file', () => {

yarn.lock

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3215,11 +3215,12 @@ escodegen@^1.9.1:
32153215
optionalDependencies:
32163216
source-map "~0.6.1"
32173217

3218-
eslint-config-1stg@^5.7.0:
3219-
version "5.7.0"
3220-
resolved "https://registry.yarnpkg.com/eslint-config-1stg/-/eslint-config-1stg-5.7.0.tgz#2dc5cf477163d1f1bd990d469cefc86d68b4a0fe"
3221-
integrity sha512-FJVxqb+wAR+a02RokbfNE+u/MjSBeHzu02nRNjzj+l+pE/6muwy118tw+QoULvpztahIMKfscJBLWvhNlpXAlQ==
3218+
eslint-config-1stg@^5.8.1:
3219+
version "5.8.1"
3220+
resolved "https://registry.yarnpkg.com/eslint-config-1stg/-/eslint-config-1stg-5.8.1.tgz#59fbdfd2f307613c61423dc810e9a14b09c8529f"
3221+
integrity sha512-r4MPqsGPfnS6rXcMp+eK2GgZ4ywf4+eOxZH284bzXLnOI++qHcRVBqhn2ITdDPwk2H6oZDqv3MOqnqw097uTMQ==
32223222
dependencies:
3223+
"@rxts/eslint-plugin-mdx" "^0.11.1"
32233224
"@typescript-eslint/eslint-plugin" "^2.0.0-alpha.5"
32243225
"@typescript-eslint/parser" "^2.0.0-alpha.5"
32253226
babel-eslint "^10.0.2"
@@ -3228,10 +3229,12 @@ eslint-config-1stg@^5.7.0:
32283229
eslint-import-resolver-typescript JounQin/eslint-import-resolver-typescript#feat/resolve_dts
32293230
eslint-plugin-babel "^5.3.0"
32303231
eslint-plugin-import "^2.18.2"
3232+
eslint-plugin-jest "^22.15.1"
32313233
eslint-plugin-node "^9.1.0"
32323234
eslint-plugin-prettier "^3.1.0"
32333235
eslint-plugin-promise "^4.2.1"
32343236
eslint-plugin-react "^7.14.3"
3237+
eslint-plugin-react-hooks "^1.7.0"
32353238
eslint-plugin-standard "^4.0.0"
32363239
eslint-plugin-vue "^5.2.3"
32373240
prettier-config-1stg "^0.2.0"
@@ -3268,7 +3271,7 @@ eslint-import-resolver-node@^0.3.2:
32683271
debug "^2.6.9"
32693272
resolve "^1.5.0"
32703273

3271-
eslint-import-resolver-typescript@JounQin/eslint-import-resolver-typescript#feat/resolve_dts:
3274+
"eslint-import-resolver-typescript@github:JounQin/eslint-import-resolver-typescript#feat/resolve_dts":
32723275
version "1.1.1"
32733276
resolved "https://codeload.github.com/JounQin/eslint-import-resolver-typescript/tar.gz/23e2e8cf71ee6c19da9f55e85b2ab34543d2a12e"
32743277
dependencies:
@@ -3320,10 +3323,10 @@ eslint-plugin-import@^2.18.2:
33203323
read-pkg-up "^2.0.0"
33213324
resolve "^1.11.0"
33223325

3323-
eslint-plugin-jest@^22.15.0:
3324-
version "22.15.0"
3325-
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-22.15.0.tgz#fe70bfff7eeb47ca0ab229588a867f82bb8592c5"
3326-
integrity sha512-hgnPbSqAIcLLS9ePb12hNHTRkXnkVaCfOwCt2pzQ8KpOKPWGA4HhLMaFN38NBa/0uvLfrZpcIRjT+6tMAfr58Q==
3326+
eslint-plugin-jest@^22.15.1:
3327+
version "22.15.1"
3328+
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-22.15.1.tgz#54c4a752a44c4bc5a564ecc22b32e1cd16a2961a"
3329+
integrity sha512-CWq/RR/3tLaKFB+FZcCJwU9hH5q/bKeO3rFP8G07+q7hcDCFNqpvdphVbEbGE6o6qo1UbciEev4ejUWv7brUhw==
33273330
dependencies:
33283331
"@typescript-eslint/experimental-utils" "^1.13.0"
33293332

@@ -3351,6 +3354,11 @@ eslint-plugin-promise@^4.2.1:
33513354
resolved "https://registry.yarnpkg.com/eslint-plugin-promise/-/eslint-plugin-promise-4.2.1.tgz#845fd8b2260ad8f82564c1222fce44ad71d9418a"
33523355
integrity sha512-VoM09vT7bfA7D+upt+FjeBO5eHIJQBUWki1aPvB+vbNiHS3+oGIJGIeyBtKQTME6UPXXy3vV07OL1tHd3ANuDw==
33533356

3357+
eslint-plugin-react-hooks@^1.7.0:
3358+
version "1.7.0"
3359+
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-1.7.0.tgz#6210b6d5a37205f0b92858f895a4e827020a7d04"
3360+
integrity sha512-iXTCFcOmlWvw4+TOE8CLWj6yX1GwzT0Y6cUfHHZqWnSk144VmVIRcVGtUAzrLES7C798lmvnt02C7rxaOX1HNA==
3361+
33543362
eslint-plugin-react@^7.14.3:
33553363
version "7.14.3"
33563364
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.14.3.tgz#911030dd7e98ba49e1b2208599571846a66bdf13"

0 commit comments

Comments
 (0)