-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Innei <[email protected]>
- Loading branch information
Showing
33 changed files
with
426 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const sortObjectKeys = (obj) => { | ||
if (typeof obj !== 'object' || obj === null) { | ||
return obj | ||
} | ||
|
||
if (Array.isArray(obj)) { | ||
return obj.map((element) => sortObjectKeys(element)) | ||
} | ||
|
||
return Object.keys(obj) | ||
.sort() | ||
.reduce((acc, key) => { | ||
acc[key] = sortObjectKeys(obj[key]) | ||
return acc | ||
}, {}) | ||
} | ||
/** | ||
* @type {import("eslint").ESLint.Plugin} | ||
*/ | ||
export default { | ||
rules: { | ||
'recursive-sort': { | ||
meta: { | ||
type: 'layout', | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
return { | ||
Program(node) { | ||
if (context.getFilename().endsWith('.json')) { | ||
const sourceCode = context.getSourceCode() | ||
const text = sourceCode.getText() | ||
|
||
try { | ||
const json = JSON.parse(text) | ||
const sortedJson = sortObjectKeys(json) | ||
const sortedText = JSON.stringify(sortedJson, null, 2) | ||
|
||
if (text.trim() !== sortedText.trim()) { | ||
context.report({ | ||
node, | ||
message: 'JSON keys are not sorted recursively', | ||
fix(fixer) { | ||
return fixer.replaceText(node, sortedText) | ||
}, | ||
}) | ||
} | ||
} catch (error) { | ||
context.report({ | ||
node, | ||
message: `Invalid JSON: ${error.message}`, | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}, | ||
}, | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.