From 06bcf8e8fe0a379147553e58f5cdf8b395034fad Mon Sep 17 00:00:00 2001 From: "C. Fuhrman" Date: Thu, 18 Jul 2024 15:47:49 -0400 Subject: [PATCH] readOnly for Property --- .../FamixTypeScriptProperty.class.st | 19 +++++++++++++++++++ .../FamixTypeScriptGenerator.class.st | 2 ++ .../FamixTypeScriptPropertyTest.class.st | 10 ++++++++++ 3 files changed, 31 insertions(+) diff --git a/src/Famix-TypeScript-Entities/FamixTypeScriptProperty.class.st b/src/Famix-TypeScript-Entities/FamixTypeScriptProperty.class.st index 494c71d..4b81b48 100644 --- a/src/Famix-TypeScript-Entities/FamixTypeScriptProperty.class.st +++ b/src/Famix-TypeScript-Entities/FamixTypeScriptProperty.class.st @@ -29,6 +29,7 @@ I represent a TypeScript class property. | `isClassSide` | `Boolean` | false | Entity can be declared class side i.e. static| | `isStub` | `Boolean` | false | Flag true if the entity attributes are incomplete, either because the entity is missing or not imported.| | `name` | `String` | nil | Basic name of the entity, not full reference.| +| `readOnly` | `Boolean` | false | Properties may be prefixed with the readonly modifier. This prevents assignments to the field outside of the constructor.| | `visibility` | `String` | nil | Visibility of the entity| " @@ -37,6 +38,9 @@ Class { #superclass : #FamixTypeScriptStructuralEntity, #traits : 'FamixTAttribute + FamixTCanBeClassSide + FamixTHasVisibility', #classTraits : 'FamixTAttribute classTrait + FamixTCanBeClassSide classTrait + FamixTHasVisibility classTrait', + #instVars : [ + '#readOnly => FMProperty defaultValue: false' + ], #category : #'Famix-TypeScript-Entities-Entities' } @@ -48,3 +52,18 @@ FamixTypeScriptProperty class >> annotation [ ^ self ] + +{ #category : #accessing } +FamixTypeScriptProperty >> readOnly [ + + + + + ^ readOnly ifNil: [ readOnly := false ] +] + +{ #category : #accessing } +FamixTypeScriptProperty >> readOnly: anObject [ + + readOnly := anObject +] diff --git a/src/Famix-TypeScript-Generator/FamixTypeScriptGenerator.class.st b/src/Famix-TypeScript-Generator/FamixTypeScriptGenerator.class.st index 5b010b6..0d65809 100644 --- a/src/Famix-TypeScript-Generator/FamixTypeScriptGenerator.class.st +++ b/src/Famix-TypeScript-Generator/FamixTypeScriptGenerator.class.st @@ -421,6 +421,8 @@ FamixTypeScriptGenerator >> defineProperties [ (decorator property: #expression type: #String) comment: 'Decorators use the form @expression, where expression must evaluate to a function that will be called at runtime with information about the decorated declaration'. + (property property: #readOnly type: #Boolean defaultValue: false) + comment: 'Properties may be prefixed with the readonly modifier. This prevents assignments to the field outside of the constructor.' "The following are not need because of THasKind (?)" "(method property: #isConstructor type: #Boolean defaultValue: false) diff --git a/src/Famix-TypeScript-Tests/FamixTypeScriptPropertyTest.class.st b/src/Famix-TypeScript-Tests/FamixTypeScriptPropertyTest.class.st index 22d0f94..4e90b43 100644 --- a/src/Famix-TypeScript-Tests/FamixTypeScriptPropertyTest.class.st +++ b/src/Famix-TypeScript-Tests/FamixTypeScriptPropertyTest.class.st @@ -4,6 +4,16 @@ Class { #category : #'Famix-TypeScript-Tests-Entities' } +{ #category : #tests } +FamixTypeScriptPropertyTest >> testPropertyReadonly [ + | property | + property := FamixTypeScriptProperty new. + self deny: property readOnly. + property readOnly: true. + self assert: property readOnly. + +] + { #category : #tests } FamixTypeScriptPropertyTest >> testPropertyVisibility [ | property |