From 01cb6e0b95c06689e2fba8f532a9a12ef2f45bbf Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Wed, 23 Jun 2021 17:47:53 +0900 Subject: [PATCH] Fix decorator indent --- lib/utils/indent-common.js | 24 +++++++++++++------ lib/utils/indent-ts.js | 9 ++++++- .../script-indent/ts-class-declaration-07.vue | 9 +++++++ .../script-indent/ts-decorator-02.vue | 14 +++++++++++ 4 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 tests/fixtures/script-indent/ts-class-declaration-07.vue create mode 100644 tests/fixtures/script-indent/ts-decorator-02.vue diff --git a/lib/utils/indent-common.js b/lib/utils/indent-common.js index 5934651ef..7d2349d3c 100644 --- a/lib/utils/indent-common.js +++ b/lib/utils/indent-common.js @@ -211,6 +211,10 @@ module.exports.defineVisitor = function create( defaultOptions ) const sourceCode = context.getSourceCode() + /** + * @typedef { { baseToken: Token | null, offset: number, baseline: boolean, expectedIndent: number | undefined } } OffsetData + */ + /** @type {Map} */ const offsets = new Map() const ignoreTokens = new Set() @@ -227,7 +231,7 @@ module.exports.defineVisitor = function create( } if (Array.isArray(token)) { for (const t of token) { - if (t === baseToken) continue + if (!t || t === baseToken) continue offsets.set(t, { baseToken, offset, @@ -260,10 +264,16 @@ module.exports.defineVisitor = function create( return } - setOffset(token, offsetData.offset, offsetData.baseToken) + setOffset( + token, + offsetData.offset, + /** @type {Token} */ (offsetData.baseToken) + ) if (offsetData.baseline) { setBaseline(token) } + const o = /** @type {OffsetData} */ (offsets.get(token)) + o.expectedIndent = offsetData.expectedIndent } /** @@ -722,7 +732,7 @@ module.exports.defineVisitor = function create( * Validate the given token with the pre-calculated expected indentation. * @param {Token} token The token to validate. * @param {number} expectedIndent The expected indentation. - * @param {number[]} [optionalExpectedIndents] The optional expected indentation. + * @param {[number, number?]} [optionalExpectedIndents] The optional expected indentation. * @returns {void} */ function validateCore(token, expectedIndent, optionalExpectedIndents) { @@ -786,8 +796,8 @@ module.exports.defineVisitor = function create( * Get the expected indent of comments. * @param {Token} nextToken The next token of comments. * @param {number} nextExpectedIndent The expected indent of the next token. - * @param {number} lastExpectedIndent The expected indent of the last token. - * @returns {number[]} + * @param {number|undefined} lastExpectedIndent The expected indent of the last token. + * @returns {[number, number?]} */ function getCommentExpectedIndents( nextToken, @@ -873,8 +883,8 @@ module.exports.defineVisitor = function create( baseline.add(token) } else if (baseline.has(offsetInfo.baseToken)) { // The base token is a baseline token on this line, so inherit it. - offsetInfo.expectedIndent = offsets.get( - offsetInfo.baseToken + offsetInfo.expectedIndent = /** @type {OffsetData} */ ( + offsets.get(offsetInfo.baseToken) ).expectedIndent baseline.add(token) } else { diff --git a/lib/utils/indent-ts.js b/lib/utils/indent-ts.js index fbb1e25b2..9641f5a32 100644 --- a/lib/utils/indent-ts.js +++ b/lib/utils/indent-ts.js @@ -1196,7 +1196,14 @@ function defineVisitor({ ) setOffset(startParentToken, 0, atToken) } else { - const startParentToken = tokenStore.getFirstToken(parent) + const startParentToken = tokenStore.getFirstToken( + parent.parent && + (parent.parent.type === 'ExportDefaultDeclaration' || + parent.parent.type === 'ExportNamedDeclaration') && + node.range[0] < parent.parent.range[0] + ? parent.parent + : parent + ) copyOffset(atToken, startParentToken) } } else { diff --git a/tests/fixtures/script-indent/ts-class-declaration-07.vue b/tests/fixtures/script-indent/ts-class-declaration-07.vue new file mode 100644 index 000000000..5568aa6d3 --- /dev/null +++ b/tests/fixtures/script-indent/ts-class-declaration-07.vue @@ -0,0 +1,9 @@ + + diff --git a/tests/fixtures/script-indent/ts-decorator-02.vue b/tests/fixtures/script-indent/ts-decorator-02.vue new file mode 100644 index 000000000..5d663f250 --- /dev/null +++ b/tests/fixtures/script-indent/ts-decorator-02.vue @@ -0,0 +1,14 @@ + +