Skip to content

refactor: remove unnecessary loadEsmModule helper #562

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/eleven-cobras-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"eslint-mdx": minor
"eslint-plugin-mdx": minor
---

refactor: remove unnecessary `loadEsmModule` helper, use native `import()` instead
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</a>
</p>

[![GitHub Actions](https://github.com/mdx-js/eslint-mdx/workflows/CI/badge.svg)](https://github.com/mdx-js/eslint-mdx/actions/workflows/ci.yml)
[![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)
[![Codecov](https://img.shields.io/codecov/c/gh/mdx-js/eslint-mdx)](https://codecov.io/gh/mdx-js/eslint-mdx)
[![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)
[![GitHub release](https://img.shields.io/github/release/mdx-js/eslint-mdx)](https://github.com/mdx-js/eslint-mdx/releases)
Expand Down
18 changes: 9 additions & 9 deletions packages/eslint-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</a>
</p>

[![GitHub Actions](https://github.com/mdx-js/eslint-mdx/workflows/CI/badge.svg)](https://github.com/mdx-js/eslint-mdx/actions/workflows/ci.yml)
[![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)
[![Codecov](https://img.shields.io/codecov/c/gh/mdx-js/eslint-mdx)](https://codecov.io/gh/mdx-js/eslint-mdx)
[![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)
[![GitHub release](https://img.shields.io/github/release/mdx-js/eslint-mdx)](https://github.com/mdx-js/eslint-mdx/releases)
Expand Down Expand Up @@ -235,21 +235,21 @@ If you're using [remark-lint][] feature with [Prettier][] both together, you can
}
```

[![Sponsors](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)

## Sponsors

| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| [![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) |
| 1stG | RxTS | UnTS |
| ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| [![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) |

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

## Backers

[![Backers](https://raw.githubusercontent.com/1stG/static/master/sponsors.svg)](https://github.com/sponsors/JounQin)

| 1stG | RxTS | UnTS |
| -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------- |
| [![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) |
| 1stG | RxTS | UnTS |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| [![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) |

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

Expand Down
41 changes: 8 additions & 33 deletions packages/eslint-mdx/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import type { Point } from 'unist'

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

export const last = <T>(items: T[] | readonly T[]) =>
// eslint-disable-next-line unicorn/prefer-at -- FIXME: Node 16.6+ required
items && items[items.length - 1]

export const arrayify = <T, R = T extends Array<infer S> ? S : T>(
...args: T[]
) =>
Expand Down Expand Up @@ -45,30 +41,9 @@ export const getPhysicalFilename = (
return filename
}

/**
* ! copied from https://github.com/just-jeb/angular-builders/blob/master/packages/custom-webpack/src/utils.ts#L53-L67
*
* This uses a dynamic import to load a module which may be ESM.
* CommonJS code can load ESM code via a dynamic import. Unfortunately, TypeScript
* will currently, unconditionally downlevel dynamic import into a require call.
* require calls cannot load ESM code and will result in a runtime error. To workaround
* this, a Function constructor is used to prevent TypeScript from changing the dynamic import.
* Once TypeScript provides support for keeping the dynamic import this workaround can
* be dropped.
*
* @param modulePath The path of the module to load.
* @returns A Promise that resolves to the dynamically imported module.
*/
/* istanbul ignore next */
export const loadEsmModule = <T>(modulePath: URL | string): Promise<T> =>
// eslint-disable-next-line @typescript-eslint/no-implied-eval, @typescript-eslint/no-unsafe-call
new Function('modulePath', `return import(modulePath);`)(
modulePath,
) as Promise<T>

/* istanbul ignore next -- used in worker */
export const getPositionAtFactory = (text: string) => {
const lines = text.split('\n')
export const getPositionAtFactory = (code: string) => {
const lines = code.split('\n')
return (offset: number): Position => {
let currOffset = 0

Expand All @@ -91,19 +66,19 @@ export const getPositionAtFactory = (text: string) => {
export const normalizePosition = ({
start,
end,
text,
code,
}: {
start: Point | { offset: number }
end: Point | { offset: number }
text?: string
code?: string
}): NormalPosition => {
const startOffset = start.offset
const endOffset = end.offset
const range: [number, number] = [startOffset, endOffset]
const getPositionAt =
text == null
code == null
? null
: /* istanbul ignore next -- used in worker */ getPositionAtFactory(text)
: /* istanbul ignore next -- used in worker */ getPositionAtFactory(code)
return {
start: startOffset,
end: endOffset,
Expand All @@ -123,10 +98,10 @@ export const normalizePosition = ({

/* istanbul ignore next -- used in worker */
export const prevCharOffsetFactory =
(text: string) =>
(code: string) =>
(offset: number): number => {
for (let i = offset; i >= 0; i--) {
const char = text[i]
const char = code[i]
if (/^\S$/.test(char)) {
return i
}
Expand Down
9 changes: 2 additions & 7 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,12 @@ export class Parser {
)
}

const physicalFilename = getPhysicalFilename(filePath)

let result: WorkerParseResult

try {
result = performSyncWork({
fileOptions: {
path: physicalFilename,
value: code,
},
physicalFilename,
filePath: getPhysicalFilename(filePath),
code,
isMdx,
ignoreRemarkConfig,
})
Expand Down
8 changes: 4 additions & 4 deletions packages/eslint-mdx/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { AST, Linter } from 'eslint'
import type { BaseNode, Program } from 'estree'
import type { JSXElement } from 'estree-jsx'
import type { Root } from 'mdast'
import type { VFileOptions } from 'vfile'
import type { VFileMessage } from 'vfile-message'

export interface ParserOptions extends Linter.ParserOptions {
Expand All @@ -24,8 +23,9 @@ export interface NormalPosition {
}

export interface WorkerOptions {
fileOptions: VFileOptions
physicalFilename: string
filePath: string
code: string
cwd?: string
isMdx: boolean
process?: boolean
ignoreRemarkConfig?: boolean
Expand All @@ -40,7 +40,7 @@ export interface WorkerParseResult {

export interface WorkerProcessResult {
messages: VFileMessage[]
content: string
content?: string
}

export type WorkerResult = WorkerParseResult | WorkerProcessResult
Expand Down
Loading
Loading