From c4161e3f0cf198ca5135be9a1d13e0bc1d16d5ce Mon Sep 17 00:00:00 2001 From: Kanad Gupta <8854718+kanadgupta@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:40:28 -0500 Subject: [PATCH] fix(spectral-config): partially revert ESM (#724) * chore: more prettier cleanup * Revert "feat(spectral): migrate to ESM (#718)" This reverts commit b1a4ee50c249e6652164dab58b2db7da13c28c65. * revert: undo dependabot changes * chore: revert dep changes * chore: specify engines * chore: hide test warnings * fix: dynamically import alex * chore: lint --- packages/spectral-config/.eslintrc | 3 +- packages/spectral-config/package.json | 5 ++-- .../spectral-config/src/functions/alex.js | 7 ++--- .../spectral-config/src/functions/index.js | 5 ++++ .../src/functions/sentencePunctuation.js | 4 +-- packages/spectral-config/src/index.js | 28 +++++++------------ .../src/rules/alex-component-description.js | 6 ++-- .../src/rules/alex-component-summary.js | 6 ++-- .../src/rules/alex-operation-description.js | 6 ++-- .../src/rules/alex-operation-summary.js | 6 ++-- .../src/rules/alex-parameter.js | 6 ++-- .../src/rules/alex-response.js | 6 ++-- .../rules/description-sentence-punctuation.js | 6 ++-- packages/spectral-config/src/rules/index.js | 13 +++++++++ .../test/alex-component-description.test.js | 5 ++-- .../test/alex-component-summary.test.js | 5 ++-- .../test/alex-operation-description.test.js | 5 ++-- .../test/alex-operation-summary.test.js | 5 ++-- .../test/alex-parameter.test.js | 5 ++-- .../test/alex-response.test.js | 5 ++-- .../description-sentence-punctuation.test.js | 5 ++-- 21 files changed, 71 insertions(+), 71 deletions(-) create mode 100644 packages/spectral-config/src/functions/index.js create mode 100644 packages/spectral-config/src/rules/index.js diff --git a/packages/spectral-config/.eslintrc b/packages/spectral-config/.eslintrc index 00ae48c8..a20a9555 100644 --- a/packages/spectral-config/.eslintrc +++ b/packages/spectral-config/.eslintrc @@ -1,5 +1,4 @@ { "extends": "@readme/eslint-config", - // required for ESM - "rules": { "import/extensions": ["error", "always"] } + "parserOptions": { "ecmaVersion": 2020 } } diff --git a/packages/spectral-config/package.json b/packages/spectral-config/package.json index c2571420..f2a90cd0 100644 --- a/packages/spectral-config/package.json +++ b/packages/spectral-config/package.json @@ -2,14 +2,13 @@ "name": "@readme/spectral-config", "version": "3.0.2", "description": "ReadMe coding standards for API documentation", - "exports": "./src/index.js", - "type": "module", + "main": "src/index.js", "engines": { "node": ">=16" }, "scripts": { "lint": "eslint .", - "test": "vitest run" + "test": "NODE_OPTIONS='--no-deprecation' vitest run" }, "repository": { "type": "git", diff --git a/packages/spectral-config/src/functions/alex.js b/packages/spectral-config/src/functions/alex.js index 464287de..d3a4cf8b 100644 --- a/packages/spectral-config/src/functions/alex.js +++ b/packages/spectral-config/src/functions/alex.js @@ -1,12 +1,11 @@ -import { text } from 'alex'; - /** * Ensure that a given string has considerate and inclusive language. * * @see {@link https://alexjs.com/} * @param {string} input */ -export default function alex(input, options, context) { +module.exports = async function alex(input, options, context) { + const { text } = await import('alex'); const errors = text(input, { profanitySureness: 1 }).messages; return errors @@ -23,4 +22,4 @@ export default function alex(input, options, context) { }; }) .filter(Boolean); -} +}; diff --git a/packages/spectral-config/src/functions/index.js b/packages/spectral-config/src/functions/index.js new file mode 100644 index 00000000..a614e3e3 --- /dev/null +++ b/packages/spectral-config/src/functions/index.js @@ -0,0 +1,5 @@ +/* eslint-disable global-require */ +module.exports = { + alex: require('./alex'), + sentencePunctuation: require('./sentencePunctuation'), +}; diff --git a/packages/spectral-config/src/functions/sentencePunctuation.js b/packages/spectral-config/src/functions/sentencePunctuation.js index 92f76d26..4316d43d 100644 --- a/packages/spectral-config/src/functions/sentencePunctuation.js +++ b/packages/spectral-config/src/functions/sentencePunctuation.js @@ -3,7 +3,7 @@ * * @param {string} input */ -export default function sentencePunctuation(input) { +module.exports = function sentencePunctuation(input) { if (!input) { // Minor protection for when we have a `description` property as part of an endpoint payload, // not a `description` of a schema entry. @@ -19,4 +19,4 @@ export default function sentencePunctuation(input) { } return []; -} +}; diff --git a/packages/spectral-config/src/index.js b/packages/spectral-config/src/index.js index 1ec7d615..6ad7d11e 100644 --- a/packages/spectral-config/src/index.js +++ b/packages/spectral-config/src/index.js @@ -1,27 +1,19 @@ -import { oas } from '@stoplight/spectral-rulesets'; +const { oas } = require('@stoplight/spectral-rulesets'); -import alexComponentDescription from './rules/alex-component-description.js'; -import alexComponentSummary from './rules/alex-component-summary.js'; -import alexOperationDescription from './rules/alex-operation-description.js'; -import alexOperationSummary from './rules/alex-operation-summary.js'; -import alexParameter from './rules/alex-parameter.js'; -import alexResponse from './rules/alex-response.js'; -import descriptionSentencePunctuation from './rules/description-sentence-punctuation.js'; +const rules = require('./rules'); -const config = { +module.exports = { extends: oas, rules: { - 'alex-component-description': alexComponentDescription, - 'alex-component-summary': alexComponentSummary, + 'alex-component-description': rules.alexComponentDescription, + 'alex-component-summary': rules.alexComponentSummary, - 'alex-operation-description': alexOperationDescription, - 'alex-operation-summary': alexOperationSummary, + 'alex-operation-description': rules.alexComponentDescription, + 'alex-operation-summary': rules.alexOperationSummary, - 'alex-parameter': alexParameter, - 'alex-response': alexResponse, + 'alex-parameter': rules.alexParameter, + 'alex-response': rules.alexResponse, - 'description-sentence-punctuation': descriptionSentencePunctuation, + 'description-sentence-punctuation': rules.descriptionSentencePunctuation, }, }; - -export default config; diff --git a/packages/spectral-config/src/rules/alex-component-description.js b/packages/spectral-config/src/rules/alex-component-description.js index 555a356e..e249bb34 100644 --- a/packages/spectral-config/src/rules/alex-component-description.js +++ b/packages/spectral-config/src/rules/alex-component-description.js @@ -1,6 +1,6 @@ -import alex from '../functions/alex.js'; +const { alex } = require('../functions'); -const rule = { +module.exports = { description: 'Component schema description should have inclusive and considerate language.', message: '{{error}}', severity: 'error', @@ -11,5 +11,3 @@ const rule = { function: alex, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/alex-component-summary.js b/packages/spectral-config/src/rules/alex-component-summary.js index 914c6fbb..e16ca641 100644 --- a/packages/spectral-config/src/rules/alex-component-summary.js +++ b/packages/spectral-config/src/rules/alex-component-summary.js @@ -1,6 +1,6 @@ -import alex from '../functions/alex.js'; +const { alex } = require('../functions'); -const rule = { +module.exports = { description: 'Component schema summaries should have inclusive and considerate language.', message: '{{error}}', severity: 'error', @@ -11,5 +11,3 @@ const rule = { function: alex, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/alex-operation-description.js b/packages/spectral-config/src/rules/alex-operation-description.js index 307040ee..e83c1708 100644 --- a/packages/spectral-config/src/rules/alex-operation-description.js +++ b/packages/spectral-config/src/rules/alex-operation-description.js @@ -1,6 +1,6 @@ -import alex from '../functions/alex.js'; +const { alex } = require('../functions'); -const rule = { +module.exports = { description: 'Operation descriptions should have inclusive and considerate language.', message: '{{error}}', severity: 'error', @@ -11,5 +11,3 @@ const rule = { function: alex, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/alex-operation-summary.js b/packages/spectral-config/src/rules/alex-operation-summary.js index 461ad536..caf717d0 100644 --- a/packages/spectral-config/src/rules/alex-operation-summary.js +++ b/packages/spectral-config/src/rules/alex-operation-summary.js @@ -1,6 +1,6 @@ -import alex from '../functions/alex.js'; +const { alex } = require('../functions'); -const rule = { +module.exports = { description: 'Operation summaries should have inclusive and considerate language.', message: '{{error}}', severity: 'error', @@ -11,5 +11,3 @@ const rule = { function: alex, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/alex-parameter.js b/packages/spectral-config/src/rules/alex-parameter.js index 8cd0390d..08296c73 100644 --- a/packages/spectral-config/src/rules/alex-parameter.js +++ b/packages/spectral-config/src/rules/alex-parameter.js @@ -1,6 +1,6 @@ -import alex from '../functions/alex.js'; +const { alex } = require('../functions'); -const rule = { +module.exports = { description: 'Operation parameters should have inclusive and considerate language.', message: '{{error}}', severity: 'error', @@ -11,5 +11,3 @@ const rule = { function: alex, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/alex-response.js b/packages/spectral-config/src/rules/alex-response.js index 77de8430..4e1bc59a 100644 --- a/packages/spectral-config/src/rules/alex-response.js +++ b/packages/spectral-config/src/rules/alex-response.js @@ -1,6 +1,6 @@ -import alex from '../functions/alex.js'; +const { alex } = require('../functions'); -const rule = { +module.exports = { description: 'Operation responses should have inclusive and considerate language.', message: '{{error}}', severity: 'error', @@ -11,5 +11,3 @@ const rule = { function: alex, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/description-sentence-punctuation.js b/packages/spectral-config/src/rules/description-sentence-punctuation.js index 78959286..805c818f 100644 --- a/packages/spectral-config/src/rules/description-sentence-punctuation.js +++ b/packages/spectral-config/src/rules/description-sentence-punctuation.js @@ -1,6 +1,6 @@ -import sentencePunctuation from '../functions/sentencePunctuation.js'; +const { sentencePunctuation } = require('../functions'); -const rule = { +module.exports = { description: 'Descriptions should have ending punctuation.', message: '{{description}}', severity: 'info', @@ -11,5 +11,3 @@ const rule = { function: sentencePunctuation, }, }; - -export default rule; diff --git a/packages/spectral-config/src/rules/index.js b/packages/spectral-config/src/rules/index.js new file mode 100644 index 00000000..b7c152dd --- /dev/null +++ b/packages/spectral-config/src/rules/index.js @@ -0,0 +1,13 @@ +/* eslint-disable global-require */ +module.exports = { + alexComponentDescription: require('./alex-component-description'), + alexComponentSummary: require('./alex-component-summary'), + + alexOperationDescription: require('./alex-operation-description'), + alexOperationSummary: require('./alex-operation-summary'), + + alexParameter: require('./alex-parameter'), + alexResponse: require('./alex-response'), + + descriptionSentencePunctuation: require('./description-sentence-punctuation'), +}; diff --git a/packages/spectral-config/test/alex-component-description.test.js b/packages/spectral-config/test/alex-component-description.test.js index e1dcff50..a458e1e3 100644 --- a/packages/spectral-config/test/alex-component-description.test.js +++ b/packages/spectral-config/test/alex-component-description.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/alex-component-description.js'; +const { alexComponentDescription } = require('../src/rules'); +const rule = alexComponentDescription; const ruleId = 'alex-component-description'; const expectedSeverity = severityCodes.error; const expectedErrorMsg = 'Don’t use `fucking`, it’s profane'; diff --git a/packages/spectral-config/test/alex-component-summary.test.js b/packages/spectral-config/test/alex-component-summary.test.js index 97a99b18..351a7f00 100644 --- a/packages/spectral-config/test/alex-component-summary.test.js +++ b/packages/spectral-config/test/alex-component-summary.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/alex-component-summary.js'; +const { alexComponentSummary } = require('../src/rules'); +const rule = alexComponentSummary; const ruleId = 'alex-component-summary'; const expectedSeverity = severityCodes.error; const expectedErrorMsg = 'Don’t use `fucking`, it’s profane'; diff --git a/packages/spectral-config/test/alex-operation-description.test.js b/packages/spectral-config/test/alex-operation-description.test.js index bf53dd65..08d3b546 100644 --- a/packages/spectral-config/test/alex-operation-description.test.js +++ b/packages/spectral-config/test/alex-operation-description.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/alex-operation-description.js'; +const { alexOperationDescription } = require('../src/rules'); +const rule = alexOperationDescription; const ruleId = 'alex-operation-description'; const expectedSeverity = severityCodes.error; const expectedErrorMsg = 'Don’t use `fucking`, it’s profane'; diff --git a/packages/spectral-config/test/alex-operation-summary.test.js b/packages/spectral-config/test/alex-operation-summary.test.js index 7b7ba5d0..d98e5f88 100644 --- a/packages/spectral-config/test/alex-operation-summary.test.js +++ b/packages/spectral-config/test/alex-operation-summary.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/alex-operation-summary.js'; +const { alexOperationSummary } = require('../src/rules'); +const rule = alexOperationSummary; const ruleId = 'alex-operation-summary'; const expectedSeverity = severityCodes.error; const expectedErrorMsg = 'Don’t use `fucking`, it’s profane'; diff --git a/packages/spectral-config/test/alex-parameter.test.js b/packages/spectral-config/test/alex-parameter.test.js index d836fa79..574ce2ac 100644 --- a/packages/spectral-config/test/alex-parameter.test.js +++ b/packages/spectral-config/test/alex-parameter.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/alex-parameter.js'; +const { alexParameter } = require('../src/rules'); +const rule = alexParameter; const ruleId = 'alex-parameter'; const expectedSeverity = severityCodes.error; const expectedErrorMsg = 'Don’t use `fucking`, it’s profane'; diff --git a/packages/spectral-config/test/alex-response.test.js b/packages/spectral-config/test/alex-response.test.js index 73aa483a..685c77a7 100644 --- a/packages/spectral-config/test/alex-response.test.js +++ b/packages/spectral-config/test/alex-response.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/alex-response.js'; +const { alexResponse } = require('../src/rules'); +const rule = alexResponse; const ruleId = 'alex-response'; const expectedSeverity = severityCodes.error; const expectedErrorMsg = 'Don’t use `fucking`, it’s profane'; diff --git a/packages/spectral-config/test/description-sentence-punctuation.test.js b/packages/spectral-config/test/description-sentence-punctuation.test.js index 2dc7dfe0..1b044d8e 100644 --- a/packages/spectral-config/test/description-sentence-punctuation.test.js +++ b/packages/spectral-config/test/description-sentence-punctuation.test.js @@ -1,9 +1,10 @@ -import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils/index.js'; +import { makeCopy, severityCodes, testRule } from '@ibm-cloud/openapi-ruleset/test/utils'; import readme from '@readme/oas-examples/3.0/json/readme.json'; import { describe, it, expect } from 'vitest'; -import rule from '../src/rules/description-sentence-punctuation.js'; +const { descriptionSentencePunctuation } = require('../src/rules'); +const rule = descriptionSentencePunctuation; const ruleId = 'description-sentence-punctuation'; const expectedSeverity = severityCodes.info; const expectedErrorMsg = 'Descriptions should have ending punctuation.';