Skip to content

Commit c57067b

Browse files
authored
Switch to ESM for internals
* Extract glob helpers that are needed in a CJS environment * Ensure ava/plugin works in a CJS environment Refactor and deploy some tricks so that ava/plugin is usable in a CJS environment, even if the rest of AVA's internals become ESM. * Ensure 'ava' can be imported in a CJS environment * Convert to ESM * Load yargs as CJS due to strange compatibility issues * Keep line number parsing code as CJS because it attempts to lazy-load dependencies * Shared workers are always loaded through a dynamic import * Shared worker filenames must now be file:// URLs, but you can provide a URL instance * Test file paths within shared workers are now file:// URLs * Error serialization and code excerpts now support ESM * Title prefixes in the reporters now correctly skip all test file extensions, not just `js` * CI: Increase delay on timeout test, mostly for Windows
1 parent b8f5685 commit c57067b

File tree

476 files changed

+2880
-2779
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

476 files changed

+2880
-2779
lines changed

.xo-config.json

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"ignores": [
3+
"media/**",
4+
"test/config/fixtures/config-errors/test.js",
5+
"test-tap/fixture/snapshots/test-sourcemaps/build/**",
6+
"test-tap/fixture/report/edgecases/ast-syntax-error.cjs"
7+
],
8+
"rules": {
9+
"import/no-anonymous-default-export": "off",
10+
"import/no-mutable-exports": "off",
11+
"import/order": [
12+
"error",
13+
{
14+
"alphabetize": {
15+
"order": "asc"
16+
},
17+
"newlines-between": "always"
18+
}
19+
],
20+
"import/newline-after-import": "error",
21+
"node/no-unsupported-features/es-syntax": "off",
22+
"no-use-extend-native/no-use-extend-native": "off"
23+
},
24+
"overrides": [
25+
{
26+
"files": "index.d.ts",
27+
"rules": {
28+
"@typescript-eslint/member-ordering": "off",
29+
"@typescript-eslint/method-signature-style": "off",
30+
"@typescript-eslint/prefer-readonly-parameter-types": "off",
31+
"@typescript-eslint/prefer-function-type": "off",
32+
"@typescript-eslint/unified-signatures": "off"
33+
}
34+
},
35+
{
36+
"files": "plugin.d.ts",
37+
"rules": {
38+
"node/prefer-global/url": "off"
39+
}
40+
},
41+
{
42+
"files": "test-{d,tap}/**/*.ts",
43+
"rules": {
44+
"@typescript-eslint/explicit-function-return-type": "off",
45+
"@typescript-eslint/no-empty-function": "off",
46+
"@typescript-eslint/no-unsafe-call": "off",
47+
"@typescript-eslint/no-unsafe-member-access": "off",
48+
"@typescript-eslint/no-unsafe-return": "off",
49+
"@typescript-eslint/no-unused-vars": "off",
50+
"@typescript-eslint/prefer-readonly-parameter-types": "off",
51+
"import/extensions": "off",
52+
"no-unused-vars": "off"
53+
}
54+
},
55+
{
56+
"files": "test-tap/**/*.js",
57+
"rules": {
58+
"promise/prefer-await-to-then": "off",
59+
"unicorn/error-message": "off",
60+
"unicorn/no-array-reduce": "off",
61+
"unicorn/prevent-abbreviations": "off"
62+
}
63+
},
64+
{
65+
"files": [
66+
"test-tap/fixture/**",
67+
"test/**/fixtures/**"
68+
],
69+
"rules": {
70+
"ava/no-todo-test": "off",
71+
"import/no-extraneous-dependencies": "off",
72+
"import/no-unresolved": "off"
73+
}
74+
}
75+
]
76+
}

entrypoints/cli.mjs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
#!/usr/bin/env node
2-
import * as cli from '../lib/cli.js'; // eslint-disable-line import/extensions
3-
cli.run();
2+
import run from '../lib/cli.js';
3+
4+
run();

entrypoints/eslint-plugin-helper.cjs

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@ const url = require('url');
44
const v8 = require('v8');
55
const {Worker} = require('worker_threads');
66

7-
const {classify, hasExtension, isHelperish, matches, normalizeFileForMatching, normalizePatterns} = require('../lib/globs');
7+
const {
8+
classify,
9+
hasExtension,
10+
isHelperish,
11+
matches,
12+
normalizeFileForMatching,
13+
normalizePatterns
14+
} = require('../lib/glob-helpers.cjs');
815

916
const MAX_DATA_LENGTH_EXCLUSIVE = 100 * 1024; // Allocate 100 KiB to exchange globs.
1017

entrypoints/main.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
'use strict';
2-
module.exports = require('../lib/worker/main');
2+
module.exports = require('../lib/worker/main.cjs');

entrypoints/main.mjs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
import test from '../lib/worker/main.js'; // eslint-disable-line import/extensions
1+
import test from '../lib/worker/main.cjs';
2+
23
export default test;

entrypoints/plugin.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
'use strict';
2-
module.exports = require('../lib/worker/plugin');
2+
module.exports = require('../lib/worker/plugin.cjs');

entrypoints/plugin.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'use strict';
2-
import * as plugin from '../lib/worker/plugin.js'; // eslint-disable-line import/extensions
1+
import * as plugin from '../lib/worker/plugin.cjs';
2+
33
const {registerSharedWorker} = plugin;
44
export {registerSharedWorker};

lib/api.js

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
'use strict';
2-
const fs = require('fs');
3-
const path = require('path');
4-
const os = require('os');
5-
const commonPathPrefix = require('common-path-prefix');
6-
const resolveCwd = require('resolve-cwd');
7-
const debounce = require('lodash/debounce');
8-
const arrify = require('arrify');
9-
const ms = require('ms');
10-
const chunkd = require('chunkd');
11-
const Emittery = require('emittery');
12-
const pMap = require('p-map');
13-
const tempDir = require('temp-dir');
14-
const globs = require('./globs');
15-
const isCi = require('./is-ci');
16-
const RunStatus = require('./run-status');
17-
const fork = require('./fork');
18-
const serializeError = require('./serialize-error');
19-
const {getApplicableLineNumbers} = require('./line-numbers');
20-
const sharedWorkers = require('./plugin-support/shared-workers');
21-
const scheduler = require('./scheduler');
1+
import fs from 'fs';
2+
import os from 'os';
3+
import path from 'path';
4+
5+
import arrify from 'arrify';
6+
import chunkd from 'chunkd';
7+
import commonPathPrefix from 'common-path-prefix';
8+
import Emittery from 'emittery';
9+
import debounce from 'lodash/debounce.js';
10+
import ms from 'ms';
11+
import pMap from 'p-map';
12+
import resolveCwd from 'resolve-cwd';
13+
import tempDir from 'temp-dir';
14+
15+
import fork from './fork.js';
16+
import * as globs from './globs.js';
17+
import isCi from './is-ci.js';
18+
import {getApplicableLineNumbers} from './line-numbers.js';
19+
import {observeWorkerProcess} from './plugin-support/shared-workers.js';
20+
import RunStatus from './run-status.js';
21+
import scheduler from './scheduler.js';
22+
import serializeError from './serialize-error.js';
2223

2324
function resolveModules(modules) {
2425
return arrify(modules).map(name => {
@@ -41,7 +42,7 @@ function getFilePathPrefix(files) {
4142
return commonPathPrefix(files);
4243
}
4344

44-
class Api extends Emittery {
45+
export default class Api extends Emittery {
4546
constructor(options) {
4647
super();
4748

@@ -233,7 +234,7 @@ class Api extends Emittery {
233234

234235
const worker = fork(file, options, apiOptions.nodeArguments);
235236
runStatus.observeWorker(worker, file, {selectingLines: lineNumbers.length > 0});
236-
deregisteredSharedWorkers.push(sharedWorkers.observeWorkerProcess(worker, runStatus));
237+
deregisteredSharedWorkers.push(observeWorkerProcess(worker, runStatus));
237238

238239
pendingWorkers.add(worker);
239240
worker.promise.then(() => {
@@ -282,5 +283,3 @@ class Api extends Emittery {
282283
return cacheDir;
283284
}
284285
}
285-
286-
module.exports = Api;

lib/assert.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
'use strict';
2-
const concordance = require('concordance');
3-
const isError = require('is-error');
4-
const isPromise = require('is-promise');
5-
const concordanceOptions = require('./concordance-options').default;
6-
const {CIRCULAR_SELECTOR, isLikeSelector, selectComparable} = require('./like-selector');
7-
const snapshotManager = require('./snapshot-manager');
1+
import concordance from 'concordance';
2+
import isError from 'is-error';
3+
import isPromise from 'is-promise';
4+
5+
import concordanceOptions from './concordance-options.js';
6+
import {CIRCULAR_SELECTOR, isLikeSelector, selectComparable} from './like-selector.js';
7+
import {SnapshotError, VersionMismatchError} from './snapshot-manager.js';
88

99
function formatDescriptorDiff(actualDescriptor, expectedDescriptor, options) {
1010
options = {...options, ...concordanceOptions};
@@ -35,7 +35,7 @@ const notImplemented = () => {
3535
throw new Error('not implemented');
3636
};
3737

38-
class AssertionError extends Error {
38+
export class AssertionError extends Error {
3939
constructor(options) {
4040
super(options.message || '');
4141
this.name = 'AssertionError';
@@ -58,9 +58,8 @@ class AssertionError extends Error {
5858
this.savedError = options.savedError ? options.savedError : getErrorWithLongStackTrace();
5959
}
6060
}
61-
exports.AssertionError = AssertionError;
6261

63-
function checkAssertionMessage(assertion, message) {
62+
export function checkAssertionMessage(assertion, message) {
6463
if (typeof message === 'undefined' || typeof message === 'string') {
6564
return true;
6665
}
@@ -73,8 +72,6 @@ function checkAssertionMessage(assertion, message) {
7372
});
7473
}
7574

76-
exports.checkAssertionMessage = checkAssertionMessage;
77-
7875
function getErrorWithLongStackTrace() {
7976
const limitBefore = Error.stackTraceLimit;
8077
Error.stackTraceLimit = Number.POSITIVE_INFINITY;
@@ -254,7 +251,7 @@ function assertExpectations({assertion, actual, expectations, message, prefix, s
254251
}
255252
}
256253

257-
class Assertions {
254+
export class Assertions {
258255
constructor({
259256
pass = notImplemented,
260257
pending = notImplemented,
@@ -756,12 +753,12 @@ class Assertions {
756753
try {
757754
result = compareWithSnapshot({expected, message});
758755
} catch (error) {
759-
if (!(error instanceof snapshotManager.SnapshotError)) {
756+
if (!(error instanceof SnapshotError)) {
760757
throw error;
761758
}
762759

763760
const improperUsage = {name: error.name, snapPath: error.snapPath};
764-
if (error instanceof snapshotManager.VersionMismatchError) {
761+
if (error instanceof VersionMismatchError) {
765762
improperUsage.snapVersion = error.snapVersion;
766763
improperUsage.expectedVersion = error.expectedVersion;
767764
}
@@ -990,4 +987,3 @@ class Assertions {
990987
}
991988
}
992989
}
993-
exports.Assertions = Assertions;

lib/chalk.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
'use strict';
2-
const chalk = require('chalk');
1+
import chalk from 'chalk';
32

4-
let ctx = null;
5-
exports.get = () => {
6-
if (!ctx) {
7-
throw new Error('Chalk has not yet been configured');
8-
}
3+
let instance = new chalk.Instance();
4+
export default instance;
95

10-
return ctx;
11-
};
6+
export {instance as chalk};
127

13-
exports.set = options => {
14-
if (ctx) {
8+
let configured = false;
9+
export function set(options) {
10+
if (configured) {
1511
throw new Error('Chalk has already been configured');
1612
}
1713

18-
ctx = new chalk.Instance(options);
19-
return ctx;
20-
};
14+
configured = true;
15+
instance = new chalk.Instance(options);
16+
}

0 commit comments

Comments
 (0)