@@ -11,6 +11,46 @@ const DEFAULT_OPTIONS = {
1111 alwaysPrefix : 'true' ,
1212} ;
1313
14+ /**
15+ * Return true if the computed property is readOnly.
16+ * @param {* } nodeItem
17+ */
18+ function _isReadOnlyComputedProperty ( nodeItem ) {
19+ return (
20+ _isComputedProperty ( nodeItem ) &&
21+ nodeItem . expression . callee . property &&
22+ nodeItem . expression . callee . property . name === 'readOnly'
23+ ) ;
24+ }
25+
26+ /**
27+ * Return true if the nodeItem is a computed property. It could either
28+ * be a regular or readOnly computed property.
29+ * @param {* } nodeItem
30+ */
31+ function _isComputedProperty ( nodeItem ) {
32+ return (
33+ nodeItem . expression . callee &&
34+ ( nodeItem . expression . callee . name === 'computed' ||
35+ ( nodeItem . expression . callee . object &&
36+ nodeItem . expression . callee . object . callee . name === 'computed' ) )
37+ ) ;
38+ }
39+
40+ /**
41+ * If the nodeItem is a computed property, then return an array of argument values.
42+ * @param {* } nodeItem
43+ */
44+ function _getArgValues ( nodeItem ) {
45+ if ( _isComputedProperty ( nodeItem ) ) {
46+ const nodeArguments = _isReadOnlyComputedProperty ( nodeItem )
47+ ? nodeItem . expression . callee . object . arguments
48+ : nodeItem . expression . arguments ;
49+
50+ return nodeArguments . map ( item => item . value ) ;
51+ }
52+ }
53+
1454module . exports = function transformer ( file , api ) {
1555 const configOptions = Object . assign ( { } , DEFAULT_OPTIONS , getOptions ( ) ) ;
1656 const classProps = [ ] ;
@@ -24,8 +64,15 @@ module.exports = function transformer(file, api) {
2464 . forEach ( path => {
2565 path . node . body . forEach ( classItem => {
2666 // Collect all the class properties in the file and add it to the
27- // classProps array.
28- if ( classItem . type === 'ClassProperty' && ! classItem . decorators ) {
67+ // classProps array. If there is a decorator associated with a class
68+ // property, then only add it to the array if it is a @tracked property.
69+ if (
70+ classItem . type === 'ClassProperty' &&
71+ ( ! classItem . decorators ||
72+ classItem . decorators . every (
73+ item => item . expression . name === 'tracked'
74+ ) )
75+ ) {
2976 classProps . push ( classItem . key . name ) ;
3077 }
3178 // Collect all the dependent keys of the computed properties present in the file
@@ -36,13 +83,8 @@ module.exports = function transformer(file, api) {
3683 classItem . decorators
3784 ) {
3885 classItem . decorators . forEach ( decoratorItem => {
39- if (
40- decoratorItem . expression . callee &&
41- decoratorItem . expression . callee . name === 'computed'
42- ) {
43- const argValues = decoratorItem . expression . arguments . map (
44- item => item . value
45- ) ;
86+ const argValues = _getArgValues ( decoratorItem ) ;
87+ if ( argValues ) {
4688 computedPropsMap [ classItem . key . name ] = argValues ;
4789 computedProps = computedProps . concat ( argValues ) ;
4890 }
@@ -63,7 +105,7 @@ module.exports = function transformer(file, api) {
63105 if ( ! path . node . decorators && computedProps . includes ( path . node . key . name ) ) {
64106 shouldImportBeAdded = true ;
65107 const trackedDecorator = buildTrackedDecorator ( path . node . key . name , j ) ;
66-
108+
67109 // @TODO : Determine if @tracked can be prefixed alongside other decorators in a property,
68110 // if yes, then change this code to push the trackedDecorator along with the
69111 // others.
@@ -86,20 +128,28 @@ module.exports = function transformer(file, api) {
86128 . filter ( path => {
87129 return (
88130 path . node . expression . type === 'CallExpression' &&
89- path . node . expression . callee &&
90- path . node . expression . callee . name === 'computed'
131+ _isComputedProperty ( path . node )
91132 ) ;
92133 } )
93134 . forEach ( path => {
135+ const isReadOnlyProperty = _isReadOnlyComputedProperty ( path . node ) ;
136+ const computedPropArguments = isReadOnlyProperty
137+ ? path . node . expression . callee . object . arguments
138+ : path . node . expression . arguments ;
139+
94140 const dependentKeys = getDependentKeys (
95- path . node . expression . arguments ,
141+ computedPropArguments ,
96142 computedPropsMap ,
97143 classProps
98144 ) ;
99145 if ( ! dependentKeys . length ) {
100146 path . replace ( ) ;
101147 } else {
102- path . node . expression . arguments = dependentKeys ;
148+ if ( isReadOnlyProperty ) {
149+ path . node . expression . callee . object . arguments = dependentKeys ;
150+ } else {
151+ path . node . expression . arguments = dependentKeys ;
152+ }
103153 }
104154 } ) ;
105155
0 commit comments