From 2ce188156d0b91fdcb9606e069a45148c673e6c0 Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Tue, 20 Feb 2024 22:49:30 +0800 Subject: [PATCH] fix: can not import in ESM package (#159) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## PR Checklist - [x] Addresses an existing open issue: fixes #152 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/eslint-plugin-package-json/blob/main/.github/CONTRIBUTING.md) were taken ## Overview related #157 --- .editorconfig | 3 +++ src/index.ts | 18 +++++++++--------- src/rules/order-properties.ts | 2 +- src/rules/prefer-repository-shorthand.ts | 2 +- src/rules/sort-collections.ts | 2 +- src/rules/unique-dependencies.ts | 2 +- src/rules/valid-local-dependency.ts | 2 +- src/rules/valid-name.ts | 2 +- src/rules/valid-package-def.ts | 2 +- src/rules/valid-repository-directory.ts | 2 +- src/rules/valid-version.ts | 2 +- src/tests/rules/order-properties.test.ts | 2 +- .../rules/prefer-repository-shorthand.test.ts | 2 +- src/tests/rules/sort-collections.test.ts | 2 +- src/tests/rules/unique-dependencies.test.ts | 2 +- src/tests/rules/valid-local-dependency.test.ts | 2 +- src/tests/rules/valid-name.test.ts | 2 +- src/tests/rules/valid-package-def.test.ts | 2 +- .../rules/valid-repository-directory.test.ts | 2 +- src/tests/rules/valid-version.test.ts | 2 +- 20 files changed, 30 insertions(+), 27 deletions(-) diff --git a/.editorconfig b/.editorconfig index 3460da02..bbc7504c 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,3 +11,6 @@ trim_trailing_whitespace = true [{package.json,*.yml}] indent_size = 2 indent_style = space + +[*.test.ts] +trim_trailing_whitespace = false diff --git a/src/index.ts b/src/index.ts index 738a197e..31af751a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,12 +1,12 @@ -import orderProperties from "./rules/order-properties.js"; -import preferRepositoryShorthand from "./rules/prefer-repository-shorthand.js"; -import sortCollections from "./rules/sort-collections.js"; -import uniqueDependencies from "./rules/unique-dependencies.js"; -import validLocalDependency from "./rules/valid-local-dependency.js"; -import validName from "./rules/valid-name.js"; -import validPackageDef from "./rules/valid-package-def.js"; -import validRepositoryDirectory from "./rules/valid-repository-directory.js"; -import validVersion from "./rules/valid-version.js"; +import { rule as orderProperties } from "./rules/order-properties.js"; +import { rule as preferRepositoryShorthand } from "./rules/prefer-repository-shorthand.js"; +import { rule as sortCollections } from "./rules/sort-collections.js"; +import { rule as uniqueDependencies } from "./rules/unique-dependencies.js"; +import { rule as validLocalDependency } from "./rules/valid-local-dependency.js"; +import { rule as validName } from "./rules/valid-name.js"; +import { rule as validPackageDef } from "./rules/valid-package-def.js"; +import { rule as validRepositoryDirectory } from "./rules/valid-repository-directory.js"; +import { rule as validVersion } from "./rules/valid-version.js"; export const rules = { "order-properties": orderProperties, diff --git a/src/rules/order-properties.ts b/src/rules/order-properties.ts index b2299412..269e594f 100644 --- a/src/rules/order-properties.ts +++ b/src/rules/order-properties.ts @@ -39,7 +39,7 @@ type Order = "legacy" | "sort-package-json"; type Options = [{ order: Order }?]; -export default createRule({ +export const rule = createRule({ create(context) { return { "Program:exit"() { diff --git a/src/rules/prefer-repository-shorthand.ts b/src/rules/prefer-repository-shorthand.ts index 5f9b4b0e..f7363a38 100644 --- a/src/rules/prefer-repository-shorthand.ts +++ b/src/rules/prefer-repository-shorthand.ts @@ -11,7 +11,7 @@ const isGitHubUrl = (url: string) => githubUrlRegex.test(url); const cleanGitHubUrl = (url: string) => url.replace(githubUrlRegex, "").replace(/\.git$/, ""); -export default createRule({ +export const rule = createRule({ create(context) { return { JSONProperty(node) { diff --git a/src/rules/sort-collections.ts b/src/rules/sort-collections.ts index 30db5e88..4829644d 100644 --- a/src/rules/sort-collections.ts +++ b/src/rules/sort-collections.ts @@ -13,7 +13,7 @@ const defaultCollections = [ type Options = string[]; -export default createRule({ +export const rule = createRule({ create(context) { const toSort = context.options[0] || defaultCollections; return { diff --git a/src/rules/unique-dependencies.ts b/src/rules/unique-dependencies.ts index 51445f23..cf39d9be 100644 --- a/src/rules/unique-dependencies.ts +++ b/src/rules/unique-dependencies.ts @@ -15,7 +15,7 @@ const dependencyPropertyNames = new Set([ "overrides", ]); -export default createRule({ +export const rule = createRule({ create(context) { function check( elements: (JsonAST.JSONNode | null)[], diff --git a/src/rules/valid-local-dependency.ts b/src/rules/valid-local-dependency.ts index d9c7ecf7..64603bcf 100644 --- a/src/rules/valid-local-dependency.ts +++ b/src/rules/valid-local-dependency.ts @@ -2,7 +2,7 @@ import path from "path"; import { createRule } from "../createRule.js"; -export default createRule({ +export const rule = createRule({ create(context) { return { "Program:exit"() { diff --git a/src/rules/valid-name.ts b/src/rules/valid-name.ts index 5788c4ca..e3c5e0b1 100644 --- a/src/rules/valid-name.ts +++ b/src/rules/valid-name.ts @@ -5,7 +5,7 @@ import validate from "validate-npm-package-name"; import { createRule } from "../createRule.js"; -export default createRule({ +export const rule = createRule({ create(context) { return { "Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=name]"( diff --git a/src/rules/valid-package-def.ts b/src/rules/valid-package-def.ts index 42d0fe12..65983132 100644 --- a/src/rules/valid-package-def.ts +++ b/src/rules/valid-package-def.ts @@ -14,7 +14,7 @@ const unusedErrorPatterns = [ const isUsableError = (errorText: string) => unusedErrorPatterns.every((pattern) => !pattern.test(errorText)); -export default createRule({ +export const rule = createRule({ create(context) { return { "Program:exit"() { diff --git a/src/rules/valid-repository-directory.ts b/src/rules/valid-repository-directory.ts index f48daf61..9da15eeb 100644 --- a/src/rules/valid-repository-directory.ts +++ b/src/rules/valid-repository-directory.ts @@ -6,7 +6,7 @@ import * as path from "node:path"; import { createRule } from "../createRule.js"; import { findPropertyWithKeyValue } from "../utils/findPropertyWithKeyValue.js"; -export default createRule({ +export const rule = createRule({ create(context) { return { "Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=repository][value.type=JSONObjectExpression]"( diff --git a/src/rules/valid-version.ts b/src/rules/valid-version.ts index 6f58ecfd..f7840bb3 100644 --- a/src/rules/valid-version.ts +++ b/src/rules/valid-version.ts @@ -5,7 +5,7 @@ import semver from "semver"; import { createRule } from "../createRule.js"; -export default createRule({ +export const rule = createRule({ create(context) { return { "Program > JSONExpressionStatement > JSONObjectExpression > JSONProperty[key.value=version]"( diff --git a/src/tests/rules/order-properties.test.ts b/src/tests/rules/order-properties.test.ts index d5f7a2be..1c73083c 100644 --- a/src/tests/rules/order-properties.test.ts +++ b/src/tests/rules/order-properties.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/order-properties.js"; +import { rule } from "../../rules/order-properties.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("order-properties", rule, { diff --git a/src/tests/rules/prefer-repository-shorthand.test.ts b/src/tests/rules/prefer-repository-shorthand.test.ts index 4768f681..241bf512 100644 --- a/src/tests/rules/prefer-repository-shorthand.test.ts +++ b/src/tests/rules/prefer-repository-shorthand.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/prefer-repository-shorthand.js"; +import { rule } from "../../rules/prefer-repository-shorthand.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("prefer-repository-shorthand", rule, { diff --git a/src/tests/rules/sort-collections.test.ts b/src/tests/rules/sort-collections.test.ts index 0ab28ab7..349783bd 100644 --- a/src/tests/rules/sort-collections.test.ts +++ b/src/tests/rules/sort-collections.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/sort-collections.js"; +import { rule } from "../../rules/sort-collections.js"; import { ruleTester } from "./ruleTester.js"; //------------------------------------------------------------------------------ diff --git a/src/tests/rules/unique-dependencies.test.ts b/src/tests/rules/unique-dependencies.test.ts index 3df0e269..8bbfb91e 100644 --- a/src/tests/rules/unique-dependencies.test.ts +++ b/src/tests/rules/unique-dependencies.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/unique-dependencies.js"; +import { rule } from "../../rules/unique-dependencies.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("unique-dependencies", rule, { diff --git a/src/tests/rules/valid-local-dependency.test.ts b/src/tests/rules/valid-local-dependency.test.ts index 50f2ed3c..ff5fff32 100644 --- a/src/tests/rules/valid-local-dependency.test.ts +++ b/src/tests/rules/valid-local-dependency.test.ts @@ -1,6 +1,6 @@ import path from "path"; -import rule from "../../rules/valid-local-dependency.js"; +import { rule } from "../../rules/valid-local-dependency.js"; import { ruleTester } from "./ruleTester.js"; const fileName = (partialPath: string) => { diff --git a/src/tests/rules/valid-name.test.ts b/src/tests/rules/valid-name.test.ts index 6fc90896..a76f806b 100644 --- a/src/tests/rules/valid-name.test.ts +++ b/src/tests/rules/valid-name.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/valid-name.js"; +import { rule } from "../../rules/valid-name.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("valid-name", rule, { diff --git a/src/tests/rules/valid-package-def.test.ts b/src/tests/rules/valid-package-def.test.ts index d2daa442..8c662e8f 100644 --- a/src/tests/rules/valid-package-def.test.ts +++ b/src/tests/rules/valid-package-def.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/valid-package-def.js"; +import { rule } from "../../rules/valid-package-def.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("valid-package-def", rule, { diff --git a/src/tests/rules/valid-repository-directory.test.ts b/src/tests/rules/valid-repository-directory.test.ts index 5d2e0091..66bc6032 100644 --- a/src/tests/rules/valid-repository-directory.test.ts +++ b/src/tests/rules/valid-repository-directory.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/valid-repository-directory.js"; +import { rule } from "../../rules/valid-repository-directory.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("valid-repository-directory", rule, { diff --git a/src/tests/rules/valid-version.test.ts b/src/tests/rules/valid-version.test.ts index d08292c4..fada342b 100644 --- a/src/tests/rules/valid-version.test.ts +++ b/src/tests/rules/valid-version.test.ts @@ -1,4 +1,4 @@ -import rule from "../../rules/valid-version.js"; +import { rule } from "../../rules/valid-version.js"; import { ruleTester } from "./ruleTester.js"; ruleTester.run("valid-version", rule, {