Skip to content

Commit

Permalink
fix: wrong end.offset - close #279 (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Mar 11, 2021
1 parent b48311c commit 188c6aa
Show file tree
Hide file tree
Showing 5 changed files with 1,439 additions and 1,838 deletions.
20 changes: 8 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,38 @@
"build": "run-p build:*",
"build:r": "r -p",
"build:ts": "tsc -b",
"clean": "rimraf packages/*/{lib,*.tsbuildinfo}",
"lint": "run-p lint:*",
"lint:es": "cross-env PARSER_NO_WATCH=true eslint . --cache --ext js,md,ts -f friendly",
"lint:ts": "tslint -p . -t stylish",
"lint:tsc": "tsc",
"postinstall": "yarn-deduplicate --strategy fewer || exit 0",
"prelint": "yarn build:ts",
"prerelease": "yarn build",
"pretest": "yarn clean",
"release": "lerna publish --conventional-commits --create-release github --yes",
"test": "jest",
"typecov": "type-coverage"
},
"devDependencies": {
"@1stg/lib-config": "^1.1.9",
"@1stg/tslint-config": "^1.1.0",
"@types/eslint": "^7.2.6",
"@types/eslint": "^7.2.7",
"@types/jest": "^26.0.20",
"@types/node": "^14.14.31",
"@types/react": "^17.0.2",
"@types/node": "^14.14.33",
"@types/react": "^17.0.3",
"@types/rebass": "^4.0.8",
"@types/unist": "^2.0.3",
"eslint-mdx": "link:packages/eslint-mdx/src",
"eslint-plugin-mdx": "link:packages/eslint-plugin-mdx/src",
"lerna": "^3.22.1",
"lerna": "^4.0.0",
"npm-run-all": "^4.1.5",
"react": "^17.0.1",
"ts-jest": "^26.5.2",
"ts-jest": "^26.5.3",
"ts-node": "^9.1.1",
"tslint": "^6.1.3",
"type-coverage": "^2.16.3",
"typescript": "^4.2.2",
"typescript": "^4.2.3",
"yarn-deduplicate": "^3.1.0"
},
"resolutions": {
"@babel/core": "^7.13.8",
"@babel/core": "^7.13.10",
"prettier": "^2.2.1",
"tslib": "^2.1.0"
},
Expand Down Expand Up @@ -88,7 +84,7 @@
]
},
"typeCoverage": {
"atLeast": 99.37,
"atLeast": 99.76,
"cache": true,
"detail": true,
"ignoreAsAssertion": true,
Expand Down
24 changes: 17 additions & 7 deletions packages/eslint-mdx/src/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,23 @@ export class Traverse {
this._enter = enter
}

combineLeftJsxNodes(jsxNodes: Node[]) {
combineLeftJsxNodes(jsxNodes: Node[], parent?: Parent): Node {
const start = jsxNodes[0].position.start
const end = last(jsxNodes).position.end
const end = { ...last(jsxNodes).position.end }
// fix #279
if (parent && parent.position.indent?.length > 0) {
end.offset += parent.position.indent.reduce(
(acc, indent, index) => acc + (index ? indent + 1 : 0),
0,
)
}
return {
type: 'jsx',
data: jsxNodes[0].data,
value: this.code.slice(start.offset, end.offset),
position: {
start: jsxNodes[0].position.start,
end: last(jsxNodes).position.end,
start,
end,
},
}
}
Expand Down Expand Up @@ -95,14 +102,17 @@ export class Traverse {
)
if (firstOpenTagIndex === -1) {
if (hasOpenTag) {
acc.push(this.combineLeftJsxNodes(jsxNodes))
acc.push(this.combineLeftJsxNodes(jsxNodes, parent))
} else {
acc.push(...jsxNodes)
}
} else {
acc.push(
...jsxNodes.slice(0, firstOpenTagIndex),
this.combineLeftJsxNodes(jsxNodes.slice(firstOpenTagIndex)),
this.combineLeftJsxNodes(
jsxNodes.slice(firstOpenTagIndex),
parent,
),
)
}
jsxNodes.length = 0
Expand All @@ -114,7 +124,7 @@ export class Traverse {
acc.push(node)
}
if (index === length - 1 && jsxNodes.length > 0) {
acc.push(this.combineLeftJsxNodes(jsxNodes))
acc.push(this.combineLeftJsxNodes(jsxNodes, parent))
}
return acc
}, [])
Expand Down
2 changes: 2 additions & 0 deletions test/__snapshots__/fixtures.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ Array [
]
`;

exports[`fixtures should match all snapshots: jsx-in-list.mdx 1`] = `Array []`;

exports[`fixtures should match all snapshots: leading-spaces.mdx 1`] = `Array []`;

exports[`fixtures should match all snapshots: markdown.md 1`] = `Array []`;
Expand Down
24 changes: 24 additions & 0 deletions test/fixtures/jsx-in-list.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
- - <div kind="docs-packages-vuetify-preset" story="page">
Vuetify preset
</div>
: some extra text describing the preset
- <div kind="docs-packages-vuetify-preset" story="page">
Vuetify preset
</div>
: some extra text describing the preset
- <div kind="docs-packages-vuetify-preset" story="page">
Vuetify preset
</div>
: some extra text describing the preset
- - <div kind="docs-packages-vuetify-preset" story="page">
Vuetify preset
</div>
: some extra text describing the preset
- <div kind="docs-packages-vuetify-preset" story="page">
Vuetify preset
</div>
: some extra text describing the preset
- <div kind="docs-packages-vuetify-preset" story="page">
Vuetify preset
</div>
: some extra text describing the preset
Loading

0 comments on commit 188c6aa

Please sign in to comment.