Skip to content

Commit

Permalink
Switch to ESM for internals
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
novemberborn authored Jun 6, 2021
1 parent b8f5685 commit c57067b
Show file tree
Hide file tree
Showing 476 changed files with 2,880 additions and 2,779 deletions.
76 changes: 76 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{
"ignores": [
"media/**",
"test/config/fixtures/config-errors/test.js",
"test-tap/fixture/snapshots/test-sourcemaps/build/**",
"test-tap/fixture/report/edgecases/ast-syntax-error.cjs"
],
"rules": {
"import/no-anonymous-default-export": "off",
"import/no-mutable-exports": "off",
"import/order": [
"error",
{
"alphabetize": {
"order": "asc"
},
"newlines-between": "always"
}
],
"import/newline-after-import": "error",
"node/no-unsupported-features/es-syntax": "off",
"no-use-extend-native/no-use-extend-native": "off"
},
"overrides": [
{
"files": "index.d.ts",
"rules": {
"@typescript-eslint/member-ordering": "off",
"@typescript-eslint/method-signature-style": "off",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"@typescript-eslint/prefer-function-type": "off",
"@typescript-eslint/unified-signatures": "off"
}
},
{
"files": "plugin.d.ts",
"rules": {
"node/prefer-global/url": "off"
}
},
{
"files": "test-{d,tap}/**/*.ts",
"rules": {
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/no-unsafe-call": "off",
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/prefer-readonly-parameter-types": "off",
"import/extensions": "off",
"no-unused-vars": "off"
}
},
{
"files": "test-tap/**/*.js",
"rules": {
"promise/prefer-await-to-then": "off",
"unicorn/error-message": "off",
"unicorn/no-array-reduce": "off",
"unicorn/prevent-abbreviations": "off"
}
},
{
"files": [
"test-tap/fixture/**",
"test/**/fixtures/**"
],
"rules": {
"ava/no-todo-test": "off",
"import/no-extraneous-dependencies": "off",
"import/no-unresolved": "off"
}
}
]
}
5 changes: 3 additions & 2 deletions entrypoints/cli.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env node
import * as cli from '../lib/cli.js'; // eslint-disable-line import/extensions
cli.run();
import run from '../lib/cli.js';

run();
9 changes: 8 additions & 1 deletion entrypoints/eslint-plugin-helper.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ const url = require('url');
const v8 = require('v8');
const {Worker} = require('worker_threads');

const {classify, hasExtension, isHelperish, matches, normalizeFileForMatching, normalizePatterns} = require('../lib/globs');
const {
classify,
hasExtension,
isHelperish,
matches,
normalizeFileForMatching,
normalizePatterns
} = require('../lib/glob-helpers.cjs');

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

Expand Down
2 changes: 1 addition & 1 deletion entrypoints/main.cjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
'use strict';
module.exports = require('../lib/worker/main');
module.exports = require('../lib/worker/main.cjs');
3 changes: 2 additions & 1 deletion entrypoints/main.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import test from '../lib/worker/main.js'; // eslint-disable-line import/extensions
import test from '../lib/worker/main.cjs';

export default test;
2 changes: 1 addition & 1 deletion entrypoints/plugin.cjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
'use strict';
module.exports = require('../lib/worker/plugin');
module.exports = require('../lib/worker/plugin.cjs');
4 changes: 2 additions & 2 deletions entrypoints/plugin.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use strict';
import * as plugin from '../lib/worker/plugin.js'; // eslint-disable-line import/extensions
import * as plugin from '../lib/worker/plugin.cjs';

const {registerSharedWorker} = plugin;
export {registerSharedWorker};
49 changes: 24 additions & 25 deletions lib/api.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const commonPathPrefix = require('common-path-prefix');
const resolveCwd = require('resolve-cwd');
const debounce = require('lodash/debounce');
const arrify = require('arrify');
const ms = require('ms');
const chunkd = require('chunkd');
const Emittery = require('emittery');
const pMap = require('p-map');
const tempDir = require('temp-dir');
const globs = require('./globs');
const isCi = require('./is-ci');
const RunStatus = require('./run-status');
const fork = require('./fork');
const serializeError = require('./serialize-error');
const {getApplicableLineNumbers} = require('./line-numbers');
const sharedWorkers = require('./plugin-support/shared-workers');
const scheduler = require('./scheduler');
import fs from 'fs';
import os from 'os';
import path from 'path';

import arrify from 'arrify';
import chunkd from 'chunkd';
import commonPathPrefix from 'common-path-prefix';
import Emittery from 'emittery';
import debounce from 'lodash/debounce.js';
import ms from 'ms';
import pMap from 'p-map';
import resolveCwd from 'resolve-cwd';
import tempDir from 'temp-dir';

import fork from './fork.js';
import * as globs from './globs.js';
import isCi from './is-ci.js';
import {getApplicableLineNumbers} from './line-numbers.js';
import {observeWorkerProcess} from './plugin-support/shared-workers.js';
import RunStatus from './run-status.js';
import scheduler from './scheduler.js';
import serializeError from './serialize-error.js';

function resolveModules(modules) {
return arrify(modules).map(name => {
Expand All @@ -41,7 +42,7 @@ function getFilePathPrefix(files) {
return commonPathPrefix(files);
}

class Api extends Emittery {
export default class Api extends Emittery {
constructor(options) {
super();

Expand Down Expand Up @@ -233,7 +234,7 @@ class Api extends Emittery {

const worker = fork(file, options, apiOptions.nodeArguments);
runStatus.observeWorker(worker, file, {selectingLines: lineNumbers.length > 0});
deregisteredSharedWorkers.push(sharedWorkers.observeWorkerProcess(worker, runStatus));
deregisteredSharedWorkers.push(observeWorkerProcess(worker, runStatus));

pendingWorkers.add(worker);
worker.promise.then(() => {
Expand Down Expand Up @@ -282,5 +283,3 @@ class Api extends Emittery {
return cacheDir;
}
}

module.exports = Api;
28 changes: 12 additions & 16 deletions lib/assert.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';
const concordance = require('concordance');
const isError = require('is-error');
const isPromise = require('is-promise');
const concordanceOptions = require('./concordance-options').default;
const {CIRCULAR_SELECTOR, isLikeSelector, selectComparable} = require('./like-selector');
const snapshotManager = require('./snapshot-manager');
import concordance from 'concordance';
import isError from 'is-error';
import isPromise from 'is-promise';

import concordanceOptions from './concordance-options.js';
import {CIRCULAR_SELECTOR, isLikeSelector, selectComparable} from './like-selector.js';
import {SnapshotError, VersionMismatchError} from './snapshot-manager.js';

function formatDescriptorDiff(actualDescriptor, expectedDescriptor, options) {
options = {...options, ...concordanceOptions};
Expand Down Expand Up @@ -35,7 +35,7 @@ const notImplemented = () => {
throw new Error('not implemented');
};

class AssertionError extends Error {
export class AssertionError extends Error {
constructor(options) {
super(options.message || '');
this.name = 'AssertionError';
Expand All @@ -58,9 +58,8 @@ class AssertionError extends Error {
this.savedError = options.savedError ? options.savedError : getErrorWithLongStackTrace();
}
}
exports.AssertionError = AssertionError;

function checkAssertionMessage(assertion, message) {
export function checkAssertionMessage(assertion, message) {
if (typeof message === 'undefined' || typeof message === 'string') {
return true;
}
Expand All @@ -73,8 +72,6 @@ function checkAssertionMessage(assertion, message) {
});
}

exports.checkAssertionMessage = checkAssertionMessage;

function getErrorWithLongStackTrace() {
const limitBefore = Error.stackTraceLimit;
Error.stackTraceLimit = Number.POSITIVE_INFINITY;
Expand Down Expand Up @@ -254,7 +251,7 @@ function assertExpectations({assertion, actual, expectations, message, prefix, s
}
}

class Assertions {
export class Assertions {
constructor({
pass = notImplemented,
pending = notImplemented,
Expand Down Expand Up @@ -756,12 +753,12 @@ class Assertions {
try {
result = compareWithSnapshot({expected, message});
} catch (error) {
if (!(error instanceof snapshotManager.SnapshotError)) {
if (!(error instanceof SnapshotError)) {
throw error;
}

const improperUsage = {name: error.name, snapPath: error.snapPath};
if (error instanceof snapshotManager.VersionMismatchError) {
if (error instanceof VersionMismatchError) {
improperUsage.snapVersion = error.snapVersion;
improperUsage.expectedVersion = error.expectedVersion;
}
Expand Down Expand Up @@ -990,4 +987,3 @@ class Assertions {
}
}
}
exports.Assertions = Assertions;
24 changes: 10 additions & 14 deletions lib/chalk.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
'use strict';
const chalk = require('chalk');
import chalk from 'chalk';

let ctx = null;
exports.get = () => {
if (!ctx) {
throw new Error('Chalk has not yet been configured');
}
let instance = new chalk.Instance();
export default instance;

return ctx;
};
export {instance as chalk};

exports.set = options => {
if (ctx) {
let configured = false;
export function set(options) {
if (configured) {
throw new Error('Chalk has already been configured');
}

ctx = new chalk.Instance(options);
return ctx;
};
configured = true;
instance = new chalk.Instance(options);
}
Loading

0 comments on commit c57067b

Please sign in to comment.