diff --git a/.changeset/shiny-spiders-deny.md b/.changeset/shiny-spiders-deny.md new file mode 100644 index 0000000..e125873 --- /dev/null +++ b/.changeset/shiny-spiders-deny.md @@ -0,0 +1,7 @@ +--- +'eslint-plugin-import-sorting': patch +--- + +Update export and build strategy + +Should fix issues with plugin being unable to load, or rule definitions not being found. diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..4a6f46f --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,15 @@ +ISC License + +Copyright Jeff Nelson + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/package.json b/package.json index 500b06b..8afd4b5 100644 --- a/package.json +++ b/package.json @@ -14,10 +14,22 @@ ], "repository": "stormwarning/eslint-plugin-import-sorting", "author": "Jeff (https://tidaltheory.io)", - "type": "module", - "main": "dist/index.cjs", + "type": "commonjs", + "exports": { + ".": { + "default": "./dist/index.js" + } + }, + "main": "dist/index.js", + "files": [ + "dist", + "docs", + "CHANGELOG.md", + "LICENSE.txt", + "README.md" + ], "scripts": { - "build": "vite build", + "build": "tsc -b tsconfig.build.json", "changeset": "changeset add", "prepare": "husky install", "release": "npm run build && changeset publish", diff --git a/src/index.ts b/src/index.ts index d9a6f8f..4701f2c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,7 @@ -import { orderRule } from './lib/rules/order.js' +import order from './rules/order.js' -const plugin = { +export = { rules: { - order: orderRule, + order, }, } - -export default plugin diff --git a/src/lib/rules/order.ts b/src/rules/order.ts similarity index 94% rename from src/lib/rules/order.ts rename to src/rules/order.ts index 9f781ef..32462d7 100644 --- a/src/lib/rules/order.ts +++ b/src/rules/order.ts @@ -1,5 +1,4 @@ import { AST_NODE_TYPES, ESLintUtils, type TSESLint, type TSESTree } from '@typescript-eslint/utils' -import type { Rule } from 'eslint' import type { ImportDeclaration } from 'estree' import { mutateRanksToAlphabetize } from '../utils/alphabetize-ranks.js' @@ -118,7 +117,7 @@ const createRule = ESLintUtils.RuleCreator( `https://github.com/stormwarning/eslint-plugin-import-sorting/blob/main/docs/rules/${name}.md`, ) -export const orderRule = createRule({ +export default createRule({ name: 'order', meta: { type: 'layout', @@ -167,7 +166,8 @@ export const orderRule = createRule({ rank: 0, }, ranks, - getBlockImports(node.parent), + /** @todo Maybe get better types for `parent` property? */ + getBlockImports(node.parent as ImportNode), ) } }, @@ -181,6 +181,7 @@ export const orderRule = createRule({ if (node.moduleReference.type === AST_NODE_TYPES.TSExternalModuleReference) { /** @todo Not sure how to properly type this property. */ + // @ts-expect-error -- Need a narrower type for `expression` property. value = node.moduleReference.expression.value as string displayName = value type = 'import' @@ -201,7 +202,8 @@ export const orderRule = createRule({ rank: 0, }, ranks, - getBlockImports(node.parent), + /** @todo Maybe get better types for `parent` property? */ + getBlockImports(node.parent as ImportNode), ) }, 'Program:exit'() { diff --git a/src/lib/utils/alphabetize-ranks.ts b/src/utils/alphabetize-ranks.ts similarity index 100% rename from src/lib/utils/alphabetize-ranks.ts rename to src/utils/alphabetize-ranks.ts diff --git a/src/lib/utils/find-comment.ts b/src/utils/find-comment.ts similarity index 100% rename from src/lib/utils/find-comment.ts rename to src/utils/find-comment.ts diff --git a/src/lib/utils/find-root-node.ts b/src/utils/find-root-node.ts similarity index 82% rename from src/lib/utils/find-root-node.ts rename to src/utils/find-root-node.ts index 36f02b5..83f968a 100644 --- a/src/lib/utils/find-root-node.ts +++ b/src/utils/find-root-node.ts @@ -4,6 +4,7 @@ export function findRootNode(node: ImportNode) { let parent = node /** @todo Not sure how to properly type `parent` property. */ + // @ts-expect-error -- Types don't account for `body` property on `parent`. // eslint-disable-next-line no-eq-null, eqeqeq while (parent.parent != null && parent.parent.body == null) { parent = parent.parent as ImportNode diff --git a/src/lib/utils/make-newlines-between-report.ts b/src/utils/make-newlines-between-report.ts similarity index 94% rename from src/lib/utils/make-newlines-between-report.ts rename to src/utils/make-newlines-between-report.ts index d303a72..9400c1d 100644 --- a/src/lib/utils/make-newlines-between-report.ts +++ b/src/utils/make-newlines-between-report.ts @@ -1,5 +1,3 @@ -/* eslint-disable @typescript-eslint/ban-types */ - import type { TSESLint } from '@typescript-eslint/utils' import type { AST } from 'eslint' @@ -13,7 +11,7 @@ import { findRootNode } from './find-root-node.js' import { takeTokensAfterWhile } from './take-tokens.js' function fixNewLineAfterImport( - context: TSESLint.RuleContext, + context: TSESLint.RuleContext, previousImport: ImportNodeObject, ) { let previousRoot = findRootNode(previousImport.node) @@ -33,7 +31,7 @@ function fixNewLineAfterImport( } function removeNewLineAfterImport( - context: TSESLint.RuleContext, + context: TSESLint.RuleContext, currentImport: ImportNodeObject, previousImport: ImportNodeObject, ) { @@ -53,7 +51,7 @@ function removeNewLineAfterImport( } export function makeNewlinesBetweenReport( - context: TSESLint.RuleContext, + context: TSESLint.RuleContext, imported: ImportNodeObject[], newlinesBetweenImports = 'always', ) { diff --git a/src/lib/utils/make-out-of-order-report.ts b/src/utils/make-out-of-order-report.ts similarity index 88% rename from src/lib/utils/make-out-of-order-report.ts rename to src/utils/make-out-of-order-report.ts index 16047a2..d9747ae 100644 --- a/src/lib/utils/make-out-of-order-report.ts +++ b/src/utils/make-out-of-order-report.ts @@ -1,5 +1,3 @@ -/* eslint-disable @typescript-eslint/ban-types */ - import { AST_NODE_TYPES, type TSESLint } from '@typescript-eslint/utils' import type { ImportNode, ImportNodeObject } from '../rules/order.js' @@ -36,7 +34,10 @@ function isPlainImportModule(node: ImportNode) { } function isPlainImportEquals(node: ImportNode): boolean { - return node.type === AST_NODE_TYPES.TSImportEqualsDeclaration && node.moduleReference.expression + return ( + node.type === AST_NODE_TYPES.TSImportEqualsDeclaration && + 'expression' in node.moduleReference + ) } function canCrossNodeWhileReorder(node: ImportNode) { @@ -50,9 +51,13 @@ function canCrossNodeWhileReorder(node: ImportNode) { function canReorderItems(firstNode: ImportNode, secondNode: ImportNode) { let { parent } = firstNode - let [firstIndex, secondIndex] = ( - [parent.body.indexOf(firstNode), parent.body.indexOf(secondNode)] as [number, number] - ).sort() + // @ts-expect-error -- Types don't account for `body` property on `parent`. + let indices = [parent.body.indexOf(firstNode), parent.body.indexOf(secondNode)] as [ + number, + number, + ] + let [firstIndex, secondIndex] = indices.sort() + // @ts-expect-error -- Types don't account for `body` property on `parent`. let nodesBetween = parent.body.slice(firstIndex, secondIndex + 1) for (let nodeBetween of nodesBetween) { @@ -72,7 +77,7 @@ function makeImportDescription(node: ImportNodeObject) { } function fixOutOfOrder( - context: TSESLint.RuleContext, + context: TSESLint.RuleContext, firstNode: ImportNodeObject, secondNode: ImportNodeObject, order: OrderTerm, @@ -126,7 +131,7 @@ function fixOutOfOrder( } function reportOutOfOrder( - context: TSESLint.RuleContext, + context: TSESLint.RuleContext, imported: ImportNodeObject[], outOfOrder: ImportNodeObject[], order: OrderTerm, @@ -138,7 +143,7 @@ function reportOutOfOrder( } export function makeOutOfOrderReport( - context: TSESLint.RuleContext, + context: TSESLint.RuleContext, imported: ImportNodeObject[], ) { let outOfOrder = findOutOfOrder(imported) diff --git a/src/lib/utils/resolve-import-group.ts b/src/utils/resolve-import-group.ts similarity index 100% rename from src/lib/utils/resolve-import-group.ts rename to src/utils/resolve-import-group.ts diff --git a/src/lib/utils/take-tokens.ts b/src/utils/take-tokens.ts similarity index 100% rename from src/lib/utils/take-tokens.ts rename to src/utils/take-tokens.ts diff --git a/tests/lib/rules/order.ts b/tests/rules/order.ts similarity index 98% rename from tests/lib/rules/order.ts rename to tests/rules/order.ts index 0c226fe..cb2fabb 100644 --- a/tests/lib/rules/order.ts +++ b/tests/rules/order.ts @@ -1,6 +1,6 @@ import { RuleTester } from '@typescript-eslint/rule-tester' -import { orderRule } from '../../../src/lib/rules/order.js' +import orderRule from '../../src/rules/order.js' const ruleTester = new RuleTester({ parser: '@typescript-eslint/parser', diff --git a/tsconfig.build.json b/tsconfig.build.json new file mode 100644 index 0000000..bdf9c5b --- /dev/null +++ b/tsconfig.build.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "node16", + "moduleResolution": "node16", + "declaration": false, + "declarationMap": false + }, + "include": ["src"] +} diff --git a/tsconfig.json b/tsconfig.json index c245c8e..b3a2eb9 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -25,6 +25,6 @@ // Completeness "skipLibCheck": true }, - "include": ["src", "tests", ".eslintrc.cjs", "vite.config.js"], + "include": ["src", "tests", ".eslintrc.cjs", "vite.config.mjs"], "exclude": ["node_modules", "dist"] } diff --git a/vite.config.js b/vite.config.mjs similarity index 78% rename from vite.config.js rename to vite.config.mjs index 330eea8..b99436b 100644 --- a/vite.config.js +++ b/vite.config.mjs @@ -5,8 +5,9 @@ export default defineConfig({ lib: { entry: './src/index.ts', formats: ['cjs'], - fileName: 'index', + fileName: () => 'index.js', }, + minify: false, rollupOptions: { external: ['node:module', 'node:path'], }, @@ -14,6 +15,6 @@ export default defineConfig({ test: { globals: true, environment: 'node', - include: 'tests/lib/**/*.ts', + include: 'tests/**/*.ts', }, })