@@ -5,8 +5,6 @@ const types = require('../utils/types');
5
5
6
6
const ERROR_MESSAGE_AND_OR = 'Computed property macro should be used with 2+ arguments' ;
7
7
8
- const COMPUTED_MACROS_AND_OR = [ 'and' , 'or' ] ;
9
-
10
8
module . exports = {
11
9
meta : {
12
10
type : 'problem' ,
@@ -24,22 +22,27 @@ module.exports = {
24
22
ERROR_MESSAGE_AND_OR ,
25
23
26
24
create ( context ) {
27
- let macroIdentifiersAndOr = [ ] ;
25
+ let importNameAnd = undefined ;
26
+ let importNameOr = undefined ;
27
+ let importNameReadOnly = undefined ;
28
28
29
29
return {
30
30
ImportDeclaration ( node ) {
31
- if ( node . source . value !== '@ember/object/computed' ) {
32
- return ;
31
+ if ( node . source . value === '@ember/object/computed' ) {
32
+ // Gather the identifiers that these macros are imported under.
33
+ importNameAnd =
34
+ importNameAnd || getImportIdentifier ( node , '@ember/object/computed' , 'and' ) ;
35
+ importNameOr = importNameOr || getImportIdentifier ( node , '@ember/object/computed' , 'or' ) ;
36
+ importNameReadOnly =
37
+ importNameReadOnly || getImportIdentifier ( node , '@ember/object/computed' , 'readOnly' ) ;
33
38
}
34
-
35
- // Gather the identifiers that these macros are imported under.
36
- macroIdentifiersAndOr = COMPUTED_MACROS_AND_OR . map ( ( fn ) =>
37
- getImportIdentifier ( node , '@ember/object/computed' , fn )
38
- ) ;
39
39
} ,
40
40
41
41
CallExpression ( node ) {
42
- if ( types . isIdentifier ( node . callee ) && macroIdentifiersAndOr . includes ( node . callee . name ) ) {
42
+ if (
43
+ types . isIdentifier ( node . callee ) &&
44
+ [ importNameAnd , importNameOr ] . includes ( node . callee . name )
45
+ ) {
43
46
if (
44
47
node . arguments . length === 1 &&
45
48
types . isStringLiteral ( node . arguments [ 0 ] ) &&
@@ -49,7 +52,18 @@ module.exports = {
49
52
node : node . callee ,
50
53
message : ERROR_MESSAGE_AND_OR ,
51
54
fix ( fixer ) {
52
- return fixer . replaceText ( node . callee , 'readOnly' ) ;
55
+ if ( importNameReadOnly ) {
56
+ return fixer . replaceText ( node . callee , importNameReadOnly ) ;
57
+ } else {
58
+ const sourceCode = context . getSourceCode ( ) ;
59
+ return [
60
+ fixer . insertTextBefore (
61
+ sourceCode . ast ,
62
+ "import { readOnly } from '@ember/object/computed';\n"
63
+ ) ,
64
+ fixer . replaceText ( node . callee , 'readOnly' ) ,
65
+ ] ;
66
+ }
53
67
} ,
54
68
} ) ;
55
69
} else if ( node . arguments . length === 0 ) {
0 commit comments