From 77402f12ffc1c410231b1685d61bc689e2d00e14 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Wed, 5 May 2021 20:59:41 +0200 Subject: [PATCH] Use ESM --- .gitignore | 1 - .prettierignore | 1 - index.js | 13 ++++++------- package.json | 27 ++++++++++----------------- readme.md | 20 ++++++++++++++------ test.js | 14 +++++++------- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index fdefc8c..735f4af 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,5 @@ .DS_Store *.log -.nyc_output/ coverage/ node_modules/ yarn.lock diff --git a/.prettierignore b/.prettierignore index e7939c4..cebe81f 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,3 +1,2 @@ coverage/ -*.json *.md diff --git a/index.js b/index.js index c927e82..9e20b69 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,7 @@ -'use strict' +import {statistics} from 'vfile-statistics' -const statistics = require('vfile-statistics') - -module.exports = (vfiles) => - vfiles.map((vfile) => { +export function toESLint(vfiles) { + return vfiles.map((vfile) => { const stats = statistics(vfile) return { @@ -15,8 +13,8 @@ module.exports = (vfiles) => ruleId: [x.source, x.ruleId].filter(Boolean).join(':') || null, line: x.line, column: x.column, - endLine: x.location.end.line, - endColumn: x.location.end.column, + endLine: x.position.end.line, + endColumn: x.position.end.column, message: x.reason } }), @@ -24,3 +22,4 @@ module.exports = (vfiles) => warningCount: stats.nonfatal } }) +} diff --git a/package.json b/package.json index 82fe38c..576c1cd 100644 --- a/package.json +++ b/package.json @@ -30,30 +30,27 @@ "files": [ "index.js" ], + "sideEffects": false, + "type": "module", + "main": "index.js", "dependencies": { - "vfile-statistics": "^1.1.1" + "vfile-statistics": "^2.0.0" }, "devDependencies": { - "nyc": "^15.0.0", + "c8": "^7.0.0", "prettier": "^2.0.0", "remark-cli": "^9.0.0", "remark-preset-wooorm": "^8.0.0", "tape": "^5.0.0", - "vfile": "^4.0.0", - "xo": "^0.38.0" + "vfile": "^5.0.0", + "xo": "^0.39.0" }, "scripts": { "format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", - "test-api": "node test", - "test-coverage": "nyc --reporter lcov node test.js", + "test-api": "node test.js", + "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", "test": "npm run format && npm run test-coverage" }, - "nyc": { - "check-coverage": true, - "lines": 100, - "functions": 100, - "branches": 100 - }, "prettier": { "tabWidth": 2, "useTabs": false, @@ -63,11 +60,7 @@ "trailingComma": "none" }, "xo": { - "prettier": true, - "esnext": false, - "rules": { - "unicorn/prefer-optional-catch-binding": "off" - } + "prettier": true }, "remarkConfig": { "plugins": [ diff --git a/readme.md b/readme.md index 3bbb655..5dea4b2 100644 --- a/readme.md +++ b/readme.md @@ -16,6 +16,9 @@ this module to display it using an ESLint formatter. ## Install +This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c): +Node 12+ is needed to use it and it must be `import`ed instead of `require`d. + [npm][]: ```sh @@ -25,19 +28,24 @@ npm install vfile-to-eslint ## Use ```js -const remark = require('remark') -const recommended = require('remark-preset-lint-recommended') -const eslintFormatterPretty = require('eslint-formatter-pretty') -const vfileToEslint = require('vfile-to-eslint') +import remark from 'remark' +import recommended from 'remark-preset-lint-recommended' +import eslintFormatterPretty from 'eslint-formatter-pretty' +import {toESLint} from 'vfile-to-eslint' const file = remark() .use(recommended) .processSync('## Hello world!') -console.log(eslintFormatterPretty(vfileToEslint([file]))) +console.log(eslintFormatterPretty(toESLint([file]))) ``` -### `vfileToEslint(files)` +## API + +This package exports the following identifiers: `toESLint`. +There is no default export. + +### `toESLint(files)` Returns an `Object` that can be passed directly to an [ESLint formatter][eslint-formatter]. diff --git a/test.js b/test.js index 6be4b41..ea678bb 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,9 @@ -var test = require('tape') -var vfile = require('vfile') -var toEslint = require('.') +import test from 'tape' +import {VFile} from 'vfile' +import {toESLint} from './index.js' -test('vfile-to-eslint', function (t) { - var file = vfile({path: '~/example.md'}) +test('toESLint', (t) => { + const file = new VFile({path: '~/example.md'}) file.info('This is perfect', {line: 5, column: 3}, 'alpha:bravo') @@ -21,9 +21,9 @@ test('vfile-to-eslint', function (t) { end: {line: 2, column: 8} } }) - } catch (_) {} + } catch {} - t.deepEqual(toEslint([file]), [ + t.deepEqual(toESLint([file]), [ { filePath: '~/example.md', messages: [