diff --git a/src/packages/core-parts/index.ts b/src/packages/core-parts/index.ts index 09a979a..05f6131 100644 --- a/src/packages/core-parts/index.ts +++ b/src/packages/core-parts/index.ts @@ -38,54 +38,36 @@ function parseLineByLine( rangeStartOfLine <= rangeStartOfBrace && rangeEndOfBrace <= rangeEndOfLine, ); const parts: LinePart[] = []; - let maybeLastPart: LinePart | null = null; const offset = indentUnit.length * indentLevel; - const trimmedLine = line.slice(offset); // base of 'mutableLine' - let mutableLine = trimmedLine; - if (braceNodesInCurrentLine.length === 0) { + let temporaryRangeEnd = rangeEndOfLine; + + for (let index = braceNodesInCurrentLine.length - 1; index >= 0; index -= 1) { + const braceNode = braceNodesInCurrentLine[index]; + const [rangeStartOfBrace, rangeEndOfBrace] = braceNode.range; + parts.push({ type: 'Text', - body: mutableLine, + body: formattedText.slice(rangeEndOfBrace, temporaryRangeEnd), }); - } else { - const lastBraceNodeInCurrentLine = braceNodesInCurrentLine.pop()!; - - if ( - lastBraceNodeInCurrentLine.type === BraceType.OB || - lastBraceNodeInCurrentLine.type === BraceType.OBTO - ) { - maybeLastPart = { - type: lastBraceNodeInCurrentLine.type, - body: formattedText.slice(lastBraceNodeInCurrentLine.range[0], rangeEndOfLine), - }; - mutableLine = formattedText.slice( - rangeStartOfLine + offset, - lastBraceNodeInCurrentLine.range[0], - ); - } else { - braceNodesInCurrentLine.push(lastBraceNodeInCurrentLine); - } - - if (braceNodesInCurrentLine.length) { - parts.push({ - type: BraceType.CB, - body: '}', - }); - mutableLine = mutableLine.slice(1); - } - - if (mutableLine) { - parts.push({ - type: 'Text', - body: mutableLine, - }); - } - - if (maybeLastPart) { - parts.push(maybeLastPart); - } + parts.push({ + type: braceNode.type, + body: formattedText.slice(rangeStartOfBrace, rangeEndOfBrace), + }); + temporaryRangeEnd = rangeStartOfBrace; + } + parts.push({ + type: 'Text', + body: formattedText.slice(rangeStartOfLine, temporaryRangeEnd).slice(offset), + }); + parts.reverse(); + + if (parts.length > 1 && parts[0].body === '') { + parts.shift(); + } + if (parts.length > 1 && parts[parts.length - 1].body === '') { + parts.pop(); } rangeStartOfLine = rangeEndOfLine + EOL.length; @@ -98,63 +80,83 @@ function parseLineByLine( } /** - * If `Text` exists after `ClosingBrace`, add a line break between `ClosingBrace` and `Text`. + * Add a line break after `ClosingBrace`. */ -function splitLineStartingWithClosingBrace(lineNodes: LineNode[]) { - for (let index = lineNodes.length - 1; index >= 0; index -= 1) { - const { indentLevel, parts } = lineNodes[index]; - const firstPart = parts.at(0); - - if (firstPart?.type === BraceType.CB) { - const secondPart = parts.at(1); - - if (secondPart) { - lineNodes.splice( - index, - 1, - { indentLevel, parts: [firstPart] }, - { - indentLevel, - parts: [ - { type: secondPart.type, body: secondPart.body.trimStart() }, - ...parts.slice(2), - ], - }, - ); +function splitLineContainingClosingBrace(lineNodes: LineNode[]) { + for (let lineIndex = lineNodes.length - 1; lineIndex >= 0; lineIndex -= 1) { + const { indentLevel, parts } = lineNodes[lineIndex]; + const temporaryLineNodes: LineNode[] = []; + + let temporaryPartIndex = 0; + + for (let partIndex = 0; partIndex < parts.length; partIndex += 1) { + const currentPart = parts[partIndex]; + + if (currentPart.type === BraceType.CB) { + temporaryLineNodes.push({ + indentLevel, + parts: parts.slice(temporaryPartIndex, partIndex + 1), + }); + temporaryPartIndex = partIndex + 1; + + const rightPart = parts.at(partIndex + 1); + + if (rightPart) { + rightPart.body = rightPart.body.trimStart(); + } } } + temporaryLineNodes.push({ + indentLevel, + parts: parts.slice(temporaryPartIndex), + }); + + lineNodes.splice( + lineIndex, + 1, + ...temporaryLineNodes.filter((lineNode) => lineNode.parts.length), + ); } } /** - * If `Text` exists before `OpeningBrace`, add a line break between `OpeningBrace` and `Text`. + * Add a line break before `OpeningBrace`. */ -function splitLineEndingWithOpeningBrace(lineNodes: LineNode[]) { - for (let index = lineNodes.length - 1; index >= 0; index -= 1) { - const { indentLevel, parts } = lineNodes[index]; - const lastPart = parts.at(-1); - - if (lastPart?.type === BraceType.OB || lastPart?.type === BraceType.OBTO) { - const secondLastPart = parts.at(-2); - - if (secondLastPart) { - lineNodes.splice( - index, - 1, - { - indentLevel, - parts: [ - ...parts.slice(0, -2), - { type: secondLastPart.type, body: secondLastPart.body.trimEnd() }, - ], - }, - { - indentLevel: lastPart?.type === BraceType.OBTO ? indentLevel + 1 : indentLevel, - parts: [lastPart], - }, - ); +function splitLineContainingOpeningBrace(lineNodes: LineNode[]) { + for (let lineIndex = lineNodes.length - 1; lineIndex >= 0; lineIndex -= 1) { + const { indentLevel, parts } = lineNodes[lineIndex]; + const temporaryLineNodes: LineNode[] = []; + + let temporaryPartIndex = parts.length; + + for (let partIndex = parts.length - 1; partIndex >= 0; partIndex -= 1) { + const currentPart = parts[partIndex]; + + if (currentPart.type === BraceType.OB || currentPart.type === BraceType.OBTO) { + temporaryLineNodes.push({ + indentLevel: currentPart.type === BraceType.OBTO ? indentLevel + 1 : indentLevel, + parts: parts.slice(partIndex, temporaryPartIndex), + }); + temporaryPartIndex = partIndex; + + const leftPart = parts.at(partIndex - 1); + + if (leftPart) { + leftPart.body = leftPart.body.trimEnd(); + } } } + temporaryLineNodes.push({ + indentLevel, + parts: parts.slice(0, temporaryPartIndex), + }); + temporaryLineNodes.reverse(); + + lineNodes.splice( + lineIndex, + 1, + ...temporaryLineNodes.filter((lineNode) => lineNode.parts.length), + ); } } @@ -197,10 +199,10 @@ export function parseLineByLineAndAssemble( const lineNodes = parseLineByLine(formattedText, indentUnit, targetBraceNodes); - splitLineStartingWithClosingBrace(lineNodes); + splitLineContainingClosingBrace(lineNodes); if (options.braceStyle === 'allman') { - splitLineEndingWithOpeningBrace(lineNodes); + splitLineContainingOpeningBrace(lineNodes); } return assembleLine(lineNodes, indentUnit); diff --git a/tests/v2-test/astro/issue-25/1tbs.test.ts b/tests/v2-test/astro/issue-25/1tbs.test.ts index 398efbd..ee69d8f 100644 --- a/tests/v2-test/astro/issue-25/1tbs.test.ts +++ b/tests/v2-test/astro/issue-25/1tbs.test.ts @@ -40,6 +40,34 @@ const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +--- +const renderComponent = ({handleSubmit = () => {}, errors}) => { + return null; +}; +--- + + +`, + output: `--- +const renderComponent = ({ handleSubmit = () => {}, errors }) => { + return null; +}; +--- + + `, }, { diff --git a/tests/v2-test/astro/issue-25/allman.test.ts b/tests/v2-test/astro/issue-25/allman.test.ts index aae981e..45e5c58 100644 --- a/tests/v2-test/astro/issue-25/allman.test.ts +++ b/tests/v2-test/astro/issue-25/allman.test.ts @@ -40,6 +40,38 @@ const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +--- +const renderComponent = ({handleSubmit = () => {}, errors}) => { + return null; +}; +--- + + +`, + output: `--- +const renderComponent = ({ handleSubmit = () => +{}, errors }) => +{ + return null; +}; +--- + + `, }, { diff --git a/tests/v2-test/astro/issue-25/stroustrup.test.ts b/tests/v2-test/astro/issue-25/stroustrup.test.ts index 0ee08d5..c748a07 100644 --- a/tests/v2-test/astro/issue-25/stroustrup.test.ts +++ b/tests/v2-test/astro/issue-25/stroustrup.test.ts @@ -40,6 +40,34 @@ const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +--- +const renderComponent = ({handleSubmit = () => {}, errors}) => { + return null; +}; +--- + + +`, + output: `--- +const renderComponent = ({ handleSubmit = () => {}, errors }) => { + return null; +}; +--- + + `, }, { diff --git a/tests/v2-test/babel/issue-25/1tbs.test.ts b/tests/v2-test/babel/issue-25/1tbs.test.ts index d42c331..81386b6 100644 --- a/tests/v2-test/babel/issue-25/1tbs.test.ts +++ b/tests/v2-test/babel/issue-25/1tbs.test.ts @@ -25,6 +25,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v2-test/babel/issue-25/allman.test.ts b/tests/v2-test/babel/issue-25/allman.test.ts index 7e52885..e6ad963 100644 --- a/tests/v2-test/babel/issue-25/allman.test.ts +++ b/tests/v2-test/babel/issue-25/allman.test.ts @@ -25,6 +25,32 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => +{}, errors }) => +{ + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v2-test/babel/issue-25/stroustrup.test.ts b/tests/v2-test/babel/issue-25/stroustrup.test.ts index 59d714c..f01e622 100644 --- a/tests/v2-test/babel/issue-25/stroustrup.test.ts +++ b/tests/v2-test/babel/issue-25/stroustrup.test.ts @@ -25,6 +25,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v2-test/typescript/issue-25/1tbs.test.ts b/tests/v2-test/typescript/issue-25/1tbs.test.ts index 7e951b9..f676adf 100644 --- a/tests/v2-test/typescript/issue-25/1tbs.test.ts +++ b/tests/v2-test/typescript/issue-25/1tbs.test.ts @@ -24,6 +24,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v2-test/typescript/issue-25/allman.test.ts b/tests/v2-test/typescript/issue-25/allman.test.ts index 938a2f8..b723d82 100644 --- a/tests/v2-test/typescript/issue-25/allman.test.ts +++ b/tests/v2-test/typescript/issue-25/allman.test.ts @@ -24,6 +24,32 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => +{}, errors }) => +{ + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v2-test/typescript/issue-25/stroustrup.test.ts b/tests/v2-test/typescript/issue-25/stroustrup.test.ts index 5282576..4cbe53e 100644 --- a/tests/v2-test/typescript/issue-25/stroustrup.test.ts +++ b/tests/v2-test/typescript/issue-25/stroustrup.test.ts @@ -24,6 +24,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v2-test/vue/issue-25/1tbs.test.ts b/tests/v2-test/vue/issue-25/1tbs.test.ts index ed7ca9f..ea236a8 100644 --- a/tests/v2-test/vue/issue-25/1tbs.test.ts +++ b/tests/v2-test/vue/issue-25/1tbs.test.ts @@ -56,6 +56,50 @@ const foo = { Click Me +`, + }, + { + name: 'destructuring assignment with default value', + input: ` + + + +`, + output: ` + + `, }, { diff --git a/tests/v2-test/vue/issue-25/allman.test.ts b/tests/v2-test/vue/issue-25/allman.test.ts index a91e938..f371b62 100644 --- a/tests/v2-test/vue/issue-25/allman.test.ts +++ b/tests/v2-test/vue/issue-25/allman.test.ts @@ -57,6 +57,55 @@ const foo = { Click Me +`, + }, + { + name: 'destructuring assignment with default value', + input: ` + + + +`, + output: ` + + `, }, { diff --git a/tests/v2-test/vue/issue-25/stroustrup.test.ts b/tests/v2-test/vue/issue-25/stroustrup.test.ts index c830281..b45c82f 100644 --- a/tests/v2-test/vue/issue-25/stroustrup.test.ts +++ b/tests/v2-test/vue/issue-25/stroustrup.test.ts @@ -56,6 +56,50 @@ const foo = { Click Me +`, + }, + { + name: 'destructuring assignment with default value', + input: ` + + + +`, + output: ` + + `, }, { diff --git a/tests/v3-test/astro/issue-25/1tbs.test.ts b/tests/v3-test/astro/issue-25/1tbs.test.ts index b15d20c..26eb936 100644 --- a/tests/v3-test/astro/issue-25/1tbs.test.ts +++ b/tests/v3-test/astro/issue-25/1tbs.test.ts @@ -40,6 +40,34 @@ const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +--- +const renderComponent = ({handleSubmit = () => {}, errors}) => { + return null; +}; +--- + + +`, + output: `--- +const renderComponent = ({ handleSubmit = () => {}, errors }) => { + return null; +}; +--- + + `, }, { diff --git a/tests/v3-test/astro/issue-25/allman.test.ts b/tests/v3-test/astro/issue-25/allman.test.ts index 09df198..343c197 100644 --- a/tests/v3-test/astro/issue-25/allman.test.ts +++ b/tests/v3-test/astro/issue-25/allman.test.ts @@ -40,6 +40,38 @@ const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +--- +const renderComponent = ({handleSubmit = () => {}, errors}) => { + return null; +}; +--- + + +`, + output: `--- +const renderComponent = ({ handleSubmit = () => +{}, errors }) => +{ + return null; +}; +--- + + `, }, { diff --git a/tests/v3-test/astro/issue-25/stroustrup.test.ts b/tests/v3-test/astro/issue-25/stroustrup.test.ts index d6d634a..f6db71a 100644 --- a/tests/v3-test/astro/issue-25/stroustrup.test.ts +++ b/tests/v3-test/astro/issue-25/stroustrup.test.ts @@ -40,6 +40,34 @@ const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +--- +const renderComponent = ({handleSubmit = () => {}, errors}) => { + return null; +}; +--- + + +`, + output: `--- +const renderComponent = ({ handleSubmit = () => {}, errors }) => { + return null; +}; +--- + + `, }, { diff --git a/tests/v3-test/babel/issue-25/1tbs.test.ts b/tests/v3-test/babel/issue-25/1tbs.test.ts index c681cda..7e2054b 100644 --- a/tests/v3-test/babel/issue-25/1tbs.test.ts +++ b/tests/v3-test/babel/issue-25/1tbs.test.ts @@ -25,6 +25,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v3-test/babel/issue-25/allman.test.ts b/tests/v3-test/babel/issue-25/allman.test.ts index f25ee87..853b015 100644 --- a/tests/v3-test/babel/issue-25/allman.test.ts +++ b/tests/v3-test/babel/issue-25/allman.test.ts @@ -25,6 +25,32 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => +{}, errors }) => +{ + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v3-test/babel/issue-25/stroustrup.test.ts b/tests/v3-test/babel/issue-25/stroustrup.test.ts index 99cb998..48b43c4 100644 --- a/tests/v3-test/babel/issue-25/stroustrup.test.ts +++ b/tests/v3-test/babel/issue-25/stroustrup.test.ts @@ -25,6 +25,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v3-test/typescript/issue-25/1tbs.test.ts b/tests/v3-test/typescript/issue-25/1tbs.test.ts index 8569437..844b840 100644 --- a/tests/v3-test/typescript/issue-25/1tbs.test.ts +++ b/tests/v3-test/typescript/issue-25/1tbs.test.ts @@ -24,6 +24,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v3-test/typescript/issue-25/allman.test.ts b/tests/v3-test/typescript/issue-25/allman.test.ts index 7969b05..5244810 100644 --- a/tests/v3-test/typescript/issue-25/allman.test.ts +++ b/tests/v3-test/typescript/issue-25/allman.test.ts @@ -24,6 +24,32 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => +{}, errors }) => +{ + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v3-test/typescript/issue-25/stroustrup.test.ts b/tests/v3-test/typescript/issue-25/stroustrup.test.ts index a2921c1..5242d9d 100644 --- a/tests/v3-test/typescript/issue-25/stroustrup.test.ts +++ b/tests/v3-test/typescript/issue-25/stroustrup.test.ts @@ -24,6 +24,30 @@ const foo = { output: `const foo = { bar: {}, }; +`, + }, + { + name: 'destructuring assignment with default value', + input: ` +const renderComponent = ({handleSubmit = () => {}, errors}) => { + const ref = React.createRef(); + + return render( + + + + ); +}; +`, + output: `const renderComponent = ({ handleSubmit = () => {}, errors }) => { + const ref = React.createRef(); + + return render( + + + , + ); +}; `, }, { diff --git a/tests/v3-test/vue/issue-25/1tbs.test.ts b/tests/v3-test/vue/issue-25/1tbs.test.ts index 69e5ab8..0ed4872 100644 --- a/tests/v3-test/vue/issue-25/1tbs.test.ts +++ b/tests/v3-test/vue/issue-25/1tbs.test.ts @@ -56,6 +56,50 @@ const foo = { Click Me +`, + }, + { + name: 'destructuring assignment with default value', + input: ` + + + +`, + output: ` + + `, }, { diff --git a/tests/v3-test/vue/issue-25/allman.test.ts b/tests/v3-test/vue/issue-25/allman.test.ts index 6e044f3..7b000c1 100644 --- a/tests/v3-test/vue/issue-25/allman.test.ts +++ b/tests/v3-test/vue/issue-25/allman.test.ts @@ -57,6 +57,55 @@ const foo = { Click Me +`, + }, + { + name: 'destructuring assignment with default value', + input: ` + + + +`, + output: ` + + `, }, { diff --git a/tests/v3-test/vue/issue-25/stroustrup.test.ts b/tests/v3-test/vue/issue-25/stroustrup.test.ts index 11f2600..1cb6ce2 100644 --- a/tests/v3-test/vue/issue-25/stroustrup.test.ts +++ b/tests/v3-test/vue/issue-25/stroustrup.test.ts @@ -56,6 +56,50 @@ const foo = { Click Me +`, + }, + { + name: 'destructuring assignment with default value', + input: ` + + + +`, + output: ` + + `, }, {