Skip to content

Commit

Permalink
update plugin messages
Browse files Browse the repository at this point in the history
  • Loading branch information
zachbryant committed Apr 21, 2024
1 parent fa25630 commit 4af6210
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 45 deletions.
31 changes: 16 additions & 15 deletions src/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,29 @@ export function reportParentNode(
) {
const { context, createReportParentObject } = createReporterArgs
const { loc, messageId } = createReportParentObject(_loc)

const { isInsensitive, isNatural, isRequiredFirst, order } = getOptions(
createReporterArgs.context,
)

let optionsString = [
isRequiredFirst && 'required-first',
isInsensitive && 'insensitive',
isNatural && 'natural',
]
.filter(Boolean)
.join(', ')
if (optionsString) optionsString += ' '

context.report({
loc,
messageId,
data: {
plural: unsortedCount > 1 ? 's' : '',
unsortedCount,
notice: getDeprecationMessage(context.id.split('/').at(-1)!),
order,
options: optionsString,
},
fix: fixerFunction,
})
Expand All @@ -52,9 +68,6 @@ export function reportBodyNodes(
finalIndicesToReport: boolean[],
) {
const { context, createReportPropertiesObject } = createReporterArgs
const { isInsensitive, isNatural, isRequiredFirst, order } = getOptions(
createReporterArgs.context,
)

for (const [node, { finalIndex }] of nodePositions.entries()) {
// If the node is not in the correct position, report it
Expand All @@ -65,15 +78,6 @@ export function reportBodyNodes(
assert(loc, 'createReportObject return value must include a node location')
assert(messageId, 'createReportObject return value must include a problem message')

let optionsString = [
isRequiredFirst && 'required-first',
isInsensitive && 'insensitive',
isNatural && 'natural',
]
.filter(Boolean)
.join(', ')
if (optionsString) optionsString += ' '

context.report({
loc,
messageId,
Expand All @@ -84,9 +88,6 @@ export function reportBodyNodes(
finalIndex + 1 < sortedBody.length
? `before '${getPropertyName(sortedBody[finalIndex + 1])}'`
: 'at the end',
order,
options: optionsString,
notice: getDeprecationMessage(context.id),
},
})
}
Expand Down
8 changes: 4 additions & 4 deletions src/types/error.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export enum ErrorMessage {
InterfaceParentInvalidOrder = `Found {{ unsortedCount }} key{{plural}} out of order.`,
EnumParentInvalidOrder = `Found {{ unsortedCount }} member{{plural}} out of order.{{ notice }}`,
InterfaceInvalidOrder = `Expected interface keys to be in {{ order }}ending {{ options }}order. '{{ nodeName }}' should be {{ messageShouldBeWhere }}. Run autofix to sort entire body.`,
EnumInvalidOrder = `Expected enum members to be in {{ order }}ending {{ options }}order. '{{ nodeName }}' should be {{ messageShouldBeWhere }}. Run autofix to sort entire body.{{ notice }}`,
InterfaceParentInvalidOrder = `Found {{ unsortedCount }} key{{plural}} out of {{ order }}ending {{ options }}order.`,
EnumParentInvalidOrder = `Found {{ unsortedCount }} member{{plural}} out of {{ order }}ending {{ options }}order.{{ notice }}`,
InterfaceInvalidOrder = `Property '{{ nodeName }}' should be {{ messageShouldBeWhere }}.`,
EnumInvalidOrder = `Member '{{ nodeName }}' should be {{ messageShouldBeWhere }}.`,
}
2 changes: 1 addition & 1 deletion src/utils/reportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function getUnsortedInfo(
export function getDeprecationMessage(name: string) {
switch (name) {
case RuleNames.StringEnum:
return `\nThis rule is deprecated. Use \`${PLUGIN_NAME}/${RuleNames.Enum}\` instead.`
return `\nThis rule is deprecated: use \`${PLUGIN_NAME}/${RuleNames.Enum}\` instead.`
default:
return ''
}
Expand Down
10 changes: 4 additions & 6 deletions tests/helpers/cases/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,21 @@ function processErrorArgs(
}

if (!omitInferredErrorCount)
errorMessages.push(getCountErrorString(category, errorArgs.length))
errorMessages.push(getCountErrorString(category, optionsSetsKey, errorArgs.length))

for (const args of errorArgs) {
// At this point args is number or string[]
if (Array.isArray(args)) {
switch (args.length) {
case 1:
errorMessages.push(getEndErrorString(category, optionsSetsKey, args[0]))
errorMessages.push(getEndErrorString(category, args[0]))
break
case 2:
errorMessages.push(
getSwapErrorString(category, optionsSetsKey, args[0], args[1]),
)
errorMessages.push(getSwapErrorString(category, args[0], args[1]))
break
}
} else {
errorMessages.push(getCountErrorString(category, args))
errorMessages.push(getCountErrorString(category, optionsSetsKey, args))
}
}
return errorMessages
Expand Down
29 changes: 10 additions & 19 deletions tests/helpers/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ export enum CaseCategory {
function getCategoryErrorString(category: CaseCategory) {
switch (category) {
case CaseCategory.Interface:
return 'interface keys'
return 'Property'
case CaseCategory.Enum:
default:
return 'enum members'
return 'Member'
}
}

Expand All @@ -45,27 +45,18 @@ function getCategoryParentErrorString(category: CaseCategory) {
}
}

export const getSwapErrorString = (
category: CaseCategory,
order: OptionsSetsKey,
a: string,
b: string,
) => {
return `Expected ${getCategoryErrorString(category)} to be in ${
orderStrings[order]
} order. '${a}' should be before '${b}'. Run autofix to sort entire body.`
export const getSwapErrorString = (category: CaseCategory, a: string, b: string) => {
return `${getCategoryErrorString(category)} '${a}' should be before '${b}'.`
}

export const getEndErrorString = (
export const getEndErrorString = (category: CaseCategory, a: string) =>
`${getCategoryErrorString(category)} '${a}' should be at the end.`

export const getCountErrorString = (
category: CaseCategory,
order: OptionsSetsKey,
a: string,
count: number,
) =>
`Expected ${getCategoryErrorString(category)} to be in ${
orderStrings[order]
} order. '${a}' should be at the end. Run autofix to sort entire body.`

export const getCountErrorString = (category: CaseCategory, count: number) =>
`Found ${count} ${getCategoryParentErrorString(category)}${
count > 1 ? 's' : ''
} out of order.`
} out of ${orderStrings[order]} order.`

0 comments on commit 4af6210

Please sign in to comment.