@@ -5,7 +5,8 @@ const debug = require('debug')('ember-no-implicit-this-codemod:transform');
5
5
const recast = require ( 'ember-template-recast' ) ;
6
6
const { getTelemetry } = require ( 'ember-codemods-telemetry-helpers' ) ;
7
7
const transform = require ( './helpers/plugin' ) ;
8
- const { getOptions : getCLIOptions } = require ( 'codemod-cli' ) ;
8
+ const { getOptions : getCLIOptions , jscodeshift } = require ( 'codemod-cli' ) ;
9
+ const { isEmberTemplate } = require ( './helpers/tagged-templates' ) ;
9
10
const DEFAULT_OPTIONS = { } ;
10
11
11
12
/**
@@ -41,23 +42,48 @@ function getOptions() {
41
42
return options ;
42
43
}
43
44
44
- module . exports = function transformer ( file /*, api */ ) {
45
- let extension = path . extname ( file . path ) ;
46
- let options = Object . assign ( { } , DEFAULT_OPTIONS , getOptions ( ) ) ;
47
-
48
- if ( ! [ '.hbs' ] . includes ( extension . toLowerCase ( ) ) ) {
49
- debug ( 'Skipping %s because it does not match the .hbs file extension' , file . path ) ;
50
-
51
- // do nothing on non-hbs files
52
- return ;
53
- }
54
-
55
- debug ( 'Parsing %s ...' , file . path ) ;
56
- let root = recast . parse ( file . source ) ;
45
+ /**
46
+ * Given the location and source text of a template, as well as codemod options,
47
+ * returns the rewritten template contents with `this` references inserted where
48
+ * necessary.
49
+ */
50
+ function rewriteTemplate ( path , source , options ) {
51
+ debug ( 'Parsing %s ...' , path ) ;
52
+ let root = recast . parse ( source ) ;
57
53
58
- debug ( 'Transforming %s ...' , file . path ) ;
54
+ debug ( 'Transforming %s ...' , path ) ;
59
55
transform ( root , options ) ;
60
56
61
- debug ( 'Generating new content for %s ...' , file . path ) ;
57
+ debug ( 'Generating new content for %s ...' , path ) ;
62
58
return recast . print ( root ) ;
59
+ }
60
+
61
+ /**
62
+ * Given a JS or TS file that potentially has embedded templates within it,
63
+ * returns updated source with those templates rewritten to include `this`
64
+ * references where needed.
65
+ */
66
+ function rewriteEmbeddedTemplates ( file , options , api ) {
67
+ return jscodeshift
68
+ . getParser ( api ) ( file . source )
69
+ . find ( 'TaggedTemplateExpression' , { tag : { type : 'Identifier' } } )
70
+ . forEach ( path => {
71
+ if ( isEmberTemplate ( path ) ) {
72
+ let { value } = path . node . quasi . quasis [ 0 ] ;
73
+ value . raw = rewriteTemplate ( file . path , value . raw , options ) ;
74
+ }
75
+ } )
76
+ . toSource ( ) ;
77
+ }
78
+
79
+ module . exports = function transformer ( file , api ) {
80
+ let extension = path . extname ( file . path ) . toLowerCase ( ) ;
81
+ let options = Object . assign ( { } , DEFAULT_OPTIONS , getOptions ( ) ) ;
82
+ if ( extension === '.hbs' ) {
83
+ return rewriteTemplate ( file . path , file . source , options ) ;
84
+ } else if ( extension === '.js' || extension === '.ts' ) {
85
+ return rewriteEmbeddedTemplates ( file , options , api ) ;
86
+ } else {
87
+ debug ( 'Skipping %s because it does not match a known extension with templates' , file . path ) ;
88
+ }
63
89
} ;
0 commit comments