Skip to content

Commit

Permalink
test: add separate test files for transform mode and feature testing
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcanaltin committed Aug 12, 2024
1 parent 59d778b commit d1731df
Show file tree
Hide file tree
Showing 16 changed files with 275 additions and 46 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- uses: EmbarkStudios/cargo-deny-action@8371184bd11e21dcf8ac82ebf8c9c9f74ebf7268 # v2.0.1
- uses: EmbarkStudios/cargo-deny-action@10d8902cf9225c404574ce39c45d5d26c3047464 # v2.0.0
with:
manifest-path: "deps/swc/bindings/binding_typescript_wasm/Cargo.toml"

Expand All @@ -37,10 +37,10 @@ jobs:
- name: Checkout repository
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
- name: Initialize CodeQL
uses: github/codeql-action/init@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
uses: github/codeql-action/init@afb54ba388a7dca6ecae48f608c4ff05ff4cc77a # v3.25.15
with:
languages: ${{ matrix.language }}
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@eb055d739abdc2e8de2e5f4ba1a8b246daa779aa # v3.26.0
uses: github/codeql-action/analyze@afb54ba388a7dca6ecae48f608c4ff05ff4cc77a # v3.25.15
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@baa1b236f990293a1b2d94c19e41c2313a85e749 # v6.0.2
- uses: wagoid/commitlint-github-action@7f0a61df502599e1f1f50880aaa7ec1e2c0592f2 # v6.0.1

code-quality:
name: Code Quality
Expand Down
4 changes: 2 additions & 2 deletions deps/swc/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions deps/swc/bindings/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion deps/swc/bindings/binding_core_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "Apache-2.0"
name = "binding_core_wasm"
publish = false
repository = "https://github.com/swc-project/swc.git"
version = "1.7.10"
version = "1.7.9"

[lib]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion deps/swc/bindings/binding_minifier_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "Apache-2.0"
name = "binding_minifier_wasm"
publish = false
repository = "https://github.com/swc-project/swc.git"
version = "1.7.10"
version = "1.7.9"

[lib]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion deps/swc/bindings/binding_typescript_wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ license = "Apache-2.0"
name = "binding_typescript_wasm"
publish = false
repository = "https://github.com/swc-project/swc.git"
version = "1.7.10"
version = "1.7.9"

[lib]
bench = false
Expand Down
5 changes: 2 additions & 3 deletions deps/swc/bindings/binding_typescript_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ use wasm_bindgen_futures::{
/// auto generated one, which is not reflecting most of types in detail.
#[wasm_bindgen(typescript_custom_section)]
const INTERFACE_DEFINITIONS: &'static str = r#"
export declare function transform(src: string, opts?: Options): Promise<TransformOutput>;
export declare function transformSync(src: string, opts?: Options): TransformOutput;
export type { Options, TransformOutput };
export function transform(src: string, opts?: Options): Promise<TransformOutput>;
export function transformSync(src: string, opts?: Options): TransformOutput;
"#;

#[wasm_bindgen(skip_typescript)]
Expand Down
2 changes: 1 addition & 1 deletion deps/swc/bindings/swc_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include = ["Cargo.toml", "src/**/*.rs"]
license = "Apache-2.0"
name = "swc_cli"
repository = "https://github.com/swc-project/swc.git"
version = "0.91.366"
version = "0.91.364"

[[bin]]
bench = false
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"강동윤 <[email protected]>"
],
"description": "wasm module for swc",
"version": "1.7.10",
"version": "1.7.9",
"license": "Apache-2.0",
"repository": {
"type": "git",
Expand Down
5 changes: 2 additions & 3 deletions lib/wasm.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* tslint:disable */
/* eslint-disable */

export declare function transform(src: string, opts?: Options): Promise<TransformOutput>;
export declare function transformSync(src: string, opts?: Options): TransformOutput;
export type { Options, TransformOutput };
export function transform(src: string, opts?: Options): Promise<TransformOutput>;
export function transformSync(src: string, opts?: Options): TransformOutput;



Expand Down
50 changes: 25 additions & 25 deletions lib/wasm.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions test/snapshots/transform-decorator-namespace.test.js.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports[`should transform TypeScript class decorators with multiple decorators 1`] = `
"@sealed\\n@log\\nclass BugReport {\\n type = \\"report\\";\\n title;\\n constructor(t){\\n this.title = t;\\n }\\n}\\nfunction sealed(constructor) {\\n Object.seal(constructor);\\n Object.seal(constructor.prototype);\\n}\\nfunction log(constructor) {\\n console.log(\\"Creating instance of\\", constructor.name);\\n}\\nconst report = new BugReport(\\"Test\\");\\n"
`;

exports[`should transform TypeScript namespaces with additional functionality 1`] = `
"var Validation;\\n(function(Validation) {\\n const lettersRegexp = /^[A-Za-z]+$/;\\n class LettersOnlyValidator {\\n isAcceptable(s) {\\n return lettersRegexp.test(s);\\n }\\n static createValidator() {\\n return new LettersOnlyValidator();\\n }\\n }\\n Validation.LettersOnlyValidator = LettersOnlyValidator;\\n})(Validation || (Validation = {}));\\nconst validator = Validation.LettersOnlyValidator.createValidator();\\nconst isValid = validator.isAcceptable(\\"test\\");\\n// Exporting these for VM context\\nglobalThis.validator = validator;\\nglobalThis.isValid = isValid;\\n"
`;
11 changes: 11 additions & 0 deletions test/snapshots/transform-feature.test.js.snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
exports[`should transform TypeScript class fields 1`] = `
"class Counter {\\n count = 0;\\n increment() {\\n this.count++;\\n }\\n}\\n"
`;

exports[`should transform TypeScript private class fields 1`] = `
"class Counter {\\n #count = 0;\\n increment() {\\n this.#count++;\\n }\\n getCount() {\\n return this.#count;\\n }\\n}\\n"
`;

exports[`should transform TypeScript type annotations and type guards 1`] = `
"function isString(value) {\\n return typeof value === 'string';\\n}\\n"
`;
106 changes: 106 additions & 0 deletions test/transform-decorator-namespace.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const { test, snapshot } = require("node:test");
const assert = require("node:assert");
const path = require("node:path");
const vm = require("node:vm");
const { transformSync } = require("../dist/index.js");

snapshot.setResolveSnapshotPath((testPath) => {
return path.join(
__dirname,
"snapshots",
`${path.basename(testPath)}.snapshot`,
);
});

test("should transform TypeScript class decorators with multiple decorators", (t) => {
const inputCode = `
@sealed
@log
class BugReport {
type = "report";
title: string;
constructor(t: string) {
this.title = t;
}
}
function sealed(constructor: Function) {
Object.seal(constructor);
Object.seal(constructor.prototype);
}
function log(constructor: Function) {
console.log("Creating instance of", constructor.name);
}
const report = new BugReport("Test");
`;

const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});

t.assert.snapshot(code);

try {
const script = new vm.Script(code);
const context = vm.createContext({});
context.report = null;
script.runInContext(context);

assert.ok(context.report, "Report instance should exist");
assert.strictEqual(
context.report.type,
"report",
"Report type should be 'report'",
);
assert.strictEqual(
context.report.title,
"Test",
"Report title should be 'Test'",
);
} catch (err) {
console.error("Error executing script:", err);
}
});

test("should transform TypeScript namespaces with additional functionality", (t) => {
const inputCode = `
namespace Validation {
export interface StringValidator {
isAcceptable(s: string): boolean;
}
const lettersRegexp = /^[A-Za-z]+$/;
export class LettersOnlyValidator implements StringValidator {
isAcceptable(s: string) {
return lettersRegexp.test(s);
}
static createValidator(): LettersOnlyValidator {
return new LettersOnlyValidator();
}
}
}
const validator = Validation.LettersOnlyValidator.createValidator();
const isValid = validator.isAcceptable("test");
// Exporting these for VM context
(globalThis as any).validator = validator;
(globalThis as any).isValid = isValid;
`;

const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});

t.assert.snapshot(code);

const script = new vm.Script(code);
const context = vm.createContext({});
script.runInContext(context);

assert.ok(context.validator, "Validator instance should exist");
assert.strictEqual(context.isValid, true, "String should be valid");
});
107 changes: 107 additions & 0 deletions test/transform-feature.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
const { test, snapshot } = require("node:test");
const { transformSync } = require("../dist/index.js");
const assert = require("node:assert");
const path = require("node:path");
const vm = require("node:vm");

snapshot.setResolveSnapshotPath((testPath) => {
return path.join(
__dirname,
"snapshots",
`${path.basename(testPath)}.snapshot`,
);
});

test("should transform TypeScript class fields", (t) => {
const inputCode = `
class Counter {
count: number = 0;
increment() {
this.count++;
}
}
`;
const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});
t.assert.snapshot(code);

const context = { result: null };
vm.createContext(context);
vm.runInContext(
`
${code}
const counter = new Counter();
counter.increment();
result = counter.count;
`,
context,
);
assert.strictEqual(context.result, 1, "Counter should increment to 1");
});

test("should transform TypeScript private class fields", (t) => {
const inputCode = `
class Counter {
#count: number = 0;
increment() {
this.#count++;
}
getCount(): number {
return this.#count;
}
}
`;
const { code } = transformSync(inputCode, {
mode: "transform",
sourceMap: true,
});
t.assert.snapshot(code);

const context = { result: null };
vm.createContext(context);
vm.runInContext(
`
${code}
const counter = new Counter();
counter.increment();
result = counter.getCount();
`,
context,
);
assert.strictEqual(
context.result,
1,
"Counter private field should increment to 1",
);
});

test("should transform TypeScript type annotations and type guards", (t) => {
const inputCode = `
function isString(value: unknown): value is string {
return typeof value === 'string';
}
`;
const { code } = transformSync(inputCode, {
mode: "transform",
sourceMaps: true,
});
t.assert.snapshot(code);

const context = { result: null };
vm.createContext(context);
vm.runInContext(
`
${code}
const check = isString("hello");
result = check;
`,
context,
);
assert.strictEqual(
context.result,
true,
"Should recognize 'hello' as a string",
);
});

0 comments on commit d1731df

Please sign in to comment.