Skip to content

Commit e76c483

Browse files
committed
fix: options unchangeable issue for default parser
1 parent e6d1374 commit e76c483

File tree

10 files changed

+110
-28
lines changed

10 files changed

+110
-28
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CHANGELOG.md

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ before_deploy:
2727
deploy:
2828
provider: script
2929
skip_cleanup: true
30-
script: yarn deploy:pre && yarn deploy:run && yarn deploy:post || echo "nothing changed to deploy"
30+
script: git tag -d v$(jq -r '.version' package.json) && yarn run run-s "deploy:*" || echo "nothing changed to deploy"
3131
on:
3232
branch: master

lerna.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"$schema": "http://json.schemastore.org/lerna",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"npmClient": "yarn",
55
"useWorkspaces": true,
66
"command": {

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-mdx",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"description": "ESLint Parser/Plugin for MDX",
55
"repository": "[email protected]:rx-ts/eslint-plugin-mdx.git",
66
"author": "JounQin <[email protected]>",
@@ -36,6 +36,7 @@
3636
"jest": "^24.8.0",
3737
"lerna": "^3.16.4",
3838
"lint-staged": "^9.2.1",
39+
"npm-run-all": "^4.1.5",
3940
"prettier": "1.18.2",
4041
"prettier-config-1stg": "^0.1.0",
4142
"react": "^16.8.6",

packages/eslint-mdx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-mdx",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"description": "ESLint Parser for MDX",
55
"repository": "[email protected]:rx-ts/eslint-plugin-mdx.git",
66
"author": "JounQin <[email protected]>",

packages/eslint-mdx/src/parser.ts

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
} from './helper'
1515
import { isComment, COMMENT_CONTENT_REGEX } from './regexp'
1616
import { traverse } from './traverse'
17-
import { ParserOptions, LocationError, Comment } from './types'
17+
import { ParserOptions, LocationError, Comment, ParserFn } from './types'
1818

1919
import { AST, Linter } from 'eslint'
2020
import { Parent, Node } from 'unist'
@@ -32,29 +32,21 @@ export const LOC_ERROR_PROPERTIES = ['column', 'index', 'lineNumber'] as const
3232

3333
export const DEFAULT_EXTENSIONS: readonly string[] = ['.mdx']
3434

35-
export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
36-
ecmaFeatures: { jsx: true },
37-
ecmaVersion: new Date().getUTCFullYear() as Linter.ParserOptions['ecmaVersion'],
38-
sourceType: 'module',
39-
}
40-
4135
export class Parser {
4236
// @internal
43-
private parser = normalizeParser(this.options.parser)
37+
private parser: ParserFn
4438

4539
// @internal
4640
private ast: AST.Program
4741

4842
// @internal
49-
private services = {
50-
JSXElementsWithHTMLComments: [] as Node[],
43+
private services: {
44+
JSXElementsWithHTMLComments: Node[]
5145
}
5246

5347
constructor(
5448
// @internal
55-
private code = '',
56-
// @internal
57-
private options: ParserOptions = DEFAULT_PARSER_OPTIONS,
49+
private options?: ParserOptions,
5850
) {
5951
this.parse = this.parse.bind(this)
6052
this.parseForESLint = this.parseForESLint.bind(this)
@@ -132,29 +124,34 @@ export class Parser {
132124
return this.normalizeJsxNodes(node)
133125
}
134126

135-
parse(code = this.code, options = this.options) {
127+
parse(code: string, options: ParserOptions) {
136128
return this.parseForESLint(code, options).ast
137129
}
138130

139-
parseForESLint(code = this.code, options = this.options) {
131+
parseForESLint(code: string, options: ParserOptions) {
132+
this.options = options
133+
140134
if (
141-
!DEFAULT_EXTENSIONS.concat(this.options.extensions || []).includes(
135+
!DEFAULT_EXTENSIONS.concat(options.extensions || []).includes(
142136
path.extname(options.filePath),
143137
)
144138
) {
145139
return this.eslintParse(code, options)
146140
}
147141

148-
const root = mdxProcessor.parse(this.code) as Parent
142+
const root = mdxProcessor.parse(code) as Parent
149143

150144
this.ast = {
151145
...normalizePosition(root.position),
152146
type: 'Program',
153-
sourceType: this.options.sourceType || 'module',
147+
sourceType: options.sourceType || 'module',
154148
body: [],
155149
comments: [],
156150
tokens: [],
157151
}
152+
this.services = {
153+
JSXElementsWithHTMLComments: [],
154+
}
158155

159156
traverse(root, {
160157
enter: (node, parent) => {
@@ -175,7 +172,11 @@ export class Parser {
175172
}
176173

177174
// @internal
178-
private eslintParse(code = this.code, options = this.options) {
175+
private eslintParse(code: string, options = this.options) {
176+
if (!this.parser || options !== this.options) {
177+
this.options = options
178+
this.parser = normalizeParser(options.parser)
179+
}
179180
const program = this.parser(code, options)
180181
return ('ast' in program && program.ast
181182
? program

packages/eslint-plugin-mdx/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rxts/eslint-plugin-mdx",
3-
"version": "0.9.4",
3+
"version": "0.9.5",
44
"description": "ESLint Plugin for MDX",
55
"repository": "[email protected]:rx-ts/eslint-plugin-mdx.git",
66
"author": "JounQin <[email protected]>",
@@ -21,7 +21,7 @@
2121
"eslint": ">=5.0.0"
2222
},
2323
"dependencies": {
24-
"eslint-mdx": "^0.9.3",
24+
"eslint-mdx": "^0.9.5",
2525
"remark-mdx": "^1.1.5",
2626
"remark-parse": "^7.0.0",
2727
"unified": "^8.3.2"

test/helper.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { ParserOptions, Parser } from 'eslint-mdx'
2+
3+
import { Linter } from 'eslint'
4+
5+
export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
6+
ecmaFeatures: {
7+
jsx: true,
8+
},
9+
ecmaVersion: new Date().getUTCFullYear() as Linter.ParserOptions['ecmaVersion'],
10+
sourceType: 'module',
11+
}
12+
13+
export const parser = new Parser(DEFAULT_PARSER_OPTIONS)

test/parser.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { first, mdxProcessor, parser } from 'eslint-mdx'
1+
import { first, mdxProcessor } from 'eslint-mdx'
2+
3+
import { parser } from './helper'
24

35
import { Node } from 'unist'
46

yarn.lock

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,11 @@ array-equal@^1.0.0:
16801680
resolved "https://registry.yarnpkg.com/array-equal/-/array-equal-1.0.0.tgz#8c2a5ef2472fd9ea742b04c77a75093ba2757c93"
16811681
integrity sha1-jCpe8kcv2ep0KwTHenUJO6J1fJM=
16821682

1683+
array-filter@~0.0.0:
1684+
version "0.0.1"
1685+
resolved "https://registry.yarnpkg.com/array-filter/-/array-filter-0.0.1.tgz#7da8cf2e26628ed732803581fd21f67cacd2eeec"
1686+
integrity sha1-fajPLiZijtcygDWB/SH2fKzS7uw=
1687+
16831688
array-find-index@^1.0.1:
16841689
version "1.0.2"
16851690
resolved "https://registry.yarnpkg.com/array-find-index/-/array-find-index-1.0.2.tgz#df010aa1287e164bbda6f9723b0a96a1ec4187a1"
@@ -1698,6 +1703,16 @@ array-includes@^3.0.3:
16981703
define-properties "^1.1.2"
16991704
es-abstract "^1.7.0"
17001705

1706+
array-map@~0.0.0:
1707+
version "0.0.0"
1708+
resolved "https://registry.yarnpkg.com/array-map/-/array-map-0.0.0.tgz#88a2bab73d1cf7bcd5c1b118a003f66f665fa662"
1709+
integrity sha1-iKK6tz0c97zVwbEYoAP2b2ZfpmI=
1710+
1711+
array-reduce@~0.0.0:
1712+
version "0.0.0"
1713+
resolved "https://registry.yarnpkg.com/array-reduce/-/array-reduce-0.0.0.tgz#173899d3ffd1c7d9383e4479525dbe278cab5f2b"
1714+
integrity sha1-FziZ0//Rx9k4PkR5Ul2+J4yrXys=
1715+
17011716
array-union@^1.0.2:
17021717
version "1.0.2"
17031718
resolved "https://registry.yarnpkg.com/array-union/-/array-union-1.0.2.tgz#9a34410e4f4e3da23dea375be5be70f24778ec39"
@@ -2937,7 +2952,7 @@ error-ex@^1.2.0, error-ex@^1.3.1:
29372952
dependencies:
29382953
is-arrayish "^0.2.1"
29392954

2940-
es-abstract@^1.11.0, es-abstract@^1.12.0, es-abstract@^1.5.1, es-abstract@^1.7.0:
2955+
es-abstract@^1.11.0, es-abstract@^1.12.0, es-abstract@^1.4.3, es-abstract@^1.5.1, es-abstract@^1.7.0:
29412956
version "1.13.0"
29422957
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.13.0.tgz#ac86145fdd5099d8dd49558ccba2eaf9b88e24e9"
29432958
integrity sha512-vDZfg/ykNxQVwup/8E1BZhVzFfBxs9NqMzGcvIJrqg5k2/5Za2bWo40dK2J1pgLngZ7c+Shh8lwYtLGyrwPutg==
@@ -3633,7 +3648,7 @@ fsevents@^1.2.7:
36333648
nan "^2.12.1"
36343649
node-pre-gyp "^0.12.0"
36353650

3636-
function-bind@^1.1.1:
3651+
function-bind@^1.0.2, function-bind@^1.1.1:
36373652
version "1.1.1"
36383653
resolved "https://registry.yarnpkg.com/function-bind/-/function-bind-1.1.1.tgz#a56899d3ea3c9bab874bb9773b7c5ede92f4895d"
36393654
integrity sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==
@@ -5022,6 +5037,11 @@ jsonfile@^4.0.0:
50225037
optionalDependencies:
50235038
graceful-fs "^4.1.6"
50245039

5040+
jsonify@~0.0.0:
5041+
version "0.0.0"
5042+
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
5043+
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=
5044+
50255045
jsonparse@^1.2.0:
50265046
version "1.3.1"
50275047
resolved "https://registry.yarnpkg.com/jsonparse/-/jsonparse-1.3.1.tgz#3f4dae4a91fac315f71062f8521cc239f1366280"
@@ -5457,6 +5477,11 @@ mem@^4.0.0:
54575477
mimic-fn "^2.0.0"
54585478
p-is-promise "^2.0.0"
54595479

5480+
memorystream@^0.3.1:
5481+
version "0.3.1"
5482+
resolved "https://registry.yarnpkg.com/memorystream/-/memorystream-0.3.1.tgz#86d7090b30ce455d63fbae12dda51a47ddcaf9b2"
5483+
integrity sha1-htcJCzDORV1j+64S3aUaR93K+bI=
5484+
54605485
54615486
version "5.0.0"
54625487
resolved "https://registry.yarnpkg.com/meow/-/meow-5.0.0.tgz#dfc73d63a9afc714a5e371760eb5c88b91078aa4"
@@ -5905,6 +5930,21 @@ npm-pick-manifest@^2.2.3:
59055930
npm-package-arg "^6.0.0"
59065931
semver "^5.4.1"
59075932

5933+
npm-run-all@^4.1.5:
5934+
version "4.1.5"
5935+
resolved "https://registry.yarnpkg.com/npm-run-all/-/npm-run-all-4.1.5.tgz#04476202a15ee0e2e214080861bff12a51d98fba"
5936+
integrity sha512-Oo82gJDAVcaMdi3nuoKFavkIHBRVqQ1qvMb+9LHk/cF4P6B2m8aP04hGf7oL6wZ9BuGwX1onlLhpuoofSyoQDQ==
5937+
dependencies:
5938+
ansi-styles "^3.2.1"
5939+
chalk "^2.4.1"
5940+
cross-spawn "^6.0.5"
5941+
memorystream "^0.3.1"
5942+
minimatch "^3.0.4"
5943+
pidtree "^0.3.0"
5944+
read-pkg "^3.0.0"
5945+
shell-quote "^1.6.1"
5946+
string.prototype.padend "^3.0.0"
5947+
59085948
npm-run-path@^2.0.0:
59095949
version "2.0.2"
59105950
resolved "https://registry.yarnpkg.com/npm-run-path/-/npm-run-path-2.0.2.tgz#35a9232dfa35d7067b4cb2ddf2357b1871536c5f"
@@ -6395,6 +6435,11 @@ picomatch@^2.0.5:
63956435
resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.0.7.tgz#514169d8c7cd0bdbeecc8a2609e34a7163de69f6"
63966436
integrity sha512-oLHIdio3tZ0qH76NybpeneBhYVj0QFTfXEFTc/B3zKQspYfYYkWYgFsmzo+4kvId/bQRcNkVeguI3y+CD22BtA==
63976437

6438+
pidtree@^0.3.0:
6439+
version "0.3.0"
6440+
resolved "https://registry.yarnpkg.com/pidtree/-/pidtree-0.3.0.tgz#f6fada10fccc9f99bf50e90d0b23d72c9ebc2e6b"
6441+
integrity sha512-9CT4NFlDcosssyg8KVFltgokyKZIFjoBxw8CTGy+5F38Y1eQWrt8tRayiUOXE+zVKQnYu5BR8JjCtvK3BcnBhg==
6442+
63986443
pify@^2.0.0, pify@^2.3.0:
63996444
version "2.3.0"
64006445
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
@@ -7190,6 +7235,16 @@ shebang-regex@^1.0.0:
71907235
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
71917236
integrity sha1-2kL0l0DAtC2yypcoVxyxkMmO/qM=
71927237

7238+
shell-quote@^1.6.1:
7239+
version "1.6.1"
7240+
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.6.1.tgz#f4781949cce402697127430ea3b3c5476f481767"
7241+
integrity sha1-9HgZSczkAmlxJ0MOo7PFR29IF2c=
7242+
dependencies:
7243+
array-filter "~0.0.0"
7244+
array-map "~0.0.0"
7245+
array-reduce "~0.0.0"
7246+
jsonify "~0.0.0"
7247+
71937248
shellwords@^0.1.1:
71947249
version "0.1.1"
71957250
resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.1.tgz#d6b9181c1a48d397324c84871efbcfc73fc0654b"
@@ -7500,6 +7555,15 @@ string-width@^3.0.0, string-width@^3.1.0:
75007555
is-fullwidth-code-point "^2.0.0"
75017556
strip-ansi "^5.1.0"
75027557

7558+
string.prototype.padend@^3.0.0:
7559+
version "3.0.0"
7560+
resolved "https://registry.yarnpkg.com/string.prototype.padend/-/string.prototype.padend-3.0.0.tgz#f3aaef7c1719f170c5eab1c32bf780d96e21f2f0"
7561+
integrity sha1-86rvfBcZ8XDF6rHDK/eA2W4h8vA=
7562+
dependencies:
7563+
define-properties "^1.1.2"
7564+
es-abstract "^1.4.3"
7565+
function-bind "^1.0.2"
7566+
75037567
string_decoder@^1.1.1:
75047568
version "1.2.0"
75057569
resolved "https://registry.yarnpkg.com/string_decoder/-/string_decoder-1.2.0.tgz#fe86e738b19544afe70469243b2a1ee9240eae8d"

0 commit comments

Comments
 (0)