Skip to content

Commit

Permalink
Fix decorator indent
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Jun 25, 2021
1 parent ba9511f commit 01cb6e0
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
24 changes: 17 additions & 7 deletions lib/utils/indent-common.js
Expand Up @@ -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<Token|null, OffsetData>} */
const offsets = new Map()
const ignoreTokens = new Set()

Expand All @@ -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,
Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion lib/utils/indent-ts.js
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions tests/fixtures/script-indent/ts-class-declaration-07.vue
@@ -0,0 +1,9 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<script lang="ts">
import {Component, Prop, Vue} from "vue-property-decorator";
@Component
export default class UserComp extends Vue {
private id = "UserComp";
}
</script>
14 changes: 14 additions & 0 deletions tests/fixtures/script-indent/ts-decorator-02.vue
@@ -0,0 +1,14 @@
<!--{"parserOptions": {"parser":"@typescript-eslint/parser"}}-->
<script lang="ts">
@
(
d
)
@
(
d
)
.d
export class Foo {
}
</script>

0 comments on commit 01cb6e0

Please sign in to comment.