File size: 7,620 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OffsetCalculator = exports.OffsetContext = void 0;
const ast_1 = require("./ast");
const commons_1 = require("./commons");
const commons_2 = require("./commons");
class OffsetContext {
    constructor(arg) {
        this.offsets = new Map();
        this.ignoreRanges = new Map();
        this.sourceCode = arg.sourceCode;
        this.options = arg.options;
    }
    /**
     * Set offset to the given index.
     */
    setOffsetIndex(index, offset, base) {
        if (index === base) {
            return;
        }
        this.offsets.set(index, {
            type: 0 /* OffsetDataType.normal */,
            base,
            offset
        });
    }
    /**
     * Set align indent to the given index.
     */
    setAlignIndent(index, alignIndent, base) {
        if (index === base) {
            return;
        }
        this.offsets.set(index, {
            type: 1 /* OffsetDataType.align */,
            base,
            alignIndent
        });
    }
    /**
     * Set offset to the given tokens.
     */
    setOffsetToken(token, offset, baseToken) {
        if (!token) {
            return;
        }
        if (Array.isArray(token)) {
            for (const t of token) {
                this.setOffsetToken(t, offset, baseToken);
            }
            return;
        }
        this.setOffsetIndex(token.range[0], offset, baseToken.range[0]);
    }
    /**
     * Copy offset to the given index from srcIndex.
     */
    copyOffset(index, srcIndex) {
        const offsetData = this.offsets.get(srcIndex);
        if (!offsetData) {
            return;
        }
        if (offsetData.type === 2 /* OffsetDataType.start */) {
            this.setStartOffsetIndex(index, offsetData.offset);
        }
        else if (offsetData.type === 1 /* OffsetDataType.align */) {
            this.setAlignIndent(index, offsetData.alignIndent, offsetData.base);
        }
        else {
            this.setOffsetIndex(index, offsetData.offset, offsetData.base);
        }
    }
    /**
     * Set start offset to the given index.
     */
    setStartOffsetIndex(index, offset) {
        this.offsets.set(index, {
            type: 2 /* OffsetDataType.start */,
            offset
        });
    }
    /**
     * Set start offset to the given token.
     */
    setStartOffsetToken(token, offset) {
        if (!token) {
            return;
        }
        if (Array.isArray(token)) {
            for (const t of token) {
                this.setStartOffsetToken(t, offset);
            }
            return;
        }
        this.setStartOffsetIndex(token.range[0], offset);
    }
    setOffsetElementList(nodes, baseNodeOrToken, lastNodeOrToken, offset, align) {
        let setIndent = (token, baseToken) => this.setOffsetToken(token, offset, baseToken);
        if (align) {
            for (const n of nodes) {
                if (n) {
                    if (!(0, commons_1.isBeginningOfLine)(this.sourceCode, n)) {
                        const startLoc = n.loc.start;
                        const alignIndent = startLoc.column - /^\s*/u.exec(this.sourceCode.lines[startLoc.line - 1])[0].length;
                        setIndent = (token, baseToken) => this.setAlignIndent(token.range[0], alignIndent, baseToken.range[0]);
                    }
                    break;
                }
            }
        }
        this._setOffsetElementList(nodes, baseNodeOrToken, lastNodeOrToken, setIndent);
    }
    _setOffsetElementList(nodes, baseNodeOrToken, lastNodeOrToken, setIndent) {
        const baseToken = this.sourceCode.getFirstToken(baseNodeOrToken);
        let prevToken = this.sourceCode.getLastToken(baseNodeOrToken);
        for (const node of nodes) {
            if (node == null) {
                continue;
            }
            const elementTokens = (0, commons_2.getFirstAndLastTokens)(this.sourceCode, node, prevToken.range[1]);
            let t = prevToken;
            while ((t = this.sourceCode.getTokenAfter(t, {
                includeComments: true,
                filter: ast_1.isNotWhitespace
            })) != null &&
                t.range[1] <= elementTokens.firstToken.range[0]) {
                setIndent(t, baseToken);
            }
            setIndent(elementTokens.firstToken, baseToken);
            prevToken = elementTokens.lastToken;
        }
        if (lastNodeOrToken) {
            const lastToken = this.sourceCode.getFirstToken(lastNodeOrToken);
            let t = prevToken;
            while ((t = this.sourceCode.getTokenAfter(t, {
                includeComments: true,
                filter: ast_1.isNotWhitespace
            })) != null &&
                t.range[1] <= lastToken.range[0]) {
                setIndent(t, baseToken);
            }
            this.setOffsetToken(lastToken, 0, baseToken);
        }
    }
    /**
     * Ignore range of the given node.
     */
    ignore(node) {
        const range = node.range;
        const n = this.ignoreRanges.get(range[0]) ?? 0;
        this.ignoreRanges.set(range[0], Math.max(n, range[1]));
    }
    getOffsetCalculator() {
        // eslint-disable-next-line @typescript-eslint/no-use-before-define -- ignore
        return new OffsetCalculator({
            offsets: this.offsets,
            options: this.options,
            ignoreRanges: [...this.ignoreRanges]
        });
    }
}
exports.OffsetContext = OffsetContext;
class OffsetCalculator {
    constructor(arg) {
        this.offsets = arg.offsets;
        this.options = arg.options;
        this.ignoreRanges = arg.ignoreRanges;
    }
    /**
     * Calculate correct indentation of the given index.
     */
    getExpectedIndentFromIndex(index) {
        const offsetInfo = this.offsets.get(index);
        if (offsetInfo == null) {
            return null;
        }
        if (offsetInfo.expectedIndent != null) {
            return offsetInfo.expectedIndent;
        }
        if (offsetInfo.type === 2 /* OffsetDataType.start */) {
            return offsetInfo.offset * this.options.indentSize;
        }
        const baseIndent = this.getExpectedIndentFromIndex(offsetInfo.base);
        if (baseIndent == null) {
            return null;
        }
        if (offsetInfo.type === 1 /* OffsetDataType.align */) {
            return baseIndent + offsetInfo.alignIndent;
        }
        return baseIndent + offsetInfo.offset * this.options.indentSize;
    }
    /**
     * Calculate correct indentation of the given token.
     */
    getExpectedIndentFromToken(token) {
        return this.getExpectedIndentFromIndex(token.range[0]);
    }
    /**
     * Calculate correct indentation of the line of the given tokens.
     */
    getExpectedIndentFromTokens(tokens) {
        for (const token of tokens) {
            const index = token.range[0];
            if (this.ignoreRanges.some(([f, t]) => f <= index && index < t)) {
                return null;
            }
            const expectedIndent = this.getExpectedIndentFromIndex(index);
            if (expectedIndent != null) {
                return expectedIndent;
            }
        }
        return null;
    }
    /** Save expected indent to give tokens */
    saveExpectedIndent(tokens, expectedIndent) {
        for (const token of tokens) {
            const offsetInfo = this.offsets.get(token.range[0]);
            if (offsetInfo == null) {
                continue;
            }
            offsetInfo.expectedIndent = offsetInfo.expectedIndent ?? expectedIndent;
        }
    }
}
exports.OffsetCalculator = OffsetCalculator;