Skip to content

Fix GH#18217 issue for FileLog. #27430

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

Merged
2 commits merged into from
Oct 2, 2018
Merged

Fix GH#18217 issue for FileLog. #27430

2 commits merged into from
Oct 2, 2018

Conversation

valera-rozuvan
Copy link
Contributor

Fixes occurrence of #18217 in FileLog class.

@weswigham weswigham requested a review from a user September 28, 2018 21:49
@ghost
Copy link

ghost commented Oct 1, 2018

The only reason this removes the need for a cast is because we don't have "strictPropertyInitialization": true, set in tsconfig-base.json -- if that were set then there would be an error at private readonly logFile: string;. As it is currently you've fooled the compiler into thinking logFile can't be undefined, when it actually can since it's only assigned conditionally in the constructor -- but the error will come back when we enable strictPropertyInitialization.

I think a simpler fix would be to just add if (this.logEnabled && this.logFile !== undefined) { ... } around the body of writeLine -- so far it's worked because callers checked isEnabled() first, but it would actually be simpler and safer if they didn't and just called writeLine unconditionally and let us handle everything in there. @sheetalkamat thoughts?

@sheetalkamat
Copy link
Member

I think keeping isEnabled is good idea since we don't want to create potential string concats if not necessary. It would be good idea to assert isEnabled instead?

@ghost
Copy link

ghost commented Oct 1, 2018

Maybe we could generate the string lazily? writeLine(getText: () => string)

@sheetalkamat
Copy link
Member

If done that way, it needs to create unnecessary function expresions.

@sheetalkamat sheetalkamat reopened this Oct 1, 2018
@valera-rozuvan
Copy link
Contributor Author

valera-rozuvan commented Oct 1, 2018

@Andy-MS

I have updated this PR. Now the parameter to logFile constructor has explicit type string | undefined. No need for conditional if on every call of writeLine. Please check ;)

@@ -13,15 +11,25 @@ namespace ts.server.typingsInstaller {

class FileLog implements Log {
private logEnabled = true;
constructor(private readonly logFile?: string) {
private readonly logFile: string = '';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if logEnabled was just rolled into the optionality of logFile -- if the log is enabled logFile should be set, otherwise logFile will be undefined. Then we don't have to initialize logFile with a value it should never actually use.

@valera-rozuvan
Copy link
Contributor Author

valera-rozuvan commented Oct 1, 2018

@Andy-MS I updated the PR, didn't see your last comment at first.

If I got your last comment correct, you think that we should use logFile property as a means to figure out if the instance of the class is enabled or not? If it's undefined, then the instance is disabled. Otherwise, it is a string, and we can proceed to try writing the file. If we get an error, we set logFile to undefined, and next time we can bail out. RIght?

Or do you like the current state of my proposed changes?

@ghost
Copy link

ghost commented Oct 2, 2018

Right.

@valera-rozuvan
Copy link
Contributor Author

@Andy-MS Updated PR.

@ghost ghost merged commit b15d6a4 into microsoft:master Oct 2, 2018
@valera-rozuvan valera-rozuvan deleted the fix_filelog_gh18217 branch October 2, 2018 18:26
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants