Skip to content

Commit

Permalink
Convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
novemberborn committed May 2, 2021
1 parent f1ea8c9 commit 729420a
Show file tree
Hide file tree
Showing 214 changed files with 1,006 additions and 950 deletions.
74 changes: 74 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
{
"ignores": [
"media/**",
"test/config/fixtures/config-errors/test.js",
"test/config/fixtures/mjs-with-tests/**",
"test-tap/fixture/ava-paths/target/test.js",
"test-tap/fixture/{source-map-initial,syntax-error}.js",
"test-tap/fixture/snapshots/test-sourcemaps/build/**",
"test-tap/fixture/power-assert.js",
"test-tap/fixture/report/edgecases/ast-syntax-error.js"
],
"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": "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();
1 change: 1 addition & 0 deletions entrypoints/main.mjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import test from '../lib/worker/main.cjs';

export default test;
47 changes: 23 additions & 24 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 * as sharedWorkers from './plugin-support/shared-workers.js';
import RunStatus from './run-status.js';
import * as 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 @@ -282,5 +283,3 @@ class Api extends Emittery {
return cacheDir;
}
}

module.exports = Api;
24 changes: 10 additions & 14 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 * as snapshotManager 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 @@ -990,4 +987,3 @@ class Assertions {
}
}
}
exports.Assertions = Assertions;
24 changes: 9 additions & 15 deletions lib/chalk.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
'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');
}

return ctx;
};
let instance = new chalk.Instance();
export default instance;

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);
}
75 changes: 43 additions & 32 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
'use strict';
const path = require('path');
const del = require('del');
const updateNotifier = require('update-notifier');
const figures = require('figures');
const arrify = require('arrify');
const yargs = require('yargs');
const readPkg = require('read-pkg');
const isCi = require('./is-ci');
const {loadConfig} = require('./load-config');
import {createRequire} from 'module';
import path from 'path';

import arrify from 'arrify';
import ciParallelVars from 'ci-parallel-vars';
import del from 'del';
import figures from 'figures';
import readPkg from 'read-pkg';
import updateNotifier from 'update-notifier';

import Api from './api.js';
import chalk from './chalk.js';
import validateEnvironmentVariables from './environment-variables.js';
import normalizeExtensions from './extensions.js';
import {normalizeGlobs, normalizePattern} from './globs.js';
import {controlFlow} from './ipc-flow-control.cjs';
import isCi from './is-ci.js';
import {splitPatternAndLineNumbers} from './line-numbers.js';
import {loadConfig} from './load-config.js';
import normalizeModuleTypes from './module-types.js';
import normalizeNodeArguments from './node-arguments.js';
import avaPackage from './pkg.cjs';
import * as providerManager from './provider-manager.js';
import DefaultReporter from './reporters/default.js';
import TapReporter from './reporters/tap.js';
import Watcher from './watcher.js';

const yargs = createRequire(import.meta.url)('yargs'); // FIXME: Use ESM

function exit(message) {
console.error(`\n ${require('./chalk').get().red(figures.cross)} ${message}`);
console.error(`\n ${chalk.red(figures.cross)} ${message}`);
process.exit(1); // eslint-disable-line unicorn/no-process-exit
}

Expand Down Expand Up @@ -83,7 +101,7 @@ const FLAGS = {
}
};

exports.run = async () => { // eslint-disable-line complexity
export default async () => { // eslint-disable-line complexity
let conf = {};
let confError = null;
try {
Expand All @@ -96,7 +114,13 @@ exports.run = async () => { // eslint-disable-line complexity
// Enter debug mode if the main process is being inspected. This assumes the
// worker processes are automatically inspected, too. It is not necessary to
// run AVA with the debug command, though it's allowed.
const activeInspector = require('inspector').url() !== undefined; // eslint-disable-line node/no-unsupported-features/node-builtins
let activeInspector = false;
try {
const {default: inspector} = await import('inspector');

activeInspector = inspector.url() !== undefined;
} catch {}

let debug = activeInspector ?
{
active: true,
Expand Down Expand Up @@ -207,8 +231,9 @@ exports.run = async () => { // eslint-disable-line complexity
}
}

const chalkOptions = {level: combined.color === false ? 0 : require('chalk').level};
const chalk = require('./chalk').set(chalkOptions);
const chalkOptions = {level: combined.color === false ? 0 : (await import('chalk')).level};
const {set: setChalk} = await import('./chalk.js');
setChalk(chalkOptions);

if (confError) {
if (confError.parent) {
Expand All @@ -218,7 +243,7 @@ exports.run = async () => { // eslint-disable-line complexity
}
}

updateNotifier({pkg: require('../package.json')}).notify();
updateNotifier({pkg: avaPackage}).notify();

const {nonSemVerExperiments: experiments, projectDir} = conf;
if (resetCache) {
Expand Down Expand Up @@ -285,19 +310,6 @@ exports.run = async () => { // eslint-disable-line complexity
exit('’sources’ has been removed. Use ’ignoredByWatcher’ to provide glob patterns of files that the watcher should ignore.');
}

const ciParallelVars = require('ci-parallel-vars');
const Api = require('./api');
const DefaultReporter = require('./reporters/default');
const TapReporter = require('./reporters/tap');
const Watcher = require('./watcher');
const normalizeExtensions = require('./extensions');
const normalizeModuleTypes = require('./module-types');
const {normalizeGlobs, normalizePattern} = require('./globs');
const normalizeNodeArguments = require('./node-arguments');
const validateEnvironmentVariables = require('./environment-variables');
const {splitPatternAndLineNumbers} = require('./line-numbers');
const providerManager = require('./provider-manager');

let pkg;
try {
pkg = readPkg.sync({cwd: projectDir});
Expand All @@ -312,7 +324,7 @@ exports.run = async () => { // eslint-disable-line complexity
const providers = [];
if (Reflect.has(conf, 'babel')) {
try {
const {level, main} = providerManager.babel(projectDir);
const {level, main} = await providerManager.babel(projectDir);
providers.push({
level,
main: main({config: conf.babel}),
Expand All @@ -325,7 +337,7 @@ exports.run = async () => { // eslint-disable-line complexity

if (Reflect.has(conf, 'typescript')) {
try {
const {level, main} = providerManager.typescript(projectDir);
const {level, main} = await providerManager.typescript(projectDir);
providers.push({
level,
main: main({config: conf.typescript}),
Expand Down Expand Up @@ -430,7 +442,6 @@ exports.run = async () => { // eslint-disable-line complexity
reporter.startRun(plan);

if (process.env.AVA_EMIT_RUN_STATUS_OVER_IPC === 'I\'ll find a payphone baby / Take some time to talk to you') {
const {controlFlow} = require('./ipc-flow-control');
const bufferedSend = controlFlow(process);

plan.status.on('stateChange', evt => {
Expand Down
15 changes: 8 additions & 7 deletions lib/code-excerpt.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';
const fs = require('fs');
const equalLength = require('equal-length');
const codeExcerpt = require('code-excerpt');
const truncate = require('cli-truncate');
const chalk = require('./chalk').get();
import fs from 'fs';

import truncate from 'cli-truncate';
import codeExcerpt from 'code-excerpt';
import equalLength from 'equal-length';

import chalk from './chalk.js';

const formatLineNumber = (lineNumber, maxLineNumber) =>
' '.repeat(Math.max(0, String(maxLineNumber).length - String(lineNumber).length)) + lineNumber;

module.exports = (source, options = {}) => {
export default (source, options = {}) => {
if (!source.isWithinProject || source.isDependency) {
return null;
}
Expand Down

0 comments on commit 729420a

Please sign in to comment.