eslint-plugin-perfectionist
Version:
ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
101 lines (100 loc) • 3.21 kB
JavaScript
import {
buildCommonJsonSchemas,
buildUseConfigurationIfJsonSchema,
matchesAstSelectorJsonSchema,
} from '../utils/json-schemas/common-json-schemas.js'
import { buildCommonGroupsJsonSchemas } from '../utils/json-schemas/common-groups-json-schemas.js'
import {
EXTRA_SPACING_ERROR,
GROUP_ORDER_ERROR,
MISSED_SPACING_ERROR,
ORDER_ERROR,
} from '../utils/report-errors.js'
import {
partitionByCommentJsonSchema,
partitionByNewLineJsonSchema,
} from '../utils/json-schemas/common-partition-json-schemas.js'
import { buildAstListeners } from '../utils/build-ast-listeners.js'
import { createEslintRule } from '../utils/create-eslint-rule.js'
import { sortImportOrExportAttributes } from './sort-import-attributes/sort-import-or-export-attributes.js'
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
var ORDER_ERROR_ID = 'unexpectedImportAttributesOrder'
var GROUP_ORDER_ERROR_ID = 'unexpectedImportAttributesGroupOrder'
var EXTRA_SPACING_ERROR_ID = 'extraSpacingBetweenImportAttributes'
var MISSED_SPACING_ERROR_ID = 'missedSpacingBetweenImportAttributes'
var defaultOptions = {
fallbackSort: { type: 'unsorted' },
newlinesInside: 'newlinesBetween',
specialCharacters: 'keep',
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
useConfigurationIf: {},
type: 'alphabetical',
ignoreCase: true,
customGroups: [],
locales: 'en-US',
alphabet: '',
order: 'asc',
groups: [],
}
var jsonSchema = {
items: {
properties: {
...buildCommonJsonSchemas(),
...buildCommonGroupsJsonSchemas(),
useConfigurationIf: buildUseConfigurationIfJsonSchema({
additionalProperties: {
matchesAstSelector: matchesAstSelectorJsonSchema,
},
}),
partitionByComment: partitionByCommentJsonSchema,
partitionByNewLine: partitionByNewLineJsonSchema,
},
additionalProperties: false,
type: 'object',
},
uniqueItems: true,
type: 'array',
}
var sort_import_attributes_default = createEslintRule({
meta: {
messages: {
[MISSED_SPACING_ERROR_ID]: MISSED_SPACING_ERROR,
[EXTRA_SPACING_ERROR_ID]: EXTRA_SPACING_ERROR,
[GROUP_ORDER_ERROR_ID]: GROUP_ORDER_ERROR,
[ORDER_ERROR_ID]: ORDER_ERROR,
},
docs: {
url: 'https://perfectionist.dev/rules/sort-import-attributes',
description: 'Enforce sorted import attributes.',
recommended: true,
},
schema: jsonSchema,
type: 'suggestion',
fixable: 'code',
},
create: context =>
buildAstListeners({
nodeTypes: [AST_NODE_TYPES.ImportDeclaration],
sorter: sortImportAttributes,
context,
}),
defaultOptions: [defaultOptions],
name: 'sort-import-attributes',
})
function sortImportAttributes({ matchedAstSelectors, context, node }) {
sortImportOrExportAttributes({
availableMessageIds: {
missedSpacingBetweenMembers: MISSED_SPACING_ERROR_ID,
extraSpacingBetweenMembers: EXTRA_SPACING_ERROR_ID,
unexpectedGroupOrder: GROUP_ORDER_ERROR_ID,
unexpectedOrder: ORDER_ERROR_ID,
},
matchedAstSelectors,
defaultOptions,
context,
node,
})
}
export { sort_import_attributes_default as default, jsonSchema }