Skip to content

Commit 8995e7a

Browse files
committed
Extracted example
1 parent ba7337f commit 8995e7a

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

src/example.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function hello(greet = 'world') {
2+
return `hello ${greet}`;
3+
}
4+
5+
it('hello world test', () => {
6+
expect(hello()).toBe('hello world');
7+
});
8+
9+
describe('hello world test', () => {
10+
jest.mock('fs');
11+
});

src/index.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
11
/* eslint-disable @typescript-eslint/tslint/config */
22
import * as lib from './index';
3+
import * as ts from 'typescript';
34

4-
it('smoke', () => {
5+
type CreateFixtureArguments = {
6+
sourceText: string;
7+
};
8+
9+
function createFixture(createFixtureArguments: CreateFixtureArguments) {
10+
const {
11+
sourceText
12+
} = createFixtureArguments;
13+
ts.createSourceFile('filename.tsx', sourceText, ts.ScriptTarget.Latest);
14+
}
15+
16+
it('smoke test', () => {
517
expect(lib).toBeTruthy();
618
});
19+

src/index.ts

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import * as ts from 'typescript';
22

3-
function typescriptTransformUnspec<T extends ts.Node>(program: ts.Program): ts.TransformerFactory<T> {
4-
return (context: ts.TransformationContext) => {
5-
const visit: ts.Visitor = (node) => {
6-
if (isSpecExpressionStatement(node)) {
7-
return undefined;
8-
}
9-
return ts.visitEachChild(node, (child) => visit(child), context);
10-
};
11-
return (node) => ts.visitNode(node, visit);
12-
};
3+
function typescriptTransformUnspec(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
4+
return (context) => (file) => visitNodeAndChildren(file, program, context);
5+
}
6+
7+
function visitNodeAndChildren(node: ts.Node, program: ts.Program, context: ts.TransformationContext): ts.SourceFile {
8+
return ts.visitEachChild(visitNode(node), (childNode) => visitNodeAndChildren(childNode, program, context), context) as ts.SourceFile;
9+
}
10+
11+
function visitNode(node: ts.Node): ts.Node | undefined {
12+
if (isSpecExpressionStatement(node)) {
13+
return undefined;
14+
}
15+
return node;
1316
}
1417

1518
function isSpecExpressionStatement(node: ts.Node) {
@@ -18,13 +21,5 @@ function isSpecExpressionStatement(node: ts.Node) {
1821
&& node.expression.expression.escapedText === 'it');
1922
}
2023

21-
if (typeof it !== 'function') {
22-
// @ts-ignore
23-
var it: any = () => { };
24-
}
25-
it('smoke', () => {
26-
expect(typescriptTransformUnspec).toBeTruthy();
27-
});
28-
2924
module.exports = typescriptTransformUnspec;
3025
module.exports.default = typescriptTransformUnspec;

webpack.config.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
const unspecTransformer = require('./src/index');
22

33
module.exports = async (options) => ({
4-
externals: {
5-
typescript: 'typescript',
6-
},
74
entry: {
8-
lib: `${__dirname}/src/index.ts`,
5+
example: `${__dirname}/src/example.ts`,
96
},
107
output: {
118
path: `${__dirname}/dist`,

0 commit comments

Comments
 (0)