@@ -220,8 +220,8 @@ class OffsetStorage {
220220
221221 /**
222222 * Sets the offset column of token B to match the offset column of token A.
223- * This is different from matchIndentOf because it matches a *column*, even if baseToken is not
224- * the first token on its line.
223+ * **WARNING**: This is different from matchIndentOf because it matches a *column*, even if baseToken is not
224+ * the first token on its line. In most cases, `matchIndentOf` should be used instead.
225225 * @param {Token } baseToken The first token
226226 * @param {Token } offsetToken The second token, whose offset should be matched to the first token
227227 * @returns {void }
@@ -239,12 +239,62 @@ class OffsetStorage {
239239 }
240240
241241 /**
242- * Sets the desired offset of a token
243- * @param {Token } token The token
244- * @param {Token } offsetFrom The token that this is offset from
245- * @param {number } offset The desired indent level
246- * @returns {void }
247- */
242+ * Sets the desired offset of a token.
243+ *
244+ * This uses a line-based offset collapsing behavior to handle tokens on the same line.
245+ * For example, consider the following two cases:
246+ *
247+ * (
248+ * [
249+ * bar
250+ * ]
251+ * )
252+ *
253+ * ([
254+ * bar
255+ * ])
256+ *
257+ * Based on the first case, it's clear that the `bar` token needs to have an offset of 1 indent level (4 spaces) from
258+ * the `[` token, and the `[` token has to have an offset of 1 indent level from the `(` token. Since the `(` token is
259+ * the first on its line (with an indent of 0 spaces), the `bar` token needs to be offset by 2 indent levels (8 spaces)
260+ * from the start of its line.
261+ *
262+ * However, in the second case `bar` should only be indented by 4 spaces. This is because the offset of 1 indent level
263+ * between the `(` and the `[` tokens gets "collapsed" because the two tokens are on the same line. As a result, the
264+ * `(` token is mapped to the `[` token with an offset of 0, and the rule correctly decides that `bar` should be indented
265+ * by 1 indent level from the start of the line.
266+ *
267+ * This is useful because rule listeners can usually just call `setDesiredOffset` for all the tokens in the node,
268+ * without needing to check which lines those tokens are on.
269+ *
270+ * Note that since collapsing only occurs when two tokens are on the same line, there are a few cases where non-intuitive
271+ * behavior can occur. For example, consider the following cases:
272+ *
273+ * foo(
274+ * ).
275+ * bar(
276+ * baz
277+ * )
278+ *
279+ * foo(
280+ * ).bar(
281+ * baz
282+ * )
283+ *
284+ * Based on the first example, it would seem that `bar` should be offset by 1 indent level from `foo`, and `baz`
285+ * should be offset by 1 indent level from `bar`. However, this is not correct, because it would result in `baz`
286+ * being indented by 2 indent levels in the second case (since `foo`, `bar`, and `baz` are all on separate lines, no
287+ * collapsing would occur).
288+ *
289+ * Instead, the correct way would be to offset `baz` by 1 level from `bar`, offset `bar` by 1 level from the `)`, and
290+ * offset the `)` by 0 levels from `foo`. This ensures that the offset between `bar` and the `)` are correctly collapsed
291+ * in the second case.
292+ *
293+ * @param {Token } token The token
294+ * @param {Token } offsetFrom The token that `token` should be offset from
295+ * @param {number } offset The desired indent level
296+ * @returns {void }
297+ */
248298 setDesiredOffset ( token , offsetFrom , offset ) {
249299 if ( offsetFrom && token . loc . start . line === offsetFrom . loc . start . line ) {
250300 this . matchIndentOf ( offsetFrom , token ) ;
0 commit comments