Skip to content

add support for readonly modifier #23954

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
merged 4 commits into from
May 10, 2018

Conversation

Kingwl
Copy link
Contributor

@Kingwl Kingwl commented May 8, 2018

Fixes #22143

  • allow readonly modifier
  • do not add set accessor if has readonly modifier
  • update all top level statement what is Initializer for readonly


function updateReadonlyPropertyInitializerStatementConstructor(changeTracker: textChanges.ChangeTracker, file: SourceFile, constructor: ConstructorDeclaration, accessorName: AcceptedNameType, fieldName: AcceptedNameType) {
if (constructor.body) {
const initializerStatement = find(constructor.body.statements, (stmt =>
Copy link
Contributor

Choose a reason for hiding this comment

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

An assignment to the private name can happen anywhere really and not just in expressionStatemetns, it can be in a comma expression nested in a call... you want to use FindAllReferences.getReferenceEntriesForNode here instead of searching the body yourself. a few notes:

  • this could return you nodes outside the body of the constructor, these you want to ignore, so filter out ones that fall outside the pos/end of the constructor
  • you only want to look at places where you are writing to the property, use isWriteAccess(node) to filter read only operations

if (!constructor.body) return;
const { file, program, cancellationToken } = context;

const referenceEntries = mapDefined(FindAllReferences.getReferenceEntriesForNode(-1, originalName, program, [file], cancellationToken), entry => (
Copy link
Contributor

Choose a reason for hiding this comment

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

use the position of the node in the file and not -1.

if (!constructor.body) return;
const { file, program, cancellationToken } = context;

const referenceEntries = mapDefined(FindAllReferences.getReferenceEntriesForNode(constructor.pos, originalName, program, [file], cancellationToken), entry => (
Copy link
Contributor

Choose a reason for hiding this comment

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

the position should be the position of the property reference you are looking for. i am surprised this even works..

@mhegazy mhegazy merged commit 1b796ed into microsoft:master May 10, 2018
@Kingwl Kingwl deleted the readonly-getter-support branch May 10, 2018 23:23
@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
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