Skip to content

Commit

Permalink
feat!: Convert to ESM (#259)
Browse files Browse the repository at this point in the history
* feat!: Switch to ESM

fixes #218

* Clean up

* Update docs

* Fix lint errors

* Update package.json

Co-authored-by: Milos Djermanovic <[email protected]>

* Update package.json

Co-authored-by: Milos Djermanovic <[email protected]>

* Fix extra-files names

* Update package.json

Co-authored-by: Milos Djermanovic <[email protected]>

* Apply feedback

* Remove ESLint 8 from CI

---------

Co-authored-by: Milos Djermanovic <[email protected]>
  • Loading branch information
nzakas and mdjermanovic authored Jul 23, 2024
1 parent 618a9f5 commit e0da221
Show file tree
Hide file tree
Showing 17 changed files with 115 additions and 151 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
eslint: [9, 8]
eslint: [9]
node: [22.x, 21.x, 20.x, 18.x, "18.18.0"]
include:
- os: windows-latest
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ jobs:
node-version: lts/*
registry-url: https://registry.npmjs.org
if: ${{ steps.release.outputs.release_created }}
- run: npm publish --provenance
- run: |
npm install
npm run build --if-present
npm publish --provenance
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
if: ${{ steps.release.outputs.release_created }}
Expand Down
61 changes: 0 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ export default [
];
```

If you are still using the deprecated `.eslintrc.js` file format for ESLint, you can extend the `plugin:markdown/recommended-legacy` config to enable the Markdown processor on all `.md` files:

```js
// .eslintrc.js
module.exports = {
extends: "plugin:markdown/recommended-legacy"
};
```

#### Advanced Configuration

You can manually include the Markdown processor by setting the `processor` option in your configuration file for all `.md` files.
Expand Down Expand Up @@ -96,32 +87,6 @@ export default [
];
```

In the deprecated `.eslintrc.js` format:

```js
// .eslintrc.js
module.exports = {
// 1. Add the plugin.
plugins: ["markdown"],
overrides: [
{
// 2. Enable the Markdown processor for all .md files.
files: ["**/*.md"],
processor: "markdown/markdown"
},
{
// 3. Optionally, customize the configuration ESLint uses for ```js
// fenced code blocks inside .md files.
files: ["**/*.md/*.js"],
// ...
rules: {
// ...
}
}
]
};
```

#### Frequently-Disabled Rules

Some rules that catch mistakes in regular code are less helpful in documentation.
Expand Down Expand Up @@ -163,30 +128,6 @@ export default [
];
```

And in the deprecated `.eslintrc.js` format:

```js
// .eslintrc.js
module.exports = {
plugins: ["markdown"],
overrides: [
{
files: ["**/*.md"],
processor: "markdown/markdown"
},
{
// 1. Target ```js code blocks in .md files.
files: ["**/*.md/*.js"],
rules: {
// 2. Disable other rules.
"no-console": "off",
"import/no-unresolved": "off"
}
}
]
};
```

#### Strict Mode

`"use strict"` directives in every code block would be annoying.
Expand All @@ -205,8 +146,6 @@ The `markdown.configs.recommended` config disables these rules in Markdown files

If you are using an `eslint.config.js` file, then you can run ESLint as usual and it will pick up file patterns in your config file. The `--ext` option is not available when using flat config.

If you are using an `.eslintrc.*` file, then you can run ESLint as usual and it will pick up file extensions specified in `overrides[].files` patterns in config files.


### Autofixing

Expand Down
24 changes: 12 additions & 12 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
"use strict";
import globals from "globals";
import eslintConfigESLint from "eslint-config-eslint";
import eslintConfigESLintFormatting from "eslint-config-eslint/formatting";
import markdown from "./src/index.js";

module.exports = [
...require("eslint-config-eslint/cjs").map(config => ({
...config,
files: ["**/*.js"]
})),
{
...require("eslint-config-eslint/formatting"),
files: ["**/*.js"]
},
export default [
...eslintConfigESLint,
eslintConfigESLintFormatting,
{
plugins: {
markdown: require(".")
markdown
}
},
{
Expand All @@ -25,8 +22,11 @@ module.exports = [
files: ["tests/**/*.js"],
languageOptions: {
globals: {
...require("globals").mocha
...globals.mocha
}
},
rules: {
"no-underscore-dangle": "off"
}
},
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"use strict";

const markdown = require("eslint-plugin-markdown");
const js = require("@eslint/js");
const globals = require("globals");
import js from "@eslint/js";
import markdown from "../../src/index.js";
import globals from "globals";
import reactRecommended from "eslint-plugin-react/configs/recommended.js";

module.exports = [
export default [
js.configs.recommended,
...markdown.configs.recommended,
require("eslint-plugin-react/configs/recommended"),
reactRecommended,
{
settings: {
react: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
"use strict";
import js from "@eslint/js";
import markdown from "../../src/index.js";
import tseslint from "typescript-eslint";

const markdown = require("eslint-plugin-markdown");
const js = require("@eslint/js")
const tseslint = require("typescript-eslint");

module.exports = tseslint.config(
export default tseslint.config(
js.configs.recommended,
...markdown.configs.recommended,
{
Expand Down
8 changes: 0 additions & 8 deletions index.js

This file was deleted.

File renamed without changes.
31 changes: 18 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
"name": "Brandon Mills",
"url": "https://github.com/btmills"
},
"type": "module",
"main": "src/index.js",
"exports": {
"import": {
"default": "./src/index.js"
}
},
"files": [
"src"
],
"repository": "eslint/eslint-plugin-markdown",
"bugs": {
"url": "https://github.com/eslint/eslint-plugin-markdown/issues"
Expand All @@ -21,35 +31,30 @@
],
"scripts": {
"lint": "eslint .",
"prepare": "node ./npm-prepare.js",
"prepare": "node ./npm-prepare.cjs",
"release:generate:latest": "eslint-generate-release",
"release:generate:alpha": "eslint-generate-prerelease alpha",
"release:generate:beta": "eslint-generate-prerelease beta",
"release:generate:rc": "eslint-generate-prerelease rc",
"release:publish": "eslint-publish-release",
"test": "nyc _mocha -- -c tests/{examples,lib}/**/*.js --timeout 30000"
"test": "c8 mocha \"tests/**/*.test.js\" --timeout 30000"
},
"main": "index.js",
"files": [
"index.js",
"lib/index.js",
"lib/processor.js"
],
"devDependencies": {
"@eslint/core": "^0.2.0",
"@eslint/js": "^9.4.0",
"chai": "^4.2.0",
"c8": "^10.1.2",
"chai": "^5.1.1",
"eslint": "^9.4.0",
"eslint-config-eslint": "^11.0.0",
"eslint-release": "^3.1.2",
"globals": "^15.1.0",
"mocha": "^6.2.2",
"nyc": "^14.1.1"
"mocha": "^10.6.0"
},
"dependencies": {
"mdast-util-from-markdown": "^0.8.5"
"mdast-util-from-markdown": "^2.0.1"
},
"peerDependencies": {
"eslint": ">=8"
"eslint": ">=9"
},
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
Expand Down
4 changes: 2 additions & 2 deletions release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"pull-request-title-pattern": "chore: release ${version} 🚀",
"include-component-in-tag": false,
"extra-files": [
"lib/index.js",
"lib/processor.js"
"src/index.js",
"src/processor.js"
]
}
}
Expand Down
6 changes: 2 additions & 4 deletions lib/index.js → src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
* @author Brandon Mills
*/

"use strict";

const processor = require("./processor");
import { processor } from "./processor.js";

const rulesConfig = {

Expand Down Expand Up @@ -100,4 +98,4 @@ plugin.configs.recommended = [
}
];

module.exports = plugin;
export default plugin;
8 changes: 3 additions & 5 deletions lib/processor.js → src/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
* @typedef {ASTNode & BlockBase} Block
*/

"use strict";

const parse = require("mdast-util-from-markdown");
import { fromMarkdown } from "mdast-util-from-markdown";

const UNSATISFIABLE_RULES = new Set([
"eol-last", // The Markdown parser strips trailing newlines in code fences
Expand Down Expand Up @@ -248,7 +246,7 @@ const languageToFileExtension = {
* @returns {Array<{ filename: string, text: string }>} Source code blocks to lint.
*/
function preprocess(text, filename) {
const ast = parse(text);
const ast = fromMarkdown(text);
const blocks = [];

blocksCache.set(filename, blocks);
Expand Down Expand Up @@ -409,7 +407,7 @@ function postprocess(messages, filename) {
});
}

module.exports = {
export const processor = {
meta: {
name: "eslint-plugin-markdown/markdown",
version: "5.1.0" // x-release-please-version
Expand Down
17 changes: 10 additions & 7 deletions tests/examples/all.js → tests/examples/all.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"use strict";

const assert = require("chai").assert;
const fs = require("fs");
const path = require("path");
const semver = require("semver");
import { assert } from "chai";
import fs from "node:fs";
import path from "node:path";
import semver from "semver";
import { createRequire } from "node:module";
import { fileURLToPath } from "node:url";

const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const examplesDir = path.resolve(__dirname, "../../examples/");
const examples = fs.readdirSync(examplesDir)
.filter(exampleDir => fs.statSync(path.join(examplesDir, exampleDir)).isDirectory())
Expand All @@ -14,7 +17,7 @@ for (const example of examples) {
const cwd = path.join(examplesDir, example);

// The plugin officially supports ESLint as early as v6, but the examples
// use ESLint v7, which has a higher minimum Node.js version than does v6.
// use ESLint v8, which has a higher minimum Node.js version than does v6.
// Only exercise the example if the running Node.js version satisfies the
// minimum version constraint. In CI, this will skip these tests in Node.js
// v8 and run them on all other Node.js versions.
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const markdown = require("../../");
const globals = require("globals");
import markdown from "../../src/index.js";
import globals from "globals";

module.exports = [
export default [
{
plugins: {
markdown
Expand Down
6 changes: 3 additions & 3 deletions tests/fixtures/recommended.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const markdown = require("../../");
const js = require("@eslint/js")
import markdown from "../../src/index.js";
import js from "@eslint/js";

module.exports = [
export default [
js.configs.recommended,
...markdown.configs.recommended,
{
Expand Down
Loading

0 comments on commit e0da221

Please sign in to comment.