1 import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils"
2 import { createRule } from "../../create-rule"
3 import { findProperty, getPropertyKeyName } from "../../util/ast"
4
5 type MessageIds =
6 | "multipliedByHundred"
7 | "centsIdentifier"
8 | "largeIntegerLikelyCents"
9
10 const PRICE_KEY_REGEX =
11 /^(amount|unit_price|raw_amount|price|original_total|subtotal|total|tax_total|shipping_total|discount_total)$/
12
13 const CENTS_NAME_REGEX = /(^cents$|_cents$|InCents$)/
14
15 // ISO 4217 currencies with zero minor units (no decimals). For these, an
16 // integer value isn't a strong signal that the amount is "in cents."
17 const ZERO_DECIMAL_CURRENCIES = new Set([
18 "bif",
19 "clp",
20 "djf",
21 "gnf",
22 "isk",
23 "jpy",
24 "kmf",
25 "krw",
26 "mga",
27 "pyg",
28 "rwf",
29 "ugx",
30 "vnd",
31 "vuv",
32 "xaf",
33 "xof",
34 "xpf",
35 ])
36
37 const LARGE_INT_THRESHOLD = 1000
38
39 function isMultiplyByCentsFactor(node: TSESTree.Node): boolean {
40 if (node.type !== AST_NODE_TYPES.BinaryExpression) {
41 return false
42 }
43 if (node.operator !== "*") {
44 return false
45 }
46 const isFactor = (n: TSESTree.Node): boolean =>
47 n.type === AST_NODE_TYPES.Literal && (n.value === 100 || n.value === 1000)
48 return isFactor(node.right) || isFactor(node.left)
49 }
50
51 function getReferenceName(node: TSESTree.Node): string | null {
52 if (node.type === AST_NODE_TYPES.Identifier) {
53 return node.name
54 }
55 if (
56 node.type === AST_NODE_TYPES.MemberExpression &&
57 !node.computed &&
58 node.property.type === AST_NODE_TYPES.Identifier
59 ) {
60 return node.property.name
61 }
62 return null
63 }
64
65 function isCentsReference(node: TSESTree.Node): boolean {
66 const name = getReferenceName(node)
67 if (!name) {
68 return false
69 }
70 return CENTS_NAME_REGEX.test(name)
71 }
72
73 function isLargeIntegerLiteral(node: TSESTree.Node): boolean {
74 if (node.type !== AST_NODE_TYPES.Literal) {
75 return false
76 }
77 if (typeof node.value !== "number") {
78 return false
79 }
80 if (!Number.isInteger(node.value)) {
81 return false
82 }
83 if (node.value < LARGE_INT_THRESHOLD) {
84 return false
85 }
86 // The raw source must have no decimal point — `1000.0` should not trigger.
87 if (typeof node.raw === "string" && node.raw.includes(".")) {
88 return false
89 }
90 return true
91 }
92
93 function getSiblingCurrencyCode(obj: TSESTree.ObjectExpression): string | null {
94 const prop = findProperty(obj, "currency_code")
95 if (!prop) {
96 return null
97 }
98 const value = prop.value
99 if (
100 value.type === AST_NODE_TYPES.Literal &&
101 typeof value.value === "string"
102 ) {
103 return value.value.toLowerCase()
104 }
105 return null
106 }
107
108 export const rule = createRule<[], MessageIds>({
109 name: "prices-in-major-units",
110 meta: {
111 type: "suggestion",
112 docs: {
113 description:
114 "Prices in Medusa are stored in major units (decimals). Flag values that look like they're in the smallest currency unit (cents).",
115 },
116 messages: {
117 multipliedByHundred:
118 "`{{key}}` looks like it's multiplied to cents. Medusa expects prices in major units (decimals).",
119 centsIdentifier:
120 "`{{key}}` is set from a `{{name}}` value that looks like it's in the smallest currency unit. Medusa expects prices in major units (decimals).",
121 largeIntegerLikelyCents:
122 "`{{key}}: {{value}}` looks like it's in the smallest currency unit. Medusa expects prices in major units (decimals).",
123 },
124 schema: [],
125 },
126 defaultOptions: [],
127 create(context) {
128 function checkAssignment(
129 key: string,
130 expr: TSESTree.Node,
131 reportNode: TSESTree.Node,
132 isZeroDecimal: boolean
133 ): void {
134 if (isMultiplyByCentsFactor(expr)) {
135 context.report({
136 node: reportNode,
137 messageId: "multipliedByHundred",
138 data: { key },
139 })
140 return
141 }
142
143 if (isCentsReference(expr)) {
144 context.report({
145 node: reportNode,
146 messageId: "centsIdentifier",
147 data: { key, name: getReferenceName(expr) ?? "" },
148 })
149 return
150 }
151
152 if (isLargeIntegerLiteral(expr) && !isZeroDecimal) {
153 context.report({
154 node: reportNode,
155 messageId: "largeIntegerLikelyCents",
156 data: {
157 key,
158 value: String((expr as TSESTree.Literal).value),
159 },
160 })
161 return
162 }
163 }
164
165 return {
166 ObjectExpression(node) {
167 const currency = getSiblingCurrencyCode(node)
168 const isZeroDecimal =
169 currency !== null && ZERO_DECIMAL_CURRENCIES.has(currency)
170
171 for (const prop of node.properties) {
172 if (prop.type !== AST_NODE_TYPES.Property) {
173 continue
174 }
175 const key = getPropertyKeyName(prop)
176 if (!key || !PRICE_KEY_REGEX.test(key)) {
177 continue
178 }
179 const value = prop.value
180 if (
181 value.type === AST_NODE_TYPES.AssignmentPattern ||
182 value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression
183 ) {
184 continue
185 }
186 checkAssignment(key, value, value, isZeroDecimal)
187 }
188 },
189 VariableDeclarator(node) {
190 if (node.id.type !== AST_NODE_TYPES.Identifier) {
191 return
192 }
193 if (!PRICE_KEY_REGEX.test(node.id.name)) {
194 return
195 }
196 if (!node.init) {
197 return
198 }
199 checkAssignment(node.id.name, node.init, node.init, false)
200 },
201 AssignmentExpression(node) {
202 if (node.operator !== "=") {
203 return
204 }
205 const name = getReferenceName(node.left)
206 if (!name || !PRICE_KEY_REGEX.test(name)) {
207 return
208 }
209 checkAssignment(name, node.right, node.right, false)
210 },
211 }
212 },
213 })
214
215 export default rule