Skip to content

Commit 68e02e0

Browse files
committed
test: Use compiler in tests
1 parent fd6ab74 commit 68e02e0

File tree

6 files changed

+48
-42
lines changed

6 files changed

+48
-42
lines changed

.github/main.workflow

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ action "Test" {
1616
args = "test"
1717
}
1818

19-
action "Build" {
19+
action "Pre Build" {
2020
needs = "Install"
21+
uses = "docker://stedolan/jq"
22+
runs = "sh"
23+
args = "Taskfile prebuild"
24+
}
25+
26+
action "Build" {
27+
needs = "Build Options"
2128
uses = "docker://node"
2229
runs = "npm"
2330
args = "run build"

Taskfile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
#!/bin/bash
22
PATH="$PWD/node_modules/.bin":$PATH
33

4-
build() {
4+
prebuild() {
55
rm -rf dist
66
cp -rf src dist && /usr/bin/find dist -name '*.spec.ts' | xargs rm -f
77
cat tsconfig.json | jq 'del(.include, .compilerOptions.outDir)' > dist/tsconfig.json
8-
pushd dist
8+
cp README.md LICENSE package.json dist
9+
}
10+
11+
build() {
12+
cd dist
913
tsc -p .
1014
rm tsconfig.json
11-
popd
12-
cp README.md LICENSE package.json dist
15+
cd ..
1316
}
1417

1518
"$@"

src/gen-transpiled.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/index.spec.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
/* eslint-disable @typescript-eslint/tslint/config */
22
import * as lib from './index';
33
import * as ts from 'typescript';
4-
import { visitNodeAndChildren } from './visitor';
4+
5+
let transformPlugin: typeof lib.typescriptTransformUnspec = lib as any;
56

67
function transform(sourceText: string) {
7-
const sourceFile = ts.createSourceFile('filename.tsx', sourceText, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
8-
visitNodeAndChildren(sourceFile, <any>undefined, <any>undefined);
9-
return sourceFile.getText();
8+
const program = ts.createProgram({
9+
rootNames: [],
10+
options: {},
11+
});
12+
const transformer = transformPlugin(program, {});
13+
const sourceFile = ts.createSourceFile('filename.tsx', sourceText, ts.ScriptTarget.ES5, true, ts.ScriptKind.TSX);
14+
let result: string | undefined;
15+
const writeCallback = (fileName: string, data: string) => {
16+
result = String(data).trim();
17+
};
18+
program.emit(sourceFile, writeCallback, undefined, false, { before: [transformer] });
19+
return result;
1020
}
1121

1222
it('smoke test', () => {
1323
expect(lib).toBeTruthy();
14-
expect(visitNodeAndChildren).toBeTruthy();
1524
expect(transform(';')).toEqual(';');
1625
});
1726

18-
fit('should be removed it call expression', () => {
27+
it('should be removed it call expression', () => {
1928
debugger;
2029
expect(transform('it()')).toEqual('');
2130
});
22-

src/index.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
import * as ts from 'typescript';
2-
import { visitNodeAndChildren } from './visitor';
32

4-
function typescriptTransformUnspec(program: ts.Program): ts.TransformerFactory<ts.SourceFile> {
3+
export function typescriptTransformUnspec(program: ts.Program, options?): ts.TransformerFactory<ts.SourceFile> {
54
return (context) => (file) => visitNodeAndChildren(file, program, context);
65
}
76

7+
function visitNodeAndChildren(node: ts.Node, program: ts.Program, context: ts.TransformationContext): ts.SourceFile {
8+
return ts.visitEachChild(visitor(node), (childNode) => visitNodeAndChildren(childNode, program, context), context) as ts.SourceFile;
9+
}
10+
11+
function visitor(node: ts.Node): ts.Node | undefined {
12+
if (isSpecExpressionStatement(node)) {
13+
return undefined;
14+
}
15+
return node;
16+
}
17+
18+
function isSpecExpressionStatement(node: ts.Node) {
19+
return (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression)
20+
&& ts.isIdentifier(node.expression.expression)
21+
&& node.expression.expression.escapedText === 'it');
22+
}
23+
824
module.exports = typescriptTransformUnspec;
925
module.exports.default = typescriptTransformUnspec;

src/visitor.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)