Skip to content

Commit

Permalink
Convert own source to ES modules (#103)
Browse files Browse the repository at this point in the history
* drop mjs extension

* convert straightforward imports and exports

* use newline-after-import instead of custom

* sort-imports off

* file extensions now required

* add type, but node plugin won't pick it up

* remaining requires converted

* eslint config

* More to convert after rebase

* MDN compat DB imports (https://github.com/mdn/browser-compat-data/releases/tag/v5.0.0)
  • Loading branch information
robatwilliams committed Mar 8, 2024
1 parent b17b5bc commit 662ec32
Show file tree
Hide file tree
Showing 35 changed files with 140 additions and 117 deletions.
15 changes: 8 additions & 7 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
"eslint:all",
"prettier",

"plugin:n/recommended"
// Need to be specific, because this config is one level above the package.jsons that have type:module
"plugin:n/recommended-module"
],
"plugins": ["import"],
"parserOptions": {
"ecmaVersion": 2022
"ecmaVersion": 2022,
"sourceType": "module"
},
"env": {
"es2021": true,
Expand All @@ -36,6 +38,7 @@
"no-ternary": "off",
"no-undefined": "off",
"one-var": "off",
"sort-imports": "off", // using import/order instead
"sort-keys": "off",
"strict": "off",

Expand Down Expand Up @@ -64,13 +67,11 @@
{ "blankLine": "always", "prev": "*", "next": ["cjs-export"] },

{ "blankLine": "always", "prev": "*", "next": "block-like" },
{ "blankLine": "always", "prev": "block-like", "next": "*" },

{ "blankLine": "always", "prev": "cjs-import", "next": "*" },
{ "blankLine": "any", "prev": "cjs-import", "next": "cjs-import" }
{ "blankLine": "always", "prev": "block-like", "next": "*" }
],

// eslint-plugin-import - Selection from the ones that are compatible with CommonJS
// eslint-plugin-import - selection (not comprehensive)
"import/newline-after-import": "error",
"import/no-extraneous-dependencies": "error",
"import/order": ["error", { "newlines-between": "never" }]
}
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Different versions may work, but majorly newer/older tooling versions are likely
- Verify there are no uncommitted changes
- `npm run ci` in root
- `npm run lint` in the example project, check number of errors and presence of new expected ones
- `../check-es-compat/bin/cli.mjs .` in the example project, it should produce the same errors
- `../check-es-compat/bin/cli.js .` in the example project, it should produce the same errors
- `lerna clean` then `lerna bootstrap`, verify no changes were introduced (e.g. to `package-lock.json`)

## Releasing
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"private": true,
"scripts": {
"check": "npm-run-all --parallel check:*",
"check:lint": "eslint --no-eslintrc --config=.eslintrc.json **/*.{js,mjs}",
"check:prettier": "prettier --check **/*.{js,json,md,mjs}",
"check:lint": "eslint --no-eslintrc --config=.eslintrc.json **/*.js",
"check:prettier": "prettier --check **/*.{js,json,md}",
"ci": "npm-run-all check test",
"postinstall": "lerna bootstrap",
"test": "lerna run test"
Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion packages/check-es-compat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
},
"license": "MIT",
"author": "Robat Williams",
"type": "module",
"bin": {
"check-es-compat": "bin/cli.mjs"
"check-es-compat": "bin/cli.js"
},
"dependencies": {
"eslint": "^8.56.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = function compareVersions(a, b) {
export default function compareVersions(a, b) {
const aParts = a.split('.').map(Number);
const bParts = b.split('.').map(Number);

Expand All @@ -12,4 +12,4 @@ module.exports = function compareVersions(a, b) {
}

return 0;
};
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const assert = require('node:assert');
const { test } = require('node:test');
const compareVersions = require('./compareVersions');
import assert from 'node:assert';
import { test } from 'node:test';
import compareVersions from './compareVersions.js';

test('equal', () => {
assert.strictEqual(compareVersions('1.2.3', '1.2.3'), 0);
Expand Down
6 changes: 2 additions & 4 deletions packages/eslint-plugin-ecmascript-compat/lib/compatibility.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable camelcase, no-underscore-dangle */
const compareVersions = require('./compareVersions');
import compareVersions from './compareVersions.js';

function unsupportedFeatures(features, targets) {
export function unsupportedFeatures(features, targets) {
return features.filter((feature) => !isFeatureSupportedByTargets(feature, targets));
}

Expand Down Expand Up @@ -54,5 +54,3 @@ function interpretSupport(versionAdded) {
isVersionUnknown: versionAdded === true,
};
}

module.exports = { unsupportedFeatures };
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable camelcase */
const assert = require('node:assert');
const { it } = require('node:test');
const { unsupportedFeatures } = require('./compatibility');
const features = require('./features');
import assert from 'node:assert';
import { it } from 'node:test';
import { unsupportedFeatures } from './compatibility.js';
import features from './features/index.js';

it('supports feature in version introduced', () => {
const feature = {
Expand Down
6 changes: 2 additions & 4 deletions packages/eslint-plugin-ecmascript-compat/lib/delegation.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function createDelegatee(config, rootContext) {
export function createDelegatee(config, rootContext) {
const { definition, options } = config;

if (!definition) {
Expand Down Expand Up @@ -44,7 +44,7 @@ function createDelegatee(config, rootContext) {
return definition.create(context);
}

function delegatingVisitor(delegatees) {
export function delegatingVisitor(delegatees) {
const delegator = {};

delegatees.forEach((visitor) => {
Expand All @@ -63,5 +63,3 @@ function delegatingVisitor(delegatees) {

return delegator;
}

module.exports = { createDelegatee, delegatingVisitor };
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const eslint = require('eslint');
const { createDelegatee, delegatingVisitor } = require('./delegation');
import eslint from 'eslint';
import { createDelegatee, delegatingVisitor } from './delegation.js';

const ruleTester = new eslint.RuleTester();

Expand Down
11 changes: 6 additions & 5 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2016.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const eslint = require('eslint');
const esPlugin = require('eslint-plugin-es-x');
const compatData = require('@mdn/browser-compat-data');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');
import eslint from 'eslint';
import esPlugin from 'eslint-plugin-es-x';
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import { noRestrictedSyntaxPrototypeMethod } from './ruleOptionsUtil.js';

const coreRules = new eslint.Linter().getRules();

module.exports = [
export default [
{
ruleConfig: {
definition: coreRules.get('no-restricted-syntax'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 46';
Expand All @@ -9,7 +10,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: 'foo.includes();',
Expand Down
11 changes: 6 additions & 5 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2017.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const eslint = require('eslint');
const esPlugin = require('eslint-plugin-es-x');
const compatData = require('@mdn/browser-compat-data');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');
import eslint from 'eslint';
import esPlugin from 'eslint-plugin-es-x';
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import { noRestrictedSyntaxPrototypeMethod } from './ruleOptionsUtil.js';

const coreRules = new eslint.Linter().getRules();

module.exports = [
export default [
{
ruleConfig: { definition: esPlugin.rules['no-async-functions'] },
compatFeatures: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 53';
Expand All @@ -16,7 +17,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: 'Object.getOwnPropertyDescriptors();',
Expand Down
11 changes: 6 additions & 5 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2018.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const eslint = require('eslint');
const esPlugin = require('eslint-plugin-es-x');
const compatData = require('@mdn/browser-compat-data');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');
import eslint from 'eslint';
import esPlugin from 'eslint-plugin-es-x';
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import { noRestrictedSyntaxPrototypeMethod } from './ruleOptionsUtil.js';

const coreRules = new eslint.Linter().getRules();

module.exports = [
export default [
{
ruleConfig: { definition: esPlugin.rules['no-async-iteration'] },
compatFeatures: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 59';
Expand All @@ -9,7 +10,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: 'foo.finally();',
Expand Down
11 changes: 6 additions & 5 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2019.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const eslint = require('eslint');
const esPlugin = require('eslint-plugin-es-x');
const compatData = require('@mdn/browser-compat-data');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');
import eslint from 'eslint';
import esPlugin from 'eslint-plugin-es-x';
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import { noRestrictedSyntaxPrototypeMethod } from './ruleOptionsUtil.js';

const coreRules = new eslint.Linter().getRules();

module.exports = [
export default [
{
ruleConfig: {
definition: coreRules.get('no-restricted-syntax'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 65';
Expand All @@ -9,7 +10,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: 'const flat = residentialAddress.flat;',
Expand Down
11 changes: 6 additions & 5 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2020.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const eslint = require('eslint');
const esPlugin = require('eslint-plugin-es-x');
const compatData = require('@mdn/browser-compat-data');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');
import eslint from 'eslint';
import esPlugin from 'eslint-plugin-es-x';
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import { noRestrictedSyntaxPrototypeMethod } from './ruleOptionsUtil.js';

const coreRules = new eslint.Linter().getRules();

module.exports = [
export default [
{
ruleConfig: {
definition: coreRules.get('no-restricted-properties'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 62';
Expand All @@ -20,7 +21,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: 'globalThis.foo;',
Expand Down
11 changes: 6 additions & 5 deletions packages/eslint-plugin-ecmascript-compat/lib/features/es2021.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const eslint = require('eslint');
const compatData = require('@mdn/browser-compat-data');
const esPlugin = require('eslint-plugin-es-x');
const { noRestrictedSyntaxPrototypeMethod } = require('./ruleOptionsUtil');
import eslint from 'eslint';
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import esPlugin from 'eslint-plugin-es-x';
import { noRestrictedSyntaxPrototypeMethod } from './ruleOptionsUtil.js';

const coreRules = new eslint.Linter().getRules();

module.exports = [
export default [
{
ruleConfig: {
definition: coreRules.get('no-restricted-syntax'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 74';
Expand All @@ -16,7 +17,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: '"A dog".replaceAll("dog", "monkey");',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const compatData = require('@mdn/browser-compat-data');
const esPlugin = require('eslint-plugin-es-x');
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import esPlugin from 'eslint-plugin-es-x';

module.exports = [
export default [
{
ruleConfig: {
definition: esPlugin.rules['no-array-string-prototype-at'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { RuleTester } = require('eslint');
import { RuleTester } from 'eslint';
import rule from '../rule.js';

// Browser that doesn't support any features of this version - see es-versions.md
process.env.BROWSERSLIST = 'Chrome >= 71';
Expand All @@ -10,7 +11,7 @@ const ruleTester = new RuleTester({
},
});

ruleTester.run('compat', require('../rule'), {
ruleTester.run('compat', rule, {
valid: [
{
code: '[].at(1);',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const compatData = require('@mdn/browser-compat-data');
const esPlugin = require('eslint-plugin-es-x');
// Import assertions aren't yet stage 4 so aren't supported by ESLint
import compatData from '@mdn/browser-compat-data/forLegacyNode';
import esPlugin from 'eslint-plugin-es-x';

module.exports = [
export default [
{
ruleConfig: {
definition: esPlugin.rules['no-array-prototype-findlast-findlastindex'],
Expand Down
Loading

0 comments on commit 662ec32

Please sign in to comment.