-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
72 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export type { types as astTypes } from 'recast'; | ||
export { visit as astVisit } from 'recast'; | ||
export { builders as astBuilders, type namedTypes as astNamedTypes } from 'ast-types'; | ||
|
||
import type { types } from 'recast'; | ||
import type { namedTypes } from 'ast-types'; | ||
|
||
export type ASTAfterHook = ( | ||
ast: namedTypes.ExpressionStatement, | ||
dataNode: namedTypes.ThisExpression | namedTypes.Identifier, | ||
) => void; | ||
|
||
export type ASTBeforeHook = ( | ||
ast: types.namedTypes.File, | ||
dataNode: namedTypes.ThisExpression | namedTypes.Identifier, | ||
) => void; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { ExpressionStatement } from 'esprima-next'; | ||
import { type ASTAfterHook, type ASTBeforeHook, Tournament } from '../src'; | ||
|
||
describe('AST Hooks', () => { | ||
test('Before hooks are called before variable expansion', () => { | ||
const hook: ASTBeforeHook = (ast) => { | ||
expect(ast.program.body).toHaveLength(1); | ||
expect(ast.program.body[0].type).toBe('ExpressionStatement'); | ||
expect((ast.program.body[0] as ExpressionStatement).expression.type).toBe('Identifier'); | ||
}; | ||
|
||
const t = new Tournament(() => {}, undefined, undefined, { before: [hook], after: [] }); | ||
expect(t.execute('{{ test }}', { test: 1 })).toBe(1); | ||
}); | ||
|
||
test('After hooks are called after variable expansion', () => { | ||
const hook: ASTAfterHook = (ast) => { | ||
expect(ast.type).toBe('ExpressionStatement'); | ||
expect((ast as ExpressionStatement).expression.type).toBe('MemberExpression'); | ||
}; | ||
|
||
const t = new Tournament(() => {}, undefined, undefined, { before: [], after: [hook] }); | ||
expect(t.execute('{{ test }}', { test: 1 })).toBe(1); | ||
}); | ||
}); |