Skip to content

Commit 33bc417

Browse files
committed
adopt cleaner TS 6.0 patterns from upstream
1 parent 3ffca38 commit 33bc417

File tree

14 files changed

+79
-70
lines changed

14 files changed

+79
-70
lines changed

src/transpilation/resolve.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { couldNotReadDependency, couldNotResolveRequire } from "./diagnostics";
1010
import { BuildMode, CompilerOptions } from "../CompilerOptions";
1111
import { findLuaRequires, LuaRequire } from "./find-lua-requires";
1212
import { Plugin } from "./plugins";
13-
import picomatch from "picomatch";
13+
import * as picomatch from "picomatch";
1414

1515
const resolver = resolve.ResolverFactory.createResolver({
1616
extensions: [".lua"],
@@ -27,7 +27,7 @@ interface ResolutionResult {
2727
}
2828

2929
class ResolutionContext {
30-
private noResolvePaths: picomatch.Matcher[];
30+
private noResolvePaths: string[];
3131

3232
public diagnostics: ts.Diagnostic[] = [];
3333
public resolvedFiles = new Map<string, ProcessedFile>();
@@ -38,9 +38,7 @@ class ResolutionContext {
3838
private readonly emitHost: EmitHost,
3939
private readonly plugins: Plugin[]
4040
) {
41-
const unique = [...new Set(options.noResolvePaths)];
42-
const matchers = unique.map(x => picomatch(x));
43-
this.noResolvePaths = matchers;
41+
this.noResolvePaths = [...new Set(options.noResolvePaths)];
4442
}
4543

4644
public addAndResolveDependencies(file: ProcessedFile): void {
@@ -73,7 +71,7 @@ class ResolutionContext {
7371
return;
7472
}
7573

76-
if (this.noResolvePaths.find(isMatch => isMatch(required.requirePath))) {
74+
if (this.noResolvePaths.find(pattern => picomatch.isMatch(required.requirePath, pattern))) {
7775
if (this.options.tstlVerbose) {
7876
console.log(
7977
`Skipping module resolution of ${required.requirePath} as it is in the tsconfig noResolvePaths.`
@@ -218,14 +216,11 @@ class ResolutionContext {
218216
if (this.options.paths) {
219217
// If no file found yet and paths are present, try to find project file via paths mappings
220218
// When baseUrl is not set, resolve paths relative to the tsconfig directory (TS 6.0+ behavior)
221-
const pathsBase = this.options.baseUrl
222-
?? (this.options.configFilePath ? path.dirname(this.options.configFilePath) : undefined);
219+
const pathsBase =
220+
this.options.baseUrl ??
221+
(this.options.configFilePath ? path.dirname(this.options.configFilePath) : undefined);
223222
if (pathsBase) {
224-
const fileFromPaths = this.tryGetModuleNameFromPaths(
225-
dependencyPath,
226-
this.options.paths,
227-
pathsBase
228-
);
223+
const fileFromPaths = this.tryGetModuleNameFromPaths(dependencyPath, this.options.paths, pathsBase);
229224
if (fileFromPaths) return fileFromPaths;
230225
}
231226
}

src/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as ts from "typescript";
2-
import nativeAssert from "assert";
2+
import * as nativeAssert from "assert";
33
import * as path from "path";
44

55
export function castArray<T>(value: T | T[]): T[];
@@ -77,7 +77,7 @@ export function cast<TOriginal, TCast extends TOriginal>(
7777
}
7878

7979
export function assert(value: any, message?: string | Error): asserts value {
80-
nativeAssert(value, message);
80+
nativeAssert.ok(value, message);
8181
}
8282

8383
export function assertNever(_value: never): never {

test/setup.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "assert";
1+
import * as assert from "assert";
22
import * as ts from "typescript";
33
import * as tstl from "../src";
44

@@ -13,7 +13,7 @@ declare global {
1313

1414
expect.extend({
1515
toHaveDiagnostics(diagnostics: ts.Diagnostic[], expected?: number[]): jest.CustomMatcherResult {
16-
assert(Array.isArray(diagnostics));
16+
assert.ok(Array.isArray(diagnostics));
1717
// @ts-ignore
1818
const matcherHint = this.utils.matcherHint("toHaveDiagnostics", undefined, "", this);
1919

test/transpile/module-resolution.spec.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,7 @@ describe("module resolution should not try to resolve modules in noResolvePaths"
470470
export function foo(): void;
471471
}`
472472
)
473-
.addExtraFile(
474-
"preload.d.ts",
475-
`declare module "preload" {}`
476-
)
473+
.addExtraFile("preload.d.ts", `declare module "preload" {}`)
477474
.setOptions({ noResolvePaths: ["ignore*"] })
478475
.expectToHaveNoDiagnostics()
479476
.expectToEqual({ result: "foo" });

test/transpile/transformers/fixtures.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import assert from "assert";
1+
import * as assert from "assert";
22
import * as ts from "typescript";
33
import * as tstl from "../../../src";
44
import { visitAndReplace } from "./utils";
@@ -36,7 +36,7 @@ export const compilerOptions =
3636
(options: tstl.CompilerOptions): ts.TransformerFactory<ts.SourceFile> =>
3737
context =>
3838
file => {
39-
assert(options.plugins?.length === 1);
39+
assert.ok(options.plugins?.length === 1);
4040
return visitAndReplace(context, file, node => {
4141
if (!ts.isReturnStatement(node)) return;
4242
return ts.factory.updateReturnStatement(node, ts.factory.createTrue());

test/unit/builtins/array.spec.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ test.each([
421421
{ array: [0, 1, 2, 3], start: 1, deleteCount: undefined },
422422
{ array: [0, 1, 2, 3], start: 1, deleteCount: null },
423423
])("array.splice (%p)", ({ array, start, deleteCount, newElements = [] }) => {
424-
const deleteCountCode = deleteCount === undefined ? "undefined as any" : deleteCount === null ? "null as any" : util.formatCode(deleteCount);
424+
const deleteCountCode =
425+
deleteCount === undefined
426+
? "undefined as any"
427+
: deleteCount === null
428+
? "null as any"
429+
: util.formatCode(deleteCount);
425430
const newElementsCode = newElements.length > 0 ? ", " + util.formatCode(...newElements) : "";
426431
util.testFunction`
427432
const array: number[] = ${util.formatCode(array)};
@@ -805,9 +810,12 @@ test.each([
805810
});
806811

807812
describe("array.fill", () => {
808-
test.each(["([] as number[])", "[1]", "[1,2,3,4]"])("Fills full length of array without other parameters (%p)", arr => {
809-
util.testExpression`${arr}.fill(5)`.expectToMatchJsResult();
810-
});
813+
test.each(["([] as number[])", "[1]", "[1,2,3,4]"])(
814+
"Fills full length of array without other parameters (%p)",
815+
arr => {
816+
util.testExpression`${arr}.fill(5)`.expectToMatchJsResult();
817+
}
818+
);
811819

812820
test.each(["[1,2,3]", "[1,2,3,4,5,6]"])("Fills starting from start parameter (%p)", arr => {
813821
util.testExpression`${arr}.fill(5, 3)`.expectToMatchJsResult();

test/unit/destructuring.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ const testCases = [
2929
{ binding: "{ x: [{ y }] }", value: { x: [{ y: "y" }] } },
3030
].map(({ binding, value }) => ({ binding, value: util.formatCode(value) }));
3131

32-
test.each([
33-
...testCases,
34-
])("in function parameter (%p)", ({ binding, value }) => {
32+
test.each([...testCases])("in function parameter (%p)", ({ binding, value }) => {
3533
util.testFunction`
3634
let ${allBindings};
3735
function test(${binding}: any) {

test/unit/error.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ test("multi return from try", () => {
9090
}
9191
const [foo, bar] = foobar();
9292
return foo + bar;
93-
`.setOptions({ strictNullChecks: false }).withLanguageExtensions();
93+
`
94+
.setOptions({ strictNullChecks: false })
95+
.withLanguageExtensions();
9496
expect(testBuilder.getMainLuaCodeChunk()).not.toMatch("unpack(foobar");
9597
testBuilder.expectToMatchJsResult();
9698
});

test/unit/functions/functions.spec.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,22 @@ test.each(["i++", "i--", "++i", "--i"])("Arrow function unary expression (%p)",
2525
`.expectToMatchJsResult();
2626
});
2727

28-
test.each(["(b: any) => a = b", "(b: any) => a += b", "(b: any) => a -= b", "(b: any) => a *= b", "(b: any) => a /= b", "(b: any) => a **= b", "(b: any) => a %= b"])(
29-
"Arrow function assignment (%p)",
30-
lambda => {
31-
util.testFunction`
28+
test.each([
29+
"(b: any) => a = b",
30+
"(b: any) => a += b",
31+
"(b: any) => a -= b",
32+
"(b: any) => a *= b",
33+
"(b: any) => a /= b",
34+
"(b: any) => a **= b",
35+
"(b: any) => a %= b",
36+
])("Arrow function assignment (%p)", lambda => {
37+
util.testFunction`
3238
let a = 10;
3339
let lambda = ${lambda};
3440
lambda(5);
3541
return a;
3642
`.expectToMatchJsResult();
37-
}
38-
);
43+
});
3944

4045
test.each([{ args: [] }, { args: [1] }, { args: [1, 2] }])("Arrow default values (%p)", ({ args }) => {
4146
util.testFunction`

test/unit/functions/validation/validFunctionAssignments.spec.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,20 +133,26 @@ test.each([
133133
});
134134

135135
test.each([
136-
...([
137-
{ value: "(s: string) => s" },
138-
{ value: "((s: string) => s)" },
139-
{ value: "function(s: string) { return s; }" },
140-
{ value: "(function(s: string) { return s; })" },
141-
] as TestFunction[]).map((f): [TestFunction, string[]] => [f, ["0", "'foobar'"]]),
142-
...([
143-
{ value: "function(this: any, s: string) { return s; }" },
144-
{ value: "(function(this: any, s: string) { return s; })" },
145-
] as TestFunction[]).map((f): [TestFunction, string[]] => [f, ["0", "'foobar'"]]),
146-
...([
147-
{ value: "function(this: void, s: string) { return s; }" },
148-
{ value: "(function(this: void, s: string) { return s; })" },
149-
] as TestFunction[]).map((f): [TestFunction, string[]] => [f, ["'foobar'"]]),
136+
...(
137+
[
138+
{ value: "(s: string) => s" },
139+
{ value: "((s: string) => s)" },
140+
{ value: "function(s: string) { return s; }" },
141+
{ value: "(function(s: string) { return s; })" },
142+
] as TestFunction[]
143+
).map((f): [TestFunction, string[]] => [f, ["0", "'foobar'"]]),
144+
...(
145+
[
146+
{ value: "function(this: any, s: string) { return s; }" },
147+
{ value: "(function(this: any, s: string) { return s; })" },
148+
] as TestFunction[]
149+
).map((f): [TestFunction, string[]] => [f, ["0", "'foobar'"]]),
150+
...(
151+
[
152+
{ value: "function(this: void, s: string) { return s; }" },
153+
{ value: "(function(this: void, s: string) { return s; })" },
154+
] as TestFunction[]
155+
).map((f): [TestFunction, string[]] => [f, ["'foobar'"]]),
150156
])("Valid function expression argument with no signature (%p, %p)", (testFunction, args) => {
151157
util.testFunction`
152158
const takesFunction: any = (fn: (this: void, ...args: any[]) => any, ...args: any[]) => {

0 commit comments

Comments
 (0)