Skip to content

Commit

Permalink
fix: skip combine jsx nodes for root/jsx node
Browse files Browse the repository at this point in the history
refactor parser, make the API simple
  • Loading branch information
JounQin committed Aug 5, 2019
1 parent 2e9d4c1 commit 90583f7
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 260 deletions.
65 changes: 26 additions & 39 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,136 +2,123 @@

All notable changes to this project will be documented in this file. See [standard-version](/~https://github.com/conventional-changelog/standard-version) for commit guidelines.

### [0.9.3](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.1...v0.9.3) (2019-08-04)
### [0.9.4](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.3...v0.9.4) (2019-08-04)

### Bug Fixes

- adjacent jsx nodes should be allowed in mdx ([8456381](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/8456381))

### [0.9.3](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.1...v0.9.3) (2019-08-04)

### Bug Fixes

* error from typescript parser in plain object ([f634e40](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/f634e40))
- error from typescript parser in plain object ([f634e40](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/f634e40))

### [0.9.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.0...v0.9.1) (2019-08-03)


### Bug Fixes

* always extends base config ([530160a](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/530160a))
- always extends base config ([530160a](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/530160a))

## [0.9.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.8.1...v0.9.0) (2019-08-03)


### Features

* support other extensions, detect parsers automatically ([5386098](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5386098))
- support other extensions, detect parsers automatically ([5386098](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5386098))

### [0.8.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.7.1...v0.8.1) (2019-08-03)


### Features

* split into two packages, use lerna as publisher ([#22](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/21)
- split into two packages, use lerna as publisher ([#22](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/21)

## [0.8.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.7.1...v0.8.0) (2019-08-03)


### Features

* split into two packages, use lerna as publisher ([#22](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/21)
- split into two packages, use lerna as publisher ([#22](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/21)

### [0.7.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.7.0...v0.7.1) (2019-08-03)


### Features

* export a parse function for compatibility usage ([0fdaf9f](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/0fdaf9f))
- export a parse function for compatibility usage ([0fdaf9f](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/0fdaf9f))

## [0.7.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.6.0...v0.7.0) (2019-08-02)


### Features

* add new rule no-unescaped-entities ([dca8633](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/dca8633))
- add new rule no-unescaped-entities ([dca8633](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/dca8633))

## [0.6.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.5.0...v0.6.0) (2019-08-02)


### Features

* add new rule `no-jsx-html-comments`, close [#13](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/13) ([67ba91e](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/67ba91e))
* processor support for normalize comments in jsx [ci skip] ([114831c](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/114831c))
- add new rule `no-jsx-html-comments`, close [#13](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/13) ([67ba91e](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/67ba91e))
- processor support for normalize comments in jsx [ci skip](<[114831c](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/114831c)>)

## [0.5.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.4.1...v0.5.0) (2019-07-31)


### Features

* add `@rxts/mdx/no-unused-expressions` rule to replace eslint's ([5d62b55](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5d62b55))
- add `@rxts/mdx/no-unused-expressions` rule to replace eslint's ([5d62b55](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5d62b55))

## [0.4.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.4.0...v0.4.1) (2019-07-31)


### Features

* perf: extract `parseMdx`, exports everything in package ([1f965a1](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/1f965a1))
- perf: extract `parseMdx`, exports everything in package ([1f965a1](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/1f965a1))

## [0.4.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.3.1...v0.4.0) (2019-07-31)


### Features

* add body and comments into ast, fix [#12](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/12) ([5b28bd5](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5b28bd5))
- add body and comments into ast, fix [#12](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/12) ([5b28bd5](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5b28bd5))

### [0.3.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.3.0...v0.3.1) (2019-07-30)


### Bug Fixes

* `.*` could not match multi lines, use `[\s\S]*` instead, close [#4](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/4) ([f7e7efe](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/f7e7efe))
- `.*` could not match multi lines, use `[\s\S]*` instead, close [#4](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/4) ([f7e7efe](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/f7e7efe))

## [0.3.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.2.1...v0.3.0) (2019-07-30)


### Bug Fixes

* inline jsx and comment parsing error, close [#4](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/4), [#7](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/7) ([5297a0b](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5297a0b))
- inline jsx and comment parsing error, close [#4](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/4), [#7](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/7) ([5297a0b](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/5297a0b))

### [0.2.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.2.0...v0.2.1) (2019-07-30)


### Bug Fixes

* eslint@5 is actually supported ([e470ddc](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/e470ddc))
- eslint@5 is actually supported ([e470ddc](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/e470ddc))

## [0.2.0](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.3...v0.2.0) (2019-07-30)


### Features

* support custom parser like babel-eslint ([b718574](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/b718574))
- support custom parser like babel-eslint ([b718574](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/b718574))

### [0.1.3](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.2...v0.1.3) (2019-07-29)


### Bug Fixes

* upgrade peer dependency eslint to >= 6.0.0 ([#3](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/3) [#4](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/4)) ([f0ab288](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/f0ab288))
- upgrade peer dependency eslint to >= 6.0.0 ([#3](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/3) [#4](/~https://github.com/rx-ts/eslint-plugin-mdx/issues/4)) ([f0ab288](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/f0ab288))

### [0.1.2](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.1...v0.1.2) (2019-07-29)


### Bug Fixes

* show correct column and line on lint error ([90c5390](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/90c5390))
- show correct column and line on lint error ([90c5390](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/90c5390))

### [0.1.1](/~https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.0...v0.1.1) (2019-07-29)


### Bug Fixes

* overrides in node_modules seems not working ([d2f1535](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/d2f1535))
- overrides in node_modules seems not working ([d2f1535](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/d2f1535))

## 0.1.0 (2019-07-29)


### Features

* traverse ast nodes ([6a71e25](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/6a71e25))
- traverse ast nodes ([6a71e25](/~https://github.com/rx-ts/eslint-plugin-mdx/commit/6a71e25))
53 changes: 52 additions & 1 deletion packages/eslint-mdx/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,67 @@
import { Arrayable, JsxNode, JsxType, JsxTypes } from './types'
// eslint-disable-next-line @typescript-eslint/no-triple-slash-reference
/// <reference path="../typings.d.ts" />

import { parse as esParse } from 'espree'

import {
Arrayable,
JsxNode,
JsxType,
JsxTypes,
ParserOptions,
ParserFn,
} from './types'

import { Position } from 'unist'
import { AST } from 'eslint'
// `SourceLocation` is not exported from estree, but it is actually working
// eslint-disable-next-line import/named
import { SourceLocation } from 'estree'

export const FALLBACK_PARSERS = [
'@typescript-eslint/parser',
'babel-eslint',
] as const

export const JSX_TYPES: JsxTypes = ['JSXElement', 'JSXFragment']

export const isJsxNode = (node: { type: string }): node is JsxNode =>
JSX_TYPES.includes(node.type as JsxType)

export const normalizeParser = (parser: ParserOptions['parser']) => {
if (parser) {
if (typeof parser === 'string') {
parser = require(parser)
}

if (typeof parser === 'object') {
parser = parser.parseForESLint || parser.parse
}

if (typeof parser !== 'function') {
throw new TypeError(
`Invalid custom parser for \`eslint-plugin-mdx\`: ${parser}`,
)
}
} else {
// try to load FALLBACK_PARSERS automatically
for (const fallback of FALLBACK_PARSERS) {
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fallbackParser = require(fallback)
parser = fallbackParser.parseForESLint || fallbackParser.parse
break
} catch (e) {}
}

if (typeof parser !== 'function') {
parser = esParse as ParserFn
}

return parser
}
}

export const normalizePosition = (position: Position) => {
const start = position.start.offset
const end = position.end.offset
Expand Down
Loading

0 comments on commit 90583f7

Please sign in to comment.