prettier-plugin-sort-json
Advanced tools
Comparing version 0.0.3 to 1.0.0
@@ -15,4 +15,4 @@ import { Parser } from 'prettier'; | ||
since: string; | ||
type: "path"; | ||
type: "string"; | ||
}; | ||
}; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.options = exports.parsers = void 0; | ||
const path_1 = __importDefault(require("path")); | ||
const fs_1 = require("fs"); | ||
const parser_babel_1 = require("prettier/parser-babel"); | ||
const integerPrefixRegex = /^(\d+)/u; | ||
/** | ||
@@ -22,2 +16,3 @@ * Lexical sort function for strings, meant to be used as the sort | ||
} | ||
const integerPrefixRegex = /^(\d+)/u; | ||
/** | ||
@@ -38,4 +33,10 @@ * Numeric sort function for strings, meant to be used as the sort | ||
if (aPrefixResult !== null && bPrefixResult !== null) { | ||
const aPrefix = parseInt(aPrefixResult[1], 10); | ||
const bPrefix = parseInt(bPrefixResult[1], 10); | ||
// Guaranteed to be non-null because we checked for `null`, and because there is a capture | ||
// group in the regex | ||
/* eslint-disable @typescript-eslint/no-non-null-assertion */ | ||
const rawAPrefix = aPrefixResult[1]; | ||
const rawBPrefix = bPrefixResult[1]; | ||
/* eslint-enable @typescript-eslint/no-non-null-assertion */ | ||
const aPrefix = parseInt(rawAPrefix, 10); | ||
const bPrefix = parseInt(rawBPrefix, 10); | ||
const difference = aPrefix - bPrefix; | ||
@@ -140,8 +141,3 @@ if (difference !== 0) { | ||
const ast = parser_babel_1.parsers.json.parse(text, _parsers, options); | ||
const { filepath, jsonRecursiveSort, jsonSortOrder } = options; | ||
if (jsonSortOrder && | ||
path_1.default.resolve(filepath) === path_1.default.resolve(jsonSortOrder)) { | ||
// Skip sorting the JSON sort order file | ||
return ast; | ||
} | ||
const { jsonRecursiveSort, jsonSortOrder } = options; | ||
// Only objects are intended to be sorted by this plugin | ||
@@ -156,11 +152,19 @@ // Arrays are considered only in recursive mode, so that we | ||
if (jsonSortOrder) { | ||
const customSortOrderContents = (0, fs_1.readFileSync)(jsonSortOrder, 'utf8'); | ||
const parsedCustomSort = JSON.parse(customSortOrderContents); | ||
let parsedCustomSort; | ||
try { | ||
parsedCustomSort = JSON.parse(jsonSortOrder); | ||
} | ||
catch (error) { | ||
// @ts-expect-error Error cause property not yet supported by '@types/node' (see https://github.com/DefinitelyTyped/DefinitelyTyped/pull/61827) | ||
throw new Error(`Failed to parse sort order option as JSON`, { | ||
cause: error, | ||
}); | ||
} | ||
if (Array.isArray(parsedCustomSort) || | ||
typeof parsedCustomSort !== 'object') { | ||
throw new Error(`Invalid custom sort order file; must be an object`); | ||
throw new Error(`Invalid custom sort order; must be an object`); | ||
} | ||
for (const categorySort of Object.values(parsedCustomSort)) { | ||
if (!allowedCategorySortValues.includes(categorySort)) { | ||
throw new Error(`Invalid custom sort file entry: value must be one of '${allowedCategorySortValues}', got '${categorySort}'`); | ||
throw new Error(`Invalid custom sort entry: value must be one of '${String(allowedCategorySortValues)}', got '${String(categorySort)}'`); | ||
} | ||
@@ -174,2 +178,4 @@ } | ||
const [, regexSpec, flags] = entry.match(regexRegex); | ||
// "regexSpec" guaranteed to be defined because of capture group. False positive for unnecessary type assertion. | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | ||
const regex = new RegExp(regexSpec, flags); | ||
@@ -194,3 +200,7 @@ return value.match(regex); | ||
else if (aIndex === bIndex) { | ||
// Sort entry guaranteed to be non-null because index was found | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
const sortEntry = sortEntries[aIndex]; | ||
// Guaranteed to be defined because `sortEntry` is derived from `Object.keys` | ||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion | ||
const categorySort = customSort[sortEntry]; | ||
@@ -218,7 +228,7 @@ const categorySortFunction = categorySort === null | ||
category: 'json-sort', | ||
description: 'A path to a JSON file specifying a custom sort order', | ||
since: '0.0.3', | ||
type: 'path', | ||
description: 'A JSON string specifying a custom sort order', | ||
since: '0.0.4', | ||
type: 'string', | ||
}, | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "prettier-plugin-sort-json", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "Prettier plugin to sort JSON files alphanumerically by key", | ||
@@ -17,3 +17,3 @@ "repository": { | ||
"scripts": { | ||
"build": "tsc --project .", | ||
"build": "tsc --project tsconfig.build.json", | ||
"lint": "yarn lint:eslint && yarn lint:misc --check", | ||
@@ -23,3 +23,3 @@ "lint:eslint": "eslint . --cache --ext js,ts", | ||
"lint:misc": "prettier '**/*.json' '**/*.md' '!CHANGELOG.md' '**/*.yml' '!.yarnrc.yml' '!fixtures/invalid-json.json' --ignore-path .gitignore", | ||
"prepack": "yarn build", | ||
"prepack": "./scripts/prepack.sh", | ||
"test": "jest", | ||
@@ -29,3 +29,3 @@ "test:watch": "jest --watch" | ||
"dependencies": { | ||
"@types/prettier": "^2.3.2" | ||
"@types/prettier": "^2.7.2" | ||
}, | ||
@@ -35,22 +35,22 @@ "devDependencies": { | ||
"@lavamoat/allow-scripts": "^2.0.3", | ||
"@metamask/auto-changelog": "^2.6.1", | ||
"@metamask/eslint-config": "^9.0.0", | ||
"@metamask/eslint-config-jest": "^9.0.0", | ||
"@metamask/eslint-config-nodejs": "^9.0.0", | ||
"@metamask/eslint-config-typescript": "^9.0.0", | ||
"@types/jest": "^28.0.0", | ||
"@typescript-eslint/eslint-plugin": "^4.28.4", | ||
"@typescript-eslint/parser": "^4.28.4", | ||
"eslint": "^7.31.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-jest": "^24.3.6", | ||
"eslint-plugin-jsdoc": "^39.0.0", | ||
"@metamask/auto-changelog": "^3.0.0", | ||
"@metamask/eslint-config": "^11.1.0", | ||
"@metamask/eslint-config-jest": "^11.1.0", | ||
"@metamask/eslint-config-nodejs": "^11.1.0", | ||
"@metamask/eslint-config-typescript": "^11.1.0", | ||
"@types/jest": "^29.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.33.1", | ||
"@typescript-eslint/parser": "^5.33.1", | ||
"eslint": "^8.22.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"eslint-plugin-jest": "^27.1.6", | ||
"eslint-plugin-jsdoc": "^39.3.6", | ||
"eslint-plugin-node": "^11.1.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"jest": "^28.1.3", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"jest": "^29.0.0", | ||
"prettier": "^2.3.2", | ||
"prettier-plugin-packagejson": "^2.2.11", | ||
"ts-jest": "^28.0.0", | ||
"typescript": "~4.7.4" | ||
"ts-jest": "^29.0.0", | ||
"typescript": "~4.9.0" | ||
}, | ||
@@ -60,3 +60,3 @@ "peerDependencies": { | ||
}, | ||
"packageManager": "yarn@3.2.1", | ||
"packageManager": "yarn@3.3.0", | ||
"engines": { | ||
@@ -70,2 +70,2 @@ "node": ">=14.0.0" | ||
} | ||
} | ||
} |
@@ -81,19 +81,15 @@ # prettier-plugin-sort-json | ||
Use a custom sort order. This order is specified using a JSON file that maps exact strings or regular expressions to sorting algorithms. | ||
Use a custom sort order. This is specified as a JSON string that maps exact strings or regular expressions to sorting algorithms. | ||
| Default | CLI | Configuration | | ||
| ------- | -------------------------- | ----------------------- | | ||
| `""` | `--json-sort-order <path>` | `jsonSortOrder: <path>` | | ||
| Default | CLI | Configuration | | ||
| ------- | ------------------------------ | ------------------------- | | ||
| `""` | `--json-sort-order '<string>'` | `jsonSortOrder: <string>` | | ||
Here is an example JSON sort order file: | ||
Here is an example JSON sort order string: | ||
```json | ||
{ | ||
"placeThisFirst": null, | ||
"/^[^\\d+]/": "lexical", | ||
"/^\\d+/": "numeric" | ||
} | ||
``` | ||
'{ "placeThisFirst": null, "/^[^\\d+]/": "lexical", "/^\\d+/": "numeric" }' | ||
``` | ||
This file sorts the key "placeThisFirst" ahead of all others. After that, the set of all keys that _don't_ start with a number are sorted lexically. Lastly, the set of keys that start with a number are sorted numerically. | ||
This sorts the key "placeThisFirst" ahead of all others. After that, the set of all keys that _don't_ start with a number are sorted lexically. Lastly, the set of keys that start with a number are sorted numerically. | ||
@@ -100,0 +96,0 @@ Each key represents a literal key value or a _category_ of keys, represented by a regular expression. Regular expressions are identified by leading and trailing forward slashes, along with some number of paths optionally following the trailing slash (supported flags are `i`, `m`, `s`, and `u`). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
41959
7
243
1
0
0
164
Updated@types/prettier@^2.7.2