Skip to content

Commit

Permalink
Update export and build strategy (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormwarning authored Jan 19, 2024
1 parent 0fd0f91 commit c307eb1
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 30 deletions.
7 changes: 7 additions & 0 deletions .changeset/shiny-spiders-deny.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 3 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
10 changes: 6 additions & 4 deletions src/lib/rules/order.ts → src/rules/order.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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),
)
}
},
Expand All @@ -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'
Expand All @@ -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'() {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* eslint-disable @typescript-eslint/ban-types */

import type { TSESLint } from '@typescript-eslint/utils'
import type { AST } from 'eslint'

Expand All @@ -13,7 +11,7 @@ import { findRootNode } from './find-root-node.js'
import { takeTokensAfterWhile } from './take-tokens.js'

function fixNewLineAfterImport(
context: TSESLint.RuleContext<string, []>,
context: TSESLint.RuleContext<string, never[]>,
previousImport: ImportNodeObject,
) {
let previousRoot = findRootNode(previousImport.node)
Expand All @@ -33,7 +31,7 @@ function fixNewLineAfterImport(
}

function removeNewLineAfterImport(
context: TSESLint.RuleContext<string, []>,
context: TSESLint.RuleContext<string, never[]>,
currentImport: ImportNodeObject,
previousImport: ImportNodeObject,
) {
Expand All @@ -53,7 +51,7 @@ function removeNewLineAfterImport(
}

export function makeNewlinesBetweenReport(
context: TSESLint.RuleContext<string, []>,
context: TSESLint.RuleContext<string, never[]>,
imported: ImportNodeObject[],
newlinesBetweenImports = 'always',
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -72,7 +77,7 @@ function makeImportDescription(node: ImportNodeObject) {
}

function fixOutOfOrder(
context: TSESLint.RuleContext<string, []>,
context: TSESLint.RuleContext<string, never[]>,
firstNode: ImportNodeObject,
secondNode: ImportNodeObject,
order: OrderTerm,
Expand Down Expand Up @@ -126,7 +131,7 @@ function fixOutOfOrder(
}

function reportOutOfOrder(
context: TSESLint.RuleContext<string, []>,
context: TSESLint.RuleContext<string, never[]>,
imported: ImportNodeObject[],
outOfOrder: ImportNodeObject[],
order: OrderTerm,
Expand All @@ -138,7 +143,7 @@ function reportOutOfOrder(
}

export function makeOutOfOrderReport(
context: TSESLint.RuleContext<string, []>,
context: TSESLint.RuleContext<string, never[]>,
imported: ImportNodeObject[],
) {
let outOfOrder = findOutOfOrder(imported)
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion tests/lib/rules/order.ts → tests/rules/order.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "node16",
"moduleResolution": "node16",
"declaration": false,
"declarationMap": false
},
"include": ["src"]
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}
5 changes: 3 additions & 2 deletions vite.config.js → vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ export default defineConfig({
lib: {
entry: './src/index.ts',
formats: ['cjs'],
fileName: 'index',
fileName: () => 'index.js',
},
minify: false,
rollupOptions: {
external: ['node:module', 'node:path'],
},
},
test: {
globals: true,
environment: 'node',
include: 'tests/lib/**/*.ts',
include: 'tests/**/*.ts',
},
})

0 comments on commit c307eb1

Please sign in to comment.