Skip to content

Commit

Permalink
Merge pull request #2134 from embroider-build/template-tag-codemod-na…
Browse files Browse the repository at this point in the history
…mehint

Template tag codemod namehint
  • Loading branch information
ef4 authored Oct 8, 2024
2 parents 74d2521 + 75dafd0 commit 8850c96
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
14 changes: 10 additions & 4 deletions packages/compat/src/resolver-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ export interface CompatResolverOptions extends CoreResolverOptions {
options: UserConfig;
}

export interface ExternalNameHint {
(path: string): string | null;
}

export interface Options {
appRoot: string;
emberVersion: string;
externalNameHint?: ExternalNameHint;
}

type BuiltIn = {
Expand Down Expand Up @@ -178,7 +183,8 @@ class TemplateResolver implements ASTPlugin {
constructor(
private env: Env,
private config: CompatResolverOptions,
private builtInsForEmberVersion: ReturnType<typeof builtInKeywords>
private builtInsForEmberVersion: ReturnType<typeof builtInKeywords>,
private externalNameHint?: ExternalNameHint
) {
this.moduleResolver = new Resolver(config);
if ((globalThis as any).embroider_audit) {
Expand Down Expand Up @@ -728,7 +734,7 @@ class TemplateResolver implements ASTPlugin {

// the extra underscore here guarantees that we will never collide with an
// HTML element.
return parts[parts.length - 1] + '_';
return this.externalNameHint?.(path) ?? parts[parts.length - 1] + '_';
}

private handleDynamicModifier(param: ASTv1.Expression): ModifierResolution | ResolutionFail | null {
Expand Down Expand Up @@ -971,7 +977,7 @@ class TemplateResolver implements ASTPlugin {
}

// This is the AST transform that resolves components, helpers and modifiers at build time
export default function makeResolverTransform({ appRoot, emberVersion }: Options) {
export default function makeResolverTransform({ appRoot, emberVersion, externalNameHint }: Options) {
let config: CompatResolverOptions = readJSONSync(join(locateEmbroiderWorkingDir(appRoot), 'resolver.json'));
const resolverTransform: ASTPluginBuilder<Env> = env => {
if (env.strictMode) {
Expand All @@ -980,7 +986,7 @@ export default function makeResolverTransform({ appRoot, emberVersion }: Options
visitor: {},
};
}
return new TemplateResolver(env, config, builtInKeywords(emberVersion));
return new TemplateResolver(env, config, builtInKeywords(emberVersion), externalNameHint);
};
(resolverTransform as any).parallelBabel = {
requireFile: __filename,
Expand Down
29 changes: 25 additions & 4 deletions packages/compat/src/template-tag-codemod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,34 @@ import { statSync, readdirSync, readFileSync, writeFileSync } from 'fs';
import Plugin from 'broccoli-plugin';
import { transformSync } from '@babel/core';
import { hbsToJS, ResolverLoader } from '@embroider/core';
import ResolverTransform from './resolver-transform';
import ResolverTransform, { type ExternalNameHint } from './resolver-transform';
import { spawn } from 'child_process';
import { locateEmbroiderWorkingDir } from '@embroider/core';

export interface TemplateTagCodemodOptions {
shouldTransformPath: (outputPath: string) => boolean;
nameHint: ExternalNameHint;
dryRun: boolean;
}

export default function templateTagCodemod(
emberApp: EmberAppInstance,
{ shouldTransformPath = (() => true) as TemplateTagCodemodOptions['shouldTransformPath'], dryRun = false } = {}
{
shouldTransformPath = (() => true) as TemplateTagCodemodOptions['shouldTransformPath'],
nameHint = (path => {
return path
.split('/')
.map(part =>
part
.split('-')
// capitalize first letter
.map(inner_part => inner_part.charAt(0).toUpperCase() + inner_part.slice(1))
.join('')
)
.join('_');
}) as TemplateTagCodemodOptions['nameHint'],
dryRun = false,
} = {}
): Node {
return new TemplateTagCodemodPlugin(
[
Expand All @@ -35,7 +51,7 @@ export default function templateTagCodemod(
},
}),
],
{ shouldTransformPath, dryRun }
{ shouldTransformPath, nameHint, dryRun }
);
}

Expand Down Expand Up @@ -89,6 +105,11 @@ class TemplateTagCodemodPlugin extends Plugin {
const babel_plugin_syntax_typescript = require.resolve('@babel/plugin-syntax-typescript', {
paths: [embroider_compat_path],
});
const resolver_transform = ResolverTransform({
appRoot: process.cwd(),
emberVersion: emberVersion,
externalNameHint: this.options.nameHint,
});

for await (const current_file of walkSync(tmp_path)) {
if (hbs_file_test.test(current_file) && this.options.shouldTransformPath(current_file)) {
Expand All @@ -103,7 +124,7 @@ class TemplateTagCodemodPlugin extends Plugin {
babel_plugin_ember_template_compilation,
{
compilerPath: ember_template_compiler.filename,
transforms: [ResolverTransform({ appRoot: process.cwd(), emberVersion: emberVersion })],
transforms: [resolver_transform],
targetFormat: 'hbs',
},
],
Expand Down

0 comments on commit 8850c96

Please sign in to comment.