@@ -5,7 +5,8 @@ const debug = require('debug')('ember-no-implicit-this-codemod:transform');
55const recast = require ( 'ember-template-recast' ) ;
66const { getTelemetry } = require ( 'ember-codemods-telemetry-helpers' ) ;
77const 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' ) ;
910const DEFAULT_OPTIONS = { } ;
1011
1112/**
@@ -41,23 +42,48 @@ function getOptions() {
4142 return options ;
4243}
4344
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 ) ;
5753
58- debug ( 'Transforming %s ...' , file . path ) ;
54+ debug ( 'Transforming %s ...' , path ) ;
5955 transform ( root , options ) ;
6056
61- debug ( 'Generating new content for %s ...' , file . path ) ;
57+ debug ( 'Generating new content for %s ...' , path ) ;
6258 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+ }
6389} ;
0 commit comments