Skip to content

Commit 6d58ad8

Browse files
committed
fix: always pass through parserOptions
1 parent e276d8e commit 6d58ad8

File tree

11 files changed

+163
-91
lines changed

11 files changed

+163
-91
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ tsNode.register({
44
compilerOptions: {
55
module: 'commonjs',
66
},
7+
transpileOnly: true,
78
})
89

910
module.exports = {

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cache:
77
- directories:
88
- .changelog
99

10-
env: EFF_NO_LINK_RULES=true
10+
env: EFF_NO_LINK_RULES=true PARSER_NO_WATCH=true
1111

1212
before_install:
1313
- curl -o- -L https://yarnpkg.com/install.sh | bash
@@ -18,7 +18,6 @@ before_install:
1818
install: yarn --frozen-lockfile
1919

2020
script:
21-
- yarn build
2221
- yarn lint
2322
- yarn test
2423

package.json

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@
1313
"build": "run-p build:*",
1414
"build:r": "r",
1515
"build:ts": "tsc -b",
16+
"clean": "rimraf packages/*/{lib,*.tsbuildinfo}",
1617
"lint": "run-p lint:*",
17-
"lint:es": "eslint . --ext js,md,ts -f friendly",
18+
"lint:es": "cross-env PARSER_NO_WATCH=true eslint . --cache --ext js,md,ts -f friendly",
1819
"lint:ts": "tslint -p . -t stylish",
19-
"lint:tsc": "tsc --noEmit",
20-
"test": "ts-node --skip-ignore node_modules/.bin/jest",
20+
"lint:tsc": "tsc --incremental --noEmit",
21+
"prelint": "yarn build",
22+
"pretest": "yarn clean",
23+
"test": "ts-node --skip-ignore -T node_modules/.bin/jest",
2124
"type-coverage": "type-coverage --cache --detail --ignore-catch --ignore-files *.d.ts --strict"
2225
},
2326
"devDependencies": {
24-
"@1stg/babel-preset": "^0.7.9",
27+
"@1stg/babel-preset": "^0.7.10",
2528
"@1stg/commitlint-config": "^0.1.2",
26-
"@1stg/eslint-config": "^0.12.21",
29+
"@1stg/eslint-config": "^0.13.1",
2730
"@1stg/husky-config": "^0.3.2",
2831
"@1stg/lint-staged": "^0.8.11",
2932
"@1stg/prettier-config": "^0.4.3",
@@ -36,7 +39,7 @@
3639
"@types/cosmiconfig": "^5.0.3",
3740
"@types/eslint": "^6.1.2",
3841
"@types/jest": "^24.0.19",
39-
"@types/node": "^12.11.1",
42+
"@types/node": "^12.11.2",
4043
"@types/react": "^16.9.9",
4144
"@types/rebass": "^4.0.3",
4245
"@types/unist": "^2.0.3",
@@ -45,7 +48,7 @@
4548
"eslint-plugin-mdx": "link:packages/eslint-plugin-mdx/src",
4649
"husky": "^3.0.9",
4750
"jest": "^24.9.0",
48-
"lerna": "^3.18.1",
51+
"lerna": "^3.18.2",
4952
"lerna-changelog": "^0.8.2",
5053
"lint-staged": "^9.4.2",
5154
"npm-run-all": "^4.1.5",
@@ -58,6 +61,9 @@
5861
"type-coverage": "^2.3.0",
5962
"typescript": "^3.6.4"
6063
},
64+
"resolutions": {
65+
"typescript": "^3.6.4"
66+
},
6167
"commitlint": {
6268
"extends": [
6369
"@1stg"

packages/eslint-mdx/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"eslint": ">=5.0.0"
3030
},
3131
"dependencies": {
32-
"espree": "^6.1.1",
33-
"remark-mdx": "^1.5.0",
32+
"espree": "^6.1.2",
33+
"remark-mdx": "^1.5.1",
3434
"remark-parse": "^7.0.1",
3535
"tslib": "^1.10.0",
3636
"unified": "^8.4.1"

packages/eslint-mdx/src/parser.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ export class Parser {
5959
}
6060

6161
// @internal
62-
private _options = DEFAULT_PARSER_OPTIONS
62+
private readonly _options = DEFAULT_PARSER_OPTIONS
6363

6464
constructor() {
6565
this.parse = this.parse.bind(this)
6666
this.parseForESLint = this.parseForESLint.bind(this)
6767
}
6868

69-
normalizeJsxNode(node: Node, parent?: Parent) {
69+
normalizeJsxNode(node: Node, parent?: Parent, options = this._options) {
7070
const value = node.value as string
7171

7272
if (node.type !== 'jsx' || isComment(value)) {
@@ -135,7 +135,7 @@ export class Parser {
135135
})
136136
}
137137

138-
return this._normalizeJsxNodes(node)
138+
return this._normalizeJsxNodes(node, options)
139139
}
140140

141141
parse(code: string, options: ParserOptions) {
@@ -176,7 +176,7 @@ export class Parser {
176176
return
177177
}
178178

179-
let normalized = this.normalizeJsxNode(node, parent)
179+
let normalized = this.normalizeJsxNode(node, parent, options)
180180
normalized = Array.isArray(normalized) ? normalized : [normalized]
181181
normalized.forEach(_node => this._nodeToAst(_node, options))
182182
},
@@ -190,15 +190,15 @@ export class Parser {
190190
}
191191

192192
// @internal
193-
private _eslintParse(code: string, options = this._options) {
193+
private _eslintParse(code: string, options: ParserOptions) {
194194
if (!this._parser || options.parser !== this._options.parser) {
195195
this._parser = normalizeParser(options.parser)
196196
}
197197
/* istanbul ignore else */
198-
if (options.filePath) {
199-
this._options = options
198+
if (options.filePath && this._options !== options) {
199+
Object.assign(this._options, options)
200200
}
201-
const program = this._parser(code, options)
201+
const program = this._parser(code, this._options)
202202
/* istanbul ignore next */
203203
return ('ast' in program && program.ast
204204
? program
@@ -207,7 +207,10 @@ export class Parser {
207207

208208
// fix adjacent JSX nodes
209209
// @internal
210-
private _normalizeJsxNodes(node: Node): Node | Node[] {
210+
private _normalizeJsxNodes(
211+
node: Node,
212+
options: ParserOptions,
213+
): Node | Node[] {
211214
const value = node.value as string
212215

213216
let program: AST.Program
@@ -216,6 +219,7 @@ export class Parser {
216219
// wrap into single element which is valid jsx but not valid jsx in mdx, so that it won't break on adjacent JSX nodes
217220
program = this._eslintParse(
218221
`${JSX_WRAPPER_START}${value}${JSX_WRAPPER_END}`,
222+
options,
219223
).ast
220224
} catch (e) {
221225
if (hasProperties<LocationError>(e, LOC_ERROR_PROPERTIES)) {
@@ -225,6 +229,7 @@ export class Parser {
225229

226230
e.index += start.offset - OFFSET
227231
e.column =
232+
/* istanbul ignore next */
228233
e.lineNumber > 1 ? e.column : e.column + start.column - OFFSET
229234
e.lineNumber += start.line - 1
230235

packages/eslint-plugin-mdx/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"cosmiconfig": "^5.2.1",
3333
"eslint-mdx": "^1.5.6",
3434
"eslint-plugin-react": ">=7.0.0",
35-
"remark-mdx": "^1.5.0",
35+
"remark-mdx": "^1.5.1",
3636
"remark-parse": "^7.0.1",
3737
"remark-stringify": "^7.0.3",
3838
"tslib": "^1.10.0",
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
export const getGlobals = <
22
T extends Record<string, unknown> | string[],
3-
G extends {}
3+
G extends Record<string, boolean>,
4+
R extends G = G & Record<T extends Array<infer R> ? R : keyof T, false>
45
>(
56
sources: T,
67
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(
8+
): R =>
9+
(Array.isArray(sources)
10+
? (sources as string[])
11+
: Object.keys(sources)
12+
).reduce(
913
(globals, source) =>
1014
Object.assign(globals, {
1115
[source]: false,
1216
}),
13-
// FIXME: find a better solution
14-
// @ts-ignore
15-
initialGlobals,
17+
initialGlobals as R,
1618
)

test/parser.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,23 @@ describe('parser', () => {
150150
parser.parse("import A from 'a'\nimport A from 'a'", parserOptions),
151151
).toThrow("unknown: Identifier 'A' has already been declared")
152152
expect(() => parser.parse('<header><>\n</header>', parserOptions)).toThrow(
153-
'Expected corresponding closing tag for JSX fragment.',
153+
'Expected corresponding JSX closing tag for <>',
154154
)
155155
expect(() => parser.parse('<h1></h2>', parserOptions)).toThrow(
156-
"Expected corresponding JSX closing tag for 'h1'.",
156+
'Expected corresponding JSX closing tag for <h1>',
157157
)
158158
expect(() => parser.parse('Header\n<>', parserOptions)).toThrow(
159-
'Expected corresponding closing tag for JSX fragment.',
159+
'Unexpected token (1:2)',
160160
)
161161
expect(() => parser.parse('<main><</main>', parserOptions)).toThrow(
162-
'Identifier expected.',
162+
'Unexpected token (1:7)',
163163
)
164164
expect(() => parser.parse('<main>{<}</main>', parserOptions)).toThrow(
165-
'Expression expected.',
165+
'Unexpected token (1:8)',
166166
)
167167
expect(() =>
168168
parser.parse('<main>\n<section><</section></main>', parserOptions),
169-
).toThrow('Identifier expected.')
169+
).toThrow('Unexpected token (2:10)')
170170
})
171171

172172
it('should not throw on adjacent JSX nodes', () =>

tsconfig.base.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"extends": "@1stg/tsconfig/loose"
2+
"extends": "@1stg/tsconfig/lib",
3+
"compilerOptions": {
4+
"strictFunctionTypes": false,
5+
"strictNullChecks": false
6+
}
37
}

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"extends": "./tsconfig.base.json",
33
"compilerOptions": {
4-
"baseUrl": ".",
54
"incremental": false,
65
"noEmit": true
76
},

0 commit comments

Comments
 (0)