Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addStatements fails if containing block is only indented a portion of an indentation level #335

Closed
cancerberoSgx opened this issue May 27, 2018 · 2 comments
Labels

Comments

@cancerberoSgx
Copy link
Contributor

I'm kind of short of time to investigate this further - so I just report it so I don't loose track of this: If you try to addStatement() to a target method/function/arrow that is already indented it will break:

import Project from 'ts-simple-ast'
  const project = new Project()
  const sourceFile = project.createSourceFile('created.ts', `
function nonIndented(){
}
  function indentedF(){
  
  }
`)
sourceFile.getFunction('nonIndented').addStatements('return null;')
console.log(sourceFile.getText())


sourceFile.getFunction('indentedF').addStatements('return null;')
console.log(sourceFile.getText())

calling addStatements on nonIndented function works nut on indentedF fails throwing this exceptino:

/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/code-block-writer/dist/code-block-writer.js:380
        this._text += Array(this._currentIndentation + 1).join(this._indentationText);
                      ^
RangeError: Invalid array length
    at CodeBlockWriter._writeIndentation (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/code-block-writer/dist/code-block-writer.js:380:23)
    at CodeBlockWriter.writeIndividual (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/code-block-writer/dist/code-block-writer.js:317:26)
    at /home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/code-block-writer/dist/code-block-writer.js:311:29
    at Array.forEach (<anonymous>)
    at CodeBlockWriter._writeIndentingNewLines (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/code-block-writer/dist/code-block-writer.js:306:15)
    at CodeBlockWriter.write (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/code-block-writer/dist/code-block-writer.js:216:14)
    at printTextFromStringOrWriter (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/ts-simple-ast/dist/utils/getTextFromStringOrWriter.js:11:16)
    at Object.getTextFromStringOrWriter (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/ts-simple-ast/dist/utils/getTextFromStringOrWriter.js:5:5)
    at SyntaxList.insertChildText (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/ts-simple-ast/dist/compiler/common/SyntaxList.js:22:34)
    at FunctionDeclaration.StatementedNode.class_1.insertStatements (/home/sg/git/typescript-plugins-of-mine/typescript-plugin-proactive-code-fixes/node_modules/ts-simple-ast/dist/compiler/statement/StatementedNode.js:37:53)
@dsherret dsherret added the bug label May 27, 2018
@dsherret
Copy link
Owner

Thanks! I took a look and it's because the result of getting the indentation is 1.5. I didn't know it could be that. I'll fix code-block-writer to handle this situation.

@dsherret dsherret changed the title addStatements fails if target method/function/arrow have 1 or more indentation levels addStatements fails if containing block is only indented a portion of an indentation level May 28, 2018
@dsherret
Copy link
Owner

Fixed in 11.2.1.

dsherret added a commit that referenced this issue Jul 4, 2018
dsherret added a commit that referenced this issue Jul 4, 2018
dsherret added a commit that referenced this issue May 14, 2019
…y indented a portion of an indentation level.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants