Skip to content

Commit 3aa0f20

Browse files
authored
refactor: remove unnecessary loadEsmModule helper (#562)
1 parent 29b034f commit 3aa0f20

File tree

10 files changed

+90
-127
lines changed

10 files changed

+90
-127
lines changed

.changeset/eleven-cobras-care.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"eslint-mdx": minor
3+
"eslint-plugin-mdx": minor
4+
---
5+
6+
refactor: remove unnecessary `loadEsmModule` helper, use native `import()` instead

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</a>
1111
</p>
1212

13-
[![GitHub Actions](https://github.com/mdx-js/eslint-mdx/workflows/CI/badge.svg)](https://github.com/mdx-js/eslint-mdx/actions/workflows/ci.yml)
13+
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mdx-js/eslint-mdx/ci.yml?branch=master)](https://github.com/mdx-js/eslint-mdx/actions/workflows/ci.yml?query=branch%3Amaster)
1414
[![Codecov](https://img.shields.io/codecov/c/gh/mdx-js/eslint-mdx)](https://codecov.io/gh/mdx-js/eslint-mdx)
1515
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fmdx-js%2Feslint-mdx%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
1616
[![GitHub release](https://img.shields.io/github/release/mdx-js/eslint-mdx)](https://github.com/mdx-js/eslint-mdx/releases)

packages/eslint-mdx/README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</a>
1111
</p>
1212

13-
[![GitHub Actions](https://github.com/mdx-js/eslint-mdx/workflows/CI/badge.svg)](https://github.com/mdx-js/eslint-mdx/actions/workflows/ci.yml)
13+
[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/mdx-js/eslint-mdx/ci.yml?branch=master)](https://github.com/mdx-js/eslint-mdx/actions/workflows/ci.yml?query=branch%3Amaster)
1414
[![Codecov](https://img.shields.io/codecov/c/gh/mdx-js/eslint-mdx)](https://codecov.io/gh/mdx-js/eslint-mdx)
1515
[![type-coverage](https://img.shields.io/badge/dynamic/json.svg?label=type-coverage&prefix=%E2%89%A5&suffix=%&query=$.typeCoverage.atLeast&uri=https%3A%2F%2Fraw.githubusercontent.com%2Fmdx-js%2Feslint-mdx%2Fmaster%2Fpackage.json)](https://github.com/plantain-00/type-coverage)
1616
[![GitHub release](https://img.shields.io/github/release/mdx-js/eslint-mdx)](https://github.com/mdx-js/eslint-mdx/releases)
@@ -235,21 +235,21 @@ If you're using [remark-lint][] feature with [Prettier][] both together, you can
235235
}
236236
```
237237

238+
[![Sponsors](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)
239+
238240
## Sponsors
239241

240-
| 1stG | RxTS | UnTS |
241-
| ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
242-
| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |
242+
| 1stG | RxTS | UnTS |
243+
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
244+
| [![1stG Open Collective sponsors](https://opencollective.com/1stG/organizations.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective sponsors](https://opencollective.com/rxts/organizations.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective sponsors](https://opencollective.com/unts/organizations.svg)](https://opencollective.com/unts) |
243245

244246
[![unified Open Collective backers and sponsors](https://opencollective.com/unified/organizations.svg)](https://opencollective.com/unified)
245247

246248
## Backers
247249

248-
[![Backers](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)
249-
250-
| 1stG | RxTS | UnTS |
251-
| -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
252-
| [![1stG Open Collective backers and sponsors](https://opencollective.com/1stG/individuals.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers and sponsors](https://opencollective.com/rxts/individuals.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers and sponsors](https://opencollective.com/unts/individuals.svg)](https://opencollective.com/unts) |
250+
| 1stG | RxTS | UnTS |
251+
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
252+
| [![1stG Open Collective backers](https://opencollective.com/1stG/individuals.svg)](https://opencollective.com/1stG) | [![RxTS Open Collective backers](https://opencollective.com/rxts/individuals.svg)](https://opencollective.com/rxts) | [![UnTS Open Collective backers](https://opencollective.com/unts/individuals.svg)](https://opencollective.com/unts) |
253253

254254
[![unified Open Collective backers and sponsors](https://opencollective.com/unified/individuals.svg)](https://opencollective.com/unified)
255255

packages/eslint-mdx/src/helpers.ts

+8-33
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import type { Point } from 'unist'
77

88
import type { CjsRequire, NormalPosition } from './types.js'
99

10-
export const last = <T>(items: T[] | readonly T[]) =>
11-
// eslint-disable-next-line unicorn/prefer-at -- FIXME: Node 16.6+ required
12-
items && items[items.length - 1]
13-
1410
export const arrayify = <T, R = T extends Array<infer S> ? S : T>(
1511
...args: T[]
1612
) =>
@@ -45,30 +41,9 @@ export const getPhysicalFilename = (
4541
return filename
4642
}
4743

48-
/**
49-
* ! copied from https://github.com/just-jeb/angular-builders/blob/master/packages/custom-webpack/src/utils.ts#L53-L67
50-
*
51-
* This uses a dynamic import to load a module which may be ESM.
52-
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
53-
* will currently, unconditionally downlevel dynamic import into a require call.
54-
* require calls cannot load ESM code and will result in a runtime error. To workaround
55-
* this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
56-
* Once TypeScript provides support for keeping the dynamic import this workaround can
57-
* be dropped.
58-
*
59-
* @param modulePath The path of the module to load.
60-
* @returns A Promise that resolves to the dynamically imported module.
61-
*/
62-
/* istanbul ignore next */
63-
export const loadEsmModule = <T>(modulePath: URL | string): Promise<T> =>
64-
// eslint-disable-next-line @typescript-eslint/no-implied-eval, @typescript-eslint/no-unsafe-call
65-
new Function('modulePath', `return import(modulePath);`)(
66-
modulePath,
67-
) as Promise<T>
68-
6944
/* istanbul ignore next -- used in worker */
70-
export const getPositionAtFactory = (text: string) => {
71-
const lines = text.split('\n')
45+
export const getPositionAtFactory = (code: string) => {
46+
const lines = code.split('\n')
7247
return (offset: number): Position => {
7348
let currOffset = 0
7449

@@ -91,19 +66,19 @@ export const getPositionAtFactory = (text: string) => {
9166
export const normalizePosition = ({
9267
start,
9368
end,
94-
text,
69+
code,
9570
}: {
9671
start: Point | { offset: number }
9772
end: Point | { offset: number }
98-
text?: string
73+
code?: string
9974
}): NormalPosition => {
10075
const startOffset = start.offset
10176
const endOffset = end.offset
10277
const range: [number, number] = [startOffset, endOffset]
10378
const getPositionAt =
104-
text == null
79+
code == null
10580
? null
106-
: /* istanbul ignore next -- used in worker */ getPositionAtFactory(text)
81+
: /* istanbul ignore next -- used in worker */ getPositionAtFactory(code)
10782
return {
10883
start: startOffset,
10984
end: endOffset,
@@ -123,10 +98,10 @@ export const normalizePosition = ({
12398

12499
/* istanbul ignore next -- used in worker */
125100
export const prevCharOffsetFactory =
126-
(text: string) =>
101+
(code: string) =>
127102
(offset: number): number => {
128103
for (let i = offset; i >= 0; i--) {
129-
const char = text[i]
104+
const char = code[i]
130105
if (/^\S$/.test(char)) {
131106
return i
132107
}

packages/eslint-mdx/src/parser.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,12 @@ export class Parser {
4747
)
4848
}
4949

50-
const physicalFilename = getPhysicalFilename(filePath)
51-
5250
let result: WorkerParseResult
5351

5452
try {
5553
result = performSyncWork({
56-
fileOptions: {
57-
path: physicalFilename,
58-
value: code,
59-
},
60-
physicalFilename,
54+
filePath: getPhysicalFilename(filePath),
55+
code,
6156
isMdx,
6257
ignoreRemarkConfig,
6358
})

packages/eslint-mdx/src/types.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { AST, Linter } from 'eslint'
33
import type { BaseNode, Program } from 'estree'
44
import type { JSXElement } from 'estree-jsx'
55
import type { Root } from 'mdast'
6-
import type { VFileOptions } from 'vfile'
76
import type { VFileMessage } from 'vfile-message'
87

98
export interface ParserOptions extends Linter.ParserOptions {
@@ -24,8 +23,9 @@ export interface NormalPosition {
2423
}
2524

2625
export interface WorkerOptions {
27-
fileOptions: VFileOptions
28-
physicalFilename: string
26+
filePath: string
27+
code: string
28+
cwd?: string
2929
isMdx: boolean
3030
process?: boolean
3131
ignoreRemarkConfig?: boolean
@@ -40,7 +40,7 @@ export interface WorkerParseResult {
4040

4141
export interface WorkerProcessResult {
4242
messages: VFileMessage[]
43-
content: string
43+
content?: string
4444
}
4545

4646
export type WorkerResult = WorkerParseResult | WorkerProcessResult

0 commit comments

Comments
 (0)