-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2231 from embroider-build/codemod-render-tests
template-tag-codemod: add support for rendering tests
- Loading branch information
Showing
7 changed files
with
335 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/template-tag-codemod/src/identify-render-tests.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { type NodePath, parseAsync, traverse, type types } from '@babel/core'; | ||
import { createRequire } from 'module'; | ||
import codeFrame from '@babel/code-frame'; | ||
|
||
const require = createRequire(import.meta.url); | ||
const { codeFrameColumns } = codeFrame; | ||
|
||
export interface RenderTest { | ||
node: types.Node; | ||
startIndex: number; | ||
endIndex: number; | ||
templateContent: string; | ||
statementStart: number; | ||
availableBinding: string; | ||
} | ||
|
||
export async function identifyRenderTests( | ||
source: string, | ||
filename: string | ||
): Promise<{ renderTests: RenderTest[]; parsed: types.File }> { | ||
let renderTests: RenderTest[] = []; | ||
let parsed = await parseAsync(source, { | ||
filename, | ||
plugins: [ | ||
[require.resolve('@babel/plugin-syntax-decorators'), { legacy: true }], | ||
require.resolve('@babel/plugin-syntax-typescript'), | ||
], | ||
}); | ||
|
||
if (!parsed) { | ||
throw new Error(`bug, unexpected output from babel parseAsync`); | ||
} | ||
|
||
function fail(node: types.Node, message: string) { | ||
let m = `[${filename}] ${message}`; | ||
if (node.loc) { | ||
m = m + '\n' + codeFrameColumns(source, node.loc); | ||
} | ||
return new Error(m); | ||
} | ||
|
||
traverse(parsed, { | ||
CallExpression(path) { | ||
if (path.get('callee').referencesImport('@ember/test-helpers', 'render')) { | ||
let [arg0] = path.get('arguments'); | ||
if (arg0.isTaggedTemplateExpression()) { | ||
let tag = arg0.get('tag'); | ||
if (isLooseHBS(tag)) { | ||
let loc = arg0.node.loc; | ||
if (!loc) { | ||
throw new Error(`bug: no locations provided by babel`); | ||
} | ||
|
||
let counter = 0; | ||
let availableBinding = 'self'; | ||
while (path.scope.getBinding(availableBinding)) { | ||
availableBinding = `self${counter++}`; | ||
} | ||
|
||
let statementCandidate: NodePath<unknown> = path; | ||
while (!statementCandidate.isStatement()) { | ||
statementCandidate = statementCandidate.parentPath; | ||
} | ||
|
||
renderTests.push({ | ||
node: arg0.node, | ||
startIndex: loc.start.index, | ||
endIndex: loc.end.index, | ||
templateContent: arg0.node.quasi.quasis[0].value.raw, | ||
statementStart: statementCandidate.node.loc!.start.index, | ||
availableBinding, | ||
}); | ||
} | ||
} else { | ||
throw fail(arg0.node, `unsupported syntax in rendering test (${arg0.type})`); | ||
} | ||
} | ||
}, | ||
}); | ||
return { renderTests, parsed }; | ||
} | ||
|
||
function isLooseHBS(path: NodePath<types.Expression>) { | ||
if (path.isReferencedIdentifier()) { | ||
if (path.referencesImport('ember-cli-htmlbars', 'hbs')) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
Oops, something went wrong.