Repository: stoplightio/spectral
Pull request #2001 (2021-12-14 23:09:02)

chore(repo): enable esModuleInterop

**Checklist**

- [x] Tests added / updated
- [x] Docs added / updated

**Does this PR introduce a breaking change?**

- [ ] Yes
- [x] No




Patch:
diff --git a/packages/cli/src/formatters/pretty.ts b/packages/cli/src/formatters/pretty.ts
index 9d3f8753..3d1a4040 100644
--- a/packages/cli/src/formatters/pretty.ts
+++ b/packages/cli/src/formatters/pretty.ts
@@ -26,7 +26,7 @@
 
 import { printPath, PrintStyle } from '@stoplight/spectral-runtime';
 import { IDiagnostic, IRange } from '@stoplight/types';
-import * as chalk from 'chalk';
+import chalk from 'chalk';
 
 import { Formatter } from './types';
 import { getColorForSeverity, getHighestSeverity, getSummary, getSeverityName, groupBySource } from './utils';
diff --git a/packages/cli/src/formatters/stylish.ts b/packages/cli/src/formatters/stylish.ts
index 30d1437d..7f0aecf3 100644
--- a/packages/cli/src/formatters/stylish.ts
+++ b/packages/cli/src/formatters/stylish.ts
@@ -24,14 +24,14 @@
  * @author Sindre Sorhus
  */
 
-import { DiagnosticSeverity, IRange } from '@stoplight/types';
-import * as chalk from 'chalk';
+import type { DiagnosticSeverity, IRange } from '@stoplight/types';
+import chalk from 'chalk';
 import stripAnsi = require('strip-ansi');
-import * as table from 'text-table';
+import table from 'text-table';
 import { printPath, PrintStyle } from '@stoplight/spectral-runtime';
-import { IRuleResult } from '@stoplight/spectral-core';
+import type { IRuleResult } from '@stoplight/spectral-core';
 
-import { Formatter } from './types';
+import type { Formatter } from './types';
 import { getColorForSeverity, getHighestSeverity, getSeverityName, getSummary, groupBySource } from './utils';
 
 // -----------------------------------------------------------------------------
diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts
index 8e3495cf..7b324643 100644
--- a/packages/cli/src/index.ts
+++ b/packages/cli/src/index.ts
@@ -8,7 +8,7 @@ import type * as Agent from 'proxy-agent';
 
 if (typeof process.env.PROXY === 'string') {
   // eslint-disable-next-line @typescript-eslint/no-var-requires
-  const ProxyAgent = require('proxy-agent') as typeof Agent;
+  const ProxyAgent = require('proxy-agent') as typeof Agent['default'];
   DEFAULT_REQUEST_OPTIONS.agent = new ProxyAgent(process.env.PROXY);
 }
 
diff --git a/packages/cli/src/services/linter/utils/getRuleset.ts b/packages/cli/src/services/linter/utils/getRuleset.ts
index b5b68aae..c16ea733 100644
--- a/packages/cli/src/services/linter/utils/getRuleset.ts
+++ b/packages/cli/src/services/linter/utils/getRuleset.ts
@@ -11,7 +11,7 @@ import { node } from '@stoplight/spectral-ruleset-bundler/presets/node';
 import { stdin } from '@stoplight/spectral-ruleset-bundler/plugins/stdin';
 import { builtins } from '@stoplight/spectral-ruleset-bundler/plugins/builtins';
 import { isError, isObject } from 'lodash';
-import * as commonjs from '@rollup/plugin-commonjs';
+import commonjs from '@rollup/plugin-commonjs';
 import { ErrorWithCause } from 'pony-cause';
 
 async function getDefaultRulesetFile(): Promise<Optional<string>> {
@@ -60,23 +60,13 @@ export async function getRuleset(rulesetFile: Optional<string>): Promise<Ruleset
       ruleset = await bundleRuleset(rulesetFile, {
         target: 'node',
         format: 'commonjs',
-        plugins: [
-          stdin(migratedRuleset, rulesetFile),
-          builtins(),
-          // sigh, 2021 and we still do not use ESM
-          (commonjs as unknown as typeof import('@rollup/plugin-commonjs').default)(),
-          ...node({ fs, fetch }),
-        ],
+        plugins: [stdin(migratedRuleset, rulesetFile), builtins(), commonjs(), ...node({ fs, fetch })],
       });
     } else {
       ruleset = await bundleRuleset(rulesetFile, {
         target: 'node',
         format: 'commonjs',
-        plugins: [
-          builtins(),
-          (commonjs as unknown as typeof import('@rollup/plugin-commonjs').default)(),
-          ...node({ fs, fetch }),
-        ],
+        plugins: [builtins(), commonjs(), ...node({ fs, fetch })],
       });
     }
   } catch (e) {
diff --git a/packages/cli/src/services/linter/utils/listFiles.ts b/packages/cli/src/services/linter/utils/listFiles.ts
index 5249adf1..2edd0173 100644
--- a/packages/cli/src/services/linter/utils/listFiles.ts
+++ b/packages/cli/src/services/linter/utils/listFiles.ts
@@ -1,5 +1,5 @@
 import { normalize } from '@stoplight/path';
-import * as fg from 'fast-glob';
+import fg from 'fast-glob';
 
 const GLOB_OPTIONS = {
   absolute: true,
diff --git a/packages/core/src/ruleset/rulesetFunction.ts b/packages/core/src/ruleset/rulesetFunction.ts
index ca7793c2..6741a0dc 100644
--- a/packages/core/src/ruleset/rulesetFunction.ts
+++ b/packages/core/src/ruleset/rulesetFunction.ts
@@ -2,7 +2,7 @@ import Ajv, { ErrorObject } from 'ajv';
 import addFormats from 'ajv-formats';
 import ajvErrors from 'ajv-errors';
 import type { RequiredError } from 'ajv/dist/vocabularies/validation/required';
-import type { AdditionalPropertiesError } from 'ajv/lib/vocabularies/applicator/additionalProperties';
+import type { AdditionalPropertiesError } from 'ajv/dist/vocabularies/applicator/additionalProperties';
 import type { EnumError } from 'ajv/dist/vocabularies/validation/enum';
 import type { JSONSchema7 } from 'json-schema';
 
diff --git a/packages/core/src/ruleset/utils/minimatch.ts b/packages/core/src/ruleset/utils/minimatch.ts
index 5ed57e87..ce18729e 100644
--- a/packages/core/src/ruleset/utils/minimatch.ts
+++ b/packages/core/src/ruleset/utils/minimatch.ts
@@ -1,4 +1,4 @@
-import * as mm from 'minimatch';
+import mm from 'minimatch';
 
 const DEFAULT_OPTS = { matchBase: true };
 
diff --git a/packages/core/src/types/spectral.ts b/packages/core/src/types/spectral.ts
index 7c9ef46b..6b30d739 100644
--- a/packages/core/src/types/spectral.ts
+++ b/packages/core/src/types/spectral.ts
@@ -1,5 +1,5 @@
 import { IDiagnostic, JsonPath } from '@stoplight/types';
-import { JSONSchema7 } from 'json-schema';
+import type { JSONSchema7 } from 'json-schema';
 import type { Resolver } from '@stoplight/spectral-ref-resolver';
 
 export interface IConstructorOpts {
diff --git a/packages/functions/src/schema/index.ts b/packages/functions/src/schema/index.ts
index 44878763..a7262517 100644
--- a/packages/functions/src/schema/index.ts
+++ b/packages/functions/src/schema/index.ts
@@ -1,5 +1,5 @@
 import type { ErrorObject } from 'ajv';
-import * as betterAjvErrors from '@stoplight/better-ajv-errors';
+import betterAjvErrors from '@stoplight/better-ajv-errors';
 import { detectDialect } from '@stoplight/spectral-formats';
 import { createAjvInstances } from './ajv';
 import MissingRefError from 'ajv/dist/compile/ref_error';
diff --git a/packages/ruleset-bundler/src/utils/isPackageImport.ts b/packages/ruleset-bundler/src/utils/isPackageImport.ts
index b24ec968..74148963 100644
--- a/packages/ruleset-bundler/src/utils/isPackageImport.ts
+++ b/packages/ruleset-bundler/src/utils/isPackageImport.ts
@@ -1,4 +1,4 @@
-import * as validate from 'validate-npm-package-name';
+import validate from 'validate-npm-package-name';
 
 const isValidPackageName = (packageName: string): boolean => validate(packageName).validForNewPackages;
 
diff --git a/packages/ruleset-migrator/src/tree/scope.ts b/packages/ruleset-migrator/src/tree/scope.ts
index 7e262c35..bc5c55bc 100644
--- a/packages/ruleset-migrator/src/tree/scope.ts
+++ b/packages/ruleset-migrator/src/tree/scope.ts
@@ -1,4 +1,4 @@
-import * as reserved from 'reserved';
+import reserved from 'reserved';
 
 const REGISTERED_WORDS = [
   ...reserved,
diff --git a/packages/rulesets/src/asyncapi/functions/asyncApi2PayloadValidation.ts b/packages/rulesets/src/asyncapi/functions/asyncApi2PayloadValidation.ts
index 891cef28..fec61d07 100644
--- a/packages/rulesets/src/asyncapi/functions/asyncApi2PayloadValidation.ts
+++ b/packages/rulesets/src/asyncapi/functions/asyncApi2PayloadValidation.ts
@@ -1,7 +1,7 @@
 import Ajv from 'ajv';
 import addFormats from 'ajv-formats';
 import { createRulesetFunction } from '@stoplight/spectral-core';
-import * as betterAjvErrors from '@stoplight/better-ajv-errors';
+import betterAjvErrors from '@stoplight/better-ajv-errors';
 import * as asyncApi2Schema from '../schemas/schema.asyncapi2.json';
 
 const fakeSchemaObjectId = 'asyncapi2#/definitions/schema';
diff --git a/packages/rulesets/src/oas/functions/oasSchema.ts b/packages/rulesets/src/oas/functions/oasSchema.ts
index 8718455b..c5c1aae5 100644
--- a/packages/rulesets/src/oas/functions/oasSchema.ts
+++ b/packages/rulesets/src/oas/functions/oasSchema.ts
@@ -1,5 +1,5 @@
 import type { SchemaObject, ErrorObject } from 'ajv';
-import * as traverse from 'json-schema-traverse';
+import traverse from 'json-schema-traverse';
 import { schema as schemaFn, SchemaOptions } from '@stoplight/spectral-functions';
 import { createRulesetFunction } from '@stoplight/spectral-core';
 import { oas2, oas3_1, extractDraftVersion, oas3_0 } from '@stoplight/spectral-formats';
diff --git a/tsconfig.json b/tsconfig.json
index 6e0496ae..f57c5fa8 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -21,6 +21,7 @@
     "moduleResolution": "node",
     "target": "ES2019",
     "module": "CommonJS",
+    "esModuleInterop": true,
     "lib": ["es2020"],
     "strict": true,
     "pretty": true,


Test patch:
diff --git a/packages/cli/src/formatters/__tests__/pretty.test.ts b/packages/cli/src/formatters/__tests__/pretty.test.ts
index 743335e4..7391f54a 100644
--- a/packages/cli/src/formatters/__tests__/pretty.test.ts
+++ b/packages/cli/src/formatters/__tests__/pretty.test.ts
@@ -1,5 +1,5 @@
 import { DiagnosticSeverity } from '@stoplight/types';
-import * as chalk from 'chalk';
+import chalk from 'chalk';
 import { pretty } from '../pretty';
 
 const oas3SchemaErrors = require('./__fixtures__/oas3-schema-errors.json');
diff --git a/packages/cli/src/formatters/__tests__/stylish.test.ts b/packages/cli/src/formatters/__tests__/stylish.test.ts
index d203f010..4fabf576 100644
--- a/packages/cli/src/formatters/__tests__/stylish.test.ts
+++ b/packages/cli/src/formatters/__tests__/stylish.test.ts
@@ -1,5 +1,5 @@
 import { DiagnosticSeverity } from '@stoplight/types';
-import * as chalk from 'chalk';
+import chalk from 'chalk';
 import { stylish } from '../stylish';
 
 const oas3SchemaErrors = require('./__fixtures__/oas3-schema-errors.json');
diff --git a/packages/cli/src/services/__tests__/linter.test.ts b/packages/cli/src/services/__tests__/linter.test.ts
index 6c75f6ee..4ab846de 100644
--- a/packages/cli/src/services/__tests__/linter.test.ts
+++ b/packages/cli/src/services/__tests__/linter.test.ts
@@ -1,5 +1,5 @@
 import { join, resolve } from '@stoplight/path';
-import * as nock from 'nock';
+import nock from 'nock';
 import * as yargs from 'yargs';
 import lintCommand from '../../commands/lint';
 import { lint } from '../linter';
diff --git a/packages/core/src/__tests__/spectral.jest.test.ts b/packages/core/src/__tests__/spectral.jest.test.ts
index c7e5da5a..38a8984a 100644
--- a/packages/core/src/__tests__/spectral.jest.test.ts
+++ b/packages/core/src/__tests__/spectral.jest.test.ts
@@ -2,7 +2,7 @@ import { normalize } from '@stoplight/path';
 import { truthy, pattern } from '@stoplight/spectral-functions';
 import { DiagnosticSeverity } from '@stoplight/types';
 import * as fs from 'fs';
-import * as nock from 'nock';
+import nock from 'nock';
 import * as path from '@stoplight/path';
 import * as Parsers from '@stoplight/spectral-parsers';
 import { httpAndFileResolver } from '@stoplight/spectral-ref-resolver';
diff --git a/packages/core/src/utils/__tests__/prepareResults.test.ts b/packages/core/src/utils/__tests__/prepareResults.test.ts
index 07960785..02c8c7b4 100644
--- a/packages/core/src/utils/__tests__/prepareResults.test.ts
+++ b/packages/core/src/utils/__tests__/prepareResults.test.ts
@@ -1,6 +1,6 @@
 import { defaultComputeResultFingerprint, prepareResults } from '../prepareResults';
 
-import * as duplicateValidationResults from './__fixtures__/duplicate-validation-results.json';
+import duplicateValidationResults from './__fixtures__/duplicate-validation-results.json';
 
 describe('prepareResults util', () => {
   it('deduplicate exact validation results', () => {
diff --git a/packages/ruleset-migrator/src/__tests__/ruleset.test.ts b/packages/ruleset-migrator/src/__tests__/ruleset.test.ts
index 99bb62e2..cc882da7 100644
--- a/packages/ruleset-migrator/src/__tests__/ruleset.test.ts
+++ b/packages/ruleset-migrator/src/__tests__/ruleset.test.ts
@@ -7,7 +7,7 @@ import { DiagnosticSeverity } from '@stoplight/types';
 import * as fetchMock from 'fetch-mock';
 
 import { migrateRuleset } from '..';
-import * as fixtures from './__fixtures__/.cache/index.json';
+import fixtures from './__fixtures__/.cache/index.json';
 
 const cwd = '/.tmp/spectral';
 
diff --git a/test-utils/node/index.ts b/test-utils/node/index.ts
index 694bb879..4d264828 100644
--- a/test-utils/node/index.ts
+++ b/test-utils/node/index.ts
@@ -1,4 +1,4 @@
-import * as nock from 'nock';
+import nock from 'nock';
 import * as nodeFs from 'fs';
 import { URL } from 'url';
 import { dirname, isURL } from '@stoplight/path';