Skip to content

Commit 2eaecb4

Browse files
authored
Support both 'yaml' and 'yml' file extensions (#2092)
* Support both 'yaml' and 'yml' file extensions We can support both formats in Dataform. Native configurations are stored in YAML format, but other configurations eg. from extensions might use YML too. * Support both 'yaml' and 'yml' file extensions We can support both formats in Dataform. Native configurations are stored in YAML format, but other configurations eg. from extensions might use YML too. * Support both 'yaml' and 'yml' file extensions We can support both formats in Dataform. Native configurations are stored in YAML format, but other configurations eg. from extensions might use YML too. * Reorganize imports in compilers_test.ts
1 parent b7b40ec commit 2eaecb4

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

cli/vm/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function compile(compileConfig: dataform.ICompileConfig) {
4848
resolve: (moduleName, parentDirName) =>
4949
path.join(parentDirName, path.relative(parentDirName, compileConfig.projectDir), moduleName)
5050
},
51-
sourceExtensions: ["js", "sql", "sqlx", "yaml"],
51+
sourceExtensions: ["js", "sql", "sqlx", "yaml", "yml"],
5252
compiler
5353
});
5454

core/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ ts_test_suite(
6767
srcs = [
6868
"main_test.ts",
6969
"utils_test.ts",
70+
"compilers_test.ts",
7071
"actions/assertion_test.ts",
7172
"actions/data_preparation_test.ts",
7273
"actions/declaration_test.ts",

core/compilers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function compile(code: string, path: string): string {
2828
if (Path.fileExtension(path) === "sqlx") {
2929
return compileSqlx(SyntaxTreeNode.create(code), path);
3030
}
31-
if (Path.fileExtension(path) === "yaml") {
31+
if (Path.fileExtension(path) === "yaml" || Path.fileExtension(path) === "yml") {
3232
try {
3333
const yamlAsJson = loadYaml(code);
3434
return `exports.asJson = ${JSON.stringify(yamlAsJson)}`;

core/compilers_test.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
import { expect } from "chai";
2+
3+
import { compile } from "df/core/compilers";
4+
import { suite, test } from "df/testing";
5+
6+
suite("core/compilers", () => {
7+
suite("compile", () => {
8+
test("compiles sqlx to js", () => {
9+
const code = `config { type: "table" } select 1`;
10+
const path = "definitions/foo.sqlx";
11+
const result = compile(code, path);
12+
expect(result).to.include("dataform.sqlxAction");
13+
expect(result).to.include("name: \"foo\"");
14+
expect(result).to.include("{ type: \"table\" }");
15+
// The sqlx compiler will format the query.
16+
expect(result).to.include("select 1");
17+
});
18+
19+
test("compiles yml to js", () => {
20+
const code = "foo: bar";
21+
const path = "definitions/foo.yml";
22+
const result = compile(code, path);
23+
expect(result).to.equal(`exports.asJson = {"foo":"bar"}`);
24+
});
25+
26+
test("compiles yaml to js", () => {
27+
const code = `
28+
- item1
29+
- item2`;
30+
const path = "definitions/foo.yaml";
31+
const result = compile(code, path);
32+
expect(result).to.equal(`exports.asJson = ["item1","item2"]`);
33+
});
34+
35+
test("throws error for invalid yaml", () => {
36+
const code = "foo: : bar";
37+
const path = "definitions/foo.yaml";
38+
expect(() => compile(code, path)).to.throw(`${path} is not a valid YAML file: YAMLException: bad indentation of a mapping entry (1:6)`);
39+
});
40+
41+
test("compiles ipynb to js", () => {
42+
const code = '{"cells": []}';
43+
const path = "definitions/foo.ipynb";
44+
const result = compile(code, path);
45+
expect(result).to.equal('exports.asJson = {"cells":[]}');
46+
});
47+
48+
test("throws error for invalid ipynb json", () => {
49+
const code = '{"cells": [}';
50+
const path = "definitions/foo.ipynb";
51+
expect(() => compile(code, path)).to.throw(`Error parsing ${path} as JSON:`);
52+
});
53+
54+
test("compiles sql to js", () => {
55+
const code = "select 1";
56+
const path = "definitions/foo.sql";
57+
const result = compile(code, path);
58+
expect(result).to.equal("exports.query = `select 1`;");
59+
});
60+
61+
test("escapes backticks in sql", () => {
62+
const code = "select `a` from `b`";
63+
const path = "definitions/foo.sql";
64+
const result = compile(code, path);
65+
expect(result).to.equal("exports.query = `select \\`a\\` from \\`b\\``;");
66+
});
67+
68+
test("escapes backslashes in sql", () => {
69+
const code = "select ''";
70+
const path = "definitions/foo.sql";
71+
const result = compile(code, path);
72+
expect(result).to.equal("exports.query = `select ''`;");
73+
});
74+
75+
test("escapes template literals in sql", () => {
76+
const code = "select ${foo}";
77+
const path = "definitions/foo.sql";
78+
const result = compile(code, path);
79+
expect(result).to.equal("exports.query = `select \\${foo}`;");
80+
});
81+
82+
test("returns raw code for other file types", () => {
83+
const code = "const a = 1;";
84+
const path = "definitions/foo.js";
85+
const result = compile(code, path);
86+
expect(result).to.equal(code);
87+
});
88+
89+
test("returns raw code for files with no extension", () => {
90+
const code = "const a = 1;";
91+
const path = "definitions/foo";
92+
const result = compile(code, path);
93+
expect(result).to.equal(code);
94+
});
95+
});
96+
});

0 commit comments

Comments
 (0)