Skip to content

Mark some arrays as readonly#17725

Merged
6 commits merged into
masterfrom
sourcefile_readonlyarray
Aug 24, 2017
Merged

Mark some arrays as readonly#17725
6 commits merged into
masterfrom
sourcefile_readonlyarray

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Aug 10, 2017

No description provided.

@ghost ghost force-pushed the sourcefile_readonlyarray branch from 2ed10ab to 7ca4506 Compare August 10, 2017 17:17
@ghost ghost requested a review from weswigham August 10, 2017 17:17
Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

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

The change in types I'm generally OK with, but the changes in style, IMO, need justification. If they're because we're not doing things correctly, do we have issues open?

Comment thread src/compiler/program.ts Outdated

if (program.getCompilerOptions().declaration) {
diagnostics = diagnostics.concat(program.getDeclarationDiagnostics(sourceFile, cancellationToken));
diagnostics = [...diagnostics, ...program.getDeclarationDiagnostics(sourceFile, cancellationToken)];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are these changes because we don't infer types neatly between a mutable array's .concat and a ReadonlyArray? Or is it stylistic? If the former, do we have an issue open for it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Unfortunately, we're using three different kinds of concatenation in the codebase: ES6 ..., ES5 .concat, and ES3 ts.concatenate. The .concat() method won't typecheck against readonly arrays (#17076) but does the same thing as .... We should really prefer ts.concatenate which won't allocate output if the inputs are empty, but in this case we're currently returning a mutable Diagnostic[], so we can't use any function that might reuse its readonly inputs -- it has to be a freshly-allocated output.

compilerResult.program ?
ts.filter(compilerResult.program.getSourceFiles(), sourceFile => !Harness.isDefaultLibraryFile(sourceFile.fileName)) :
[]),
const inputSourceFiles = compilerResult.configFileSourceFiles.slice();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How does this change involve ReadonlyArray? What necessitated the change in style?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

OK, this one was mostly just because I didn't want to see a closure in a filter in a ?: in a concat in a map and wanted to break it up. But also because configFileSourceFiles is now readonly and #17076 caused a compile error.

const missing = program.getMissingFilePaths();
assert.isDefined(missing);
assert.deepEqual(missing, ["d:/pretend/nonexistent.ts"]); // Absolute path
assert.deepEqual(missing, ["d:/pretend/nonexistent.ts" as Path]); // Absolute path
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What makes this cast (and the similar ones below) required now, but not before? Should it have been required before?

Copy link
Copy Markdown
Author

@ghost ghost Aug 11, 2017

Choose a reason for hiding this comment

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

Yeah, I think it should have been required before. missing is a ReadonlyArray<Path>, it shouldn't be compared against a string array.

@ghost ghost merged commit e2141ad into master Aug 24, 2017
@ghost ghost deleted the sourcefile_readonlyarray branch August 24, 2017 16:55
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants