diff --git a/src/CompilerOptions.ts b/src/CompilerOptions.ts index b22ee031a..deee41b70 100644 --- a/src/CompilerOptions.ts +++ b/src/CompilerOptions.ts @@ -87,5 +87,9 @@ export function validateOptions(options: CompilerOptions): ts.Diagnostic[] { diagnostics.push(diagnosticFactories.unsupportedJsxEmit()); } + if (options.paths && !options.baseUrl) { + diagnostics.push(diagnosticFactories.pathsWithoutBaseUrl()); + } + return diagnostics; } diff --git a/src/transpilation/diagnostics.ts b/src/transpilation/diagnostics.ts index 0aa772cd3..1f37f3260 100644 --- a/src/transpilation/diagnostics.ts +++ b/src/transpilation/diagnostics.ts @@ -55,3 +55,7 @@ export const cannotBundleLibrary = createDiagnosticFactory( ); export const unsupportedJsxEmit = createDiagnosticFactory(() => 'JSX is only supported with "react" jsx option.'); + +export const pathsWithoutBaseUrl = createDiagnosticFactory( + () => "When configuring 'paths' in tsconfig.json, the option 'baseUrl' must also be provided." +); diff --git a/test/transpile/__snapshots__/module-resolution.spec.ts.snap b/test/transpile/__snapshots__/module-resolution.spec.ts.snap index 5cb03e86b..2e4d888cb 100644 --- a/test/transpile/__snapshots__/module-resolution.spec.ts.snap +++ b/test/transpile/__snapshots__/module-resolution.spec.ts.snap @@ -2,16 +2,16 @@ exports[`supports complicated paths configuration 1`] = ` Array [ - "/monorepo-complicated/dist/packages/tstl-program/packages/mypackage/src/bar.lua", - "/monorepo-complicated/dist/packages/tstl-program/packages/mypackage/src/index.lua", - "/monorepo-complicated/dist/packages/tstl-program/packages/myprogram/src/main.lua", + "/paths-base-tsconfig/dist/packages/tstl-program/packages/mypackage/src/bar.lua", + "/paths-base-tsconfig/dist/packages/tstl-program/packages/mypackage/src/index.lua", + "/paths-base-tsconfig/dist/packages/tstl-program/packages/myprogram/src/main.lua", ] `; exports[`supports paths configuration 1`] = ` Array [ - "/monorepo-with-paths/myprogram/dist/mypackage/bar.lua", - "/monorepo-with-paths/myprogram/dist/mypackage/index.lua", - "/monorepo-with-paths/myprogram/dist/myprogram/main.lua", + "/paths-simple/myprogram/dist/mypackage/bar.lua", + "/paths-simple/myprogram/dist/mypackage/index.lua", + "/paths-simple/myprogram/dist/myprogram/main.lua", ] `; diff --git a/test/transpile/module-resolution.spec.ts b/test/transpile/module-resolution.spec.ts index d78114ef4..4e8f3d49b 100644 --- a/test/transpile/module-resolution.spec.ts +++ b/test/transpile/module-resolution.spec.ts @@ -4,6 +4,7 @@ import * as util from "../util"; import * as ts from "typescript"; import { BuildMode } from "../../src"; import { normalizeSlashes } from "../../src/utils"; +import { pathsWithoutBaseUrl } from "../../src/transpilation/diagnostics"; describe("basic module resolution", () => { const projectPath = path.resolve(__dirname, "module-resolution", "project-with-node-modules"); @@ -542,7 +543,7 @@ test("lualib_module with parent directory import (#1307)", () => { test("supports paths configuration", () => { // Package root - const baseProjectPath = path.resolve(__dirname, "module-resolution", "monorepo-with-paths"); + const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-simple"); // myprogram package const projectPath = path.join(baseProjectPath, "myprogram"); const projectTsConfig = path.join(projectPath, "tsconfig.json"); @@ -565,7 +566,7 @@ test("supports paths configuration", () => { test("supports complicated paths configuration", () => { // Package root - const baseProjectPath = path.resolve(__dirname, "module-resolution", "monorepo-complicated"); + const baseProjectPath = path.resolve(__dirname, "module-resolution", "paths-base-tsconfig"); // myprogram package const projectPath = path.join(baseProjectPath, "packages", "myprogram"); const projectTsConfig = path.join(projectPath, "tsconfig.json"); @@ -586,6 +587,10 @@ test("supports complicated paths configuration", () => { .expectToEqual({ foo: 314, bar: 271 }); }); +test("paths without baseUrl is error", () => { + util.testFunction``.setOptions({ paths: {} }).expectToHaveDiagnostics([pathsWithoutBaseUrl.code]); +}); + function snapshotPaths(files: tstl.TranspiledFile[]) { return files.map(f => normalizeSlashes(f.outPath).split("module-resolution")[1]).sort(); } diff --git a/test/transpile/module-resolution/monorepo-with-paths/myprogram/main.ts b/test/transpile/module-resolution/monorepo-with-paths/myprogram/main.ts deleted file mode 100644 index 973b93922..000000000 --- a/test/transpile/module-resolution/monorepo-with-paths/myprogram/main.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { foo } from "mypackage"; -import { bar } from "mypackage/bar"; - -export { foo, bar }; diff --git a/test/transpile/module-resolution/monorepo-with-paths/myprogram/tsconfig.json b/test/transpile/module-resolution/monorepo-with-paths/myprogram/tsconfig.json deleted file mode 100644 index d5f59e8b5..000000000 --- a/test/transpile/module-resolution/monorepo-with-paths/myprogram/tsconfig.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "compilerOptions": { - "baseUrl": "..", - "outDir": "dist", - "paths": { - "mypackage": ["mypackage"] - } - } -} diff --git a/test/transpile/module-resolution/monorepo-complicated/packages/mypackage/src/bar.ts b/test/transpile/module-resolution/paths-base-tsconfig/packages/mypackage/src/bar.ts similarity index 100% rename from test/transpile/module-resolution/monorepo-complicated/packages/mypackage/src/bar.ts rename to test/transpile/module-resolution/paths-base-tsconfig/packages/mypackage/src/bar.ts diff --git a/test/transpile/module-resolution/monorepo-complicated/packages/mypackage/src/index.ts b/test/transpile/module-resolution/paths-base-tsconfig/packages/mypackage/src/index.ts similarity index 100% rename from test/transpile/module-resolution/monorepo-complicated/packages/mypackage/src/index.ts rename to test/transpile/module-resolution/paths-base-tsconfig/packages/mypackage/src/index.ts diff --git a/test/transpile/module-resolution/monorepo-complicated/packages/mypackage/tsconfig.json b/test/transpile/module-resolution/paths-base-tsconfig/packages/mypackage/tsconfig.json similarity index 100% rename from test/transpile/module-resolution/monorepo-complicated/packages/mypackage/tsconfig.json rename to test/transpile/module-resolution/paths-base-tsconfig/packages/mypackage/tsconfig.json diff --git a/test/transpile/module-resolution/monorepo-complicated/packages/myprogram/src/main.ts b/test/transpile/module-resolution/paths-base-tsconfig/packages/myprogram/src/main.ts similarity index 100% rename from test/transpile/module-resolution/monorepo-complicated/packages/myprogram/src/main.ts rename to test/transpile/module-resolution/paths-base-tsconfig/packages/myprogram/src/main.ts diff --git a/test/transpile/module-resolution/monorepo-complicated/packages/myprogram/tsconfig.json b/test/transpile/module-resolution/paths-base-tsconfig/packages/myprogram/tsconfig.json similarity index 100% rename from test/transpile/module-resolution/monorepo-complicated/packages/myprogram/tsconfig.json rename to test/transpile/module-resolution/paths-base-tsconfig/packages/myprogram/tsconfig.json diff --git a/test/transpile/module-resolution/monorepo-complicated/tsconfig.base.json b/test/transpile/module-resolution/paths-base-tsconfig/tsconfig.base.json similarity index 100% rename from test/transpile/module-resolution/monorepo-complicated/tsconfig.base.json rename to test/transpile/module-resolution/paths-base-tsconfig/tsconfig.base.json diff --git a/test/transpile/module-resolution/monorepo-with-paths/mypackage/bar.ts b/test/transpile/module-resolution/paths-simple/mypackage/bar.ts similarity index 100% rename from test/transpile/module-resolution/monorepo-with-paths/mypackage/bar.ts rename to test/transpile/module-resolution/paths-simple/mypackage/bar.ts diff --git a/test/transpile/module-resolution/monorepo-with-paths/mypackage/index.ts b/test/transpile/module-resolution/paths-simple/mypackage/index.ts similarity index 100% rename from test/transpile/module-resolution/monorepo-with-paths/mypackage/index.ts rename to test/transpile/module-resolution/paths-simple/mypackage/index.ts diff --git a/test/transpile/module-resolution/monorepo-with-paths/mypackage/tsconfig.json b/test/transpile/module-resolution/paths-simple/mypackage/tsconfig.json similarity index 100% rename from test/transpile/module-resolution/monorepo-with-paths/mypackage/tsconfig.json rename to test/transpile/module-resolution/paths-simple/mypackage/tsconfig.json diff --git a/test/transpile/module-resolution/paths-simple/myprogram/main.ts b/test/transpile/module-resolution/paths-simple/myprogram/main.ts new file mode 100644 index 000000000..e7687877a --- /dev/null +++ b/test/transpile/module-resolution/paths-simple/myprogram/main.ts @@ -0,0 +1,4 @@ +import { foo } from "myOtherPackage"; +import { bar } from "myOtherPackage/bar"; + +export { foo, bar }; diff --git a/test/transpile/module-resolution/paths-simple/myprogram/tsconfig.json b/test/transpile/module-resolution/paths-simple/myprogram/tsconfig.json new file mode 100644 index 000000000..f01271ac9 --- /dev/null +++ b/test/transpile/module-resolution/paths-simple/myprogram/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "baseUrl": ".", + "outDir": "dist", + "paths": { + "myOtherPackage": ["../mypackage"], + "myOtherPackage/*": ["../mypackage/*"] + } + } +} diff --git a/test/unit/jsx.spec.ts b/test/unit/jsx.spec.ts index 03b00e95d..a72d0d7f9 100644 --- a/test/unit/jsx.spec.ts +++ b/test/unit/jsx.spec.ts @@ -1,6 +1,8 @@ import * as util from "../util"; import { TestBuilder } from "../util"; import { JsxEmit } from "typescript"; +import { unsupportedJsxEmit } from "../../src/transpilation/diagnostics"; +import { unsupportedNodeKind } from "../../src/transformation/utils/diagnostics"; // language=TypeScript const reactLib = ` @@ -368,6 +370,6 @@ describe("jsx", () => { .setOptions({ jsx: JsxEmit.Preserve, }) - .expectToHaveDiagnostics(); + .expectToHaveDiagnostics([unsupportedJsxEmit.code, unsupportedNodeKind.code]); }); });