Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/vm/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function compile(compileConfig: dataform.ICompileConfig) {
resolve: (moduleName, parentDirName) =>
path.join(parentDirName, path.relative(parentDirName, compileConfig.projectDir), moduleName)
},
sourceExtensions: ["js", "sql", "sqlx", "yaml"],
sourceExtensions: ["js", "sql", "sqlx", "yaml", "yml"],
compiler
});

Expand Down
1 change: 1 addition & 0 deletions core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ ts_test_suite(
srcs = [
"main_test.ts",
"utils_test.ts",
"compilers_test.ts",
"actions/assertion_test.ts",
"actions/data_preparation_test.ts",
"actions/declaration_test.ts",
Expand Down
2 changes: 1 addition & 1 deletion core/compilers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function compile(code: string, path: string): string {
if (Path.fileExtension(path) === "sqlx") {
return compileSqlx(SyntaxTreeNode.create(code), path);
}
if (Path.fileExtension(path) === "yaml") {
if (Path.fileExtension(path) === "yaml" || Path.fileExtension(path) === "yml") {
try {
const yamlAsJson = loadYaml(code);
return `exports.asJson = ${JSON.stringify(yamlAsJson)}`;
Expand Down
96 changes: 96 additions & 0 deletions core/compilers_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { expect } from "chai";

import { compile } from "df/core/compilers";
import { suite, test } from "df/testing";

suite("core/compilers", () => {
suite("compile", () => {
test("compiles sqlx to js", () => {
const code = `config { type: "table" } select 1`;
const path = "definitions/foo.sqlx";
const result = compile(code, path);
expect(result).to.include("dataform.sqlxAction");
expect(result).to.include("name: \"foo\"");
expect(result).to.include("{ type: \"table\" }");
// The sqlx compiler will format the query.
expect(result).to.include("select 1");
});

test("compiles yml to js", () => {
const code = "foo: bar";
const path = "definitions/foo.yml";
const result = compile(code, path);
expect(result).to.equal(`exports.asJson = {"foo":"bar"}`);
});

test("compiles yaml to js", () => {
const code = `
- item1
- item2`;
const path = "definitions/foo.yaml";
const result = compile(code, path);
expect(result).to.equal(`exports.asJson = ["item1","item2"]`);
});

test("throws error for invalid yaml", () => {
const code = "foo: : bar";
const path = "definitions/foo.yaml";
expect(() => compile(code, path)).to.throw(`${path} is not a valid YAML file: YAMLException: bad indentation of a mapping entry (1:6)`);
});

test("compiles ipynb to js", () => {
const code = '{"cells": []}';
const path = "definitions/foo.ipynb";
const result = compile(code, path);
expect(result).to.equal('exports.asJson = {"cells":[]}');
});

test("throws error for invalid ipynb json", () => {
const code = '{"cells": [}';
const path = "definitions/foo.ipynb";
expect(() => compile(code, path)).to.throw(`Error parsing ${path} as JSON:`);
});

test("compiles sql to js", () => {
const code = "select 1";
const path = "definitions/foo.sql";
const result = compile(code, path);
expect(result).to.equal("exports.query = `select 1`;");
});

test("escapes backticks in sql", () => {
const code = "select `a` from `b`";
const path = "definitions/foo.sql";
const result = compile(code, path);
expect(result).to.equal("exports.query = `select \\`a\\` from \\`b\\``;");
});

test("escapes backslashes in sql", () => {
const code = "select ''";
const path = "definitions/foo.sql";
const result = compile(code, path);
expect(result).to.equal("exports.query = `select ''`;");
});

test("escapes template literals in sql", () => {
const code = "select ${foo}";
const path = "definitions/foo.sql";
const result = compile(code, path);
expect(result).to.equal("exports.query = `select \\${foo}`;");
});

test("returns raw code for other file types", () => {
const code = "const a = 1;";
const path = "definitions/foo.js";
const result = compile(code, path);
expect(result).to.equal(code);
});

test("returns raw code for files with no extension", () => {
const code = "const a = 1;";
const path = "definitions/foo";
const result = compile(code, path);
expect(result).to.equal(code);
});
});
});
Loading