Skip to content

Commit

Permalink
improved recordAndTuple generator error message, added tests for inva…
Browse files Browse the repository at this point in the history
…lid,missing options
  • Loading branch information
rickbutton committed Jan 14, 2020
1 parent cdfd3bc commit 1437239
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
4 changes: 3 additions & 1 deletion packages/babel-generator/src/generators/types.js
Expand Up @@ -119,7 +119,9 @@ export function RecordExpression(node: Object) {
endToken = "}";
} else {
throw new Error(
`${this.format.recordAndTupleSyntaxType} is not a valid recordAndTuple syntax type`,
`The "recordAndTupleSyntaxType" generator option must be either "bar" or "hash" (${JSON.stringify(
this.format.recordAndTupleSyntaxType,
)} received).`,
);
}

Expand Down
@@ -0,0 +1 @@
#{ a: 1 }
@@ -0,0 +1,5 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]],
"recordAndTupleSyntaxType": "invalid",
"throws": "The \"recordAndTupleSyntaxType\" generator option must be either \"bar\" or \"hash\" (\"invalid\" received)"
}
@@ -0,0 +1 @@
#{ a: 1 }
@@ -0,0 +1,4 @@
{
"plugins": [["recordAndTuple", { "syntaxType": "hash" }]],
"throws": "The \"recordAndTupleSyntaxType\" generator option must be either \"bar\" or \"hash\" (undefined received)"
}
37 changes: 24 additions & 13 deletions packages/babel-generator/test/index.js
Expand Up @@ -525,22 +525,33 @@ suites.forEach(function(testSuite) {
sourceMaps: task.sourceMap ? true : task.options.sourceMaps,
};

const result = generate(actualAst, options, actualCode);

if (options.sourceMaps) {
expect(result.map).toEqual(task.sourceMap);
function run() {
return generate(actualAst, options, actualCode);
}

if (
!expected.code &&
result.code &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
console.log(`New test file created: ${expected.loc}`);
fs.writeFileSync(expected.loc, result.code);
const throwMsg = task.options.throws;
if (throwMsg) {
expect(() => run()).toThrow(
throwMsg === true ? undefined : throwMsg,
);
} else {
expect(result.code).toBe(expected.code);
const result = run();

if (options.sourceMaps) {
expect(result.map).toEqual(task.sourceMap);
}

if (
!expected.code &&
result.code &&
fs.statSync(path.dirname(expected.loc)).isDirectory() &&
!process.env.CI
) {
console.log(`New test file created: ${expected.loc}`);
fs.writeFileSync(expected.loc, result.code);
} else {
expect(result.code).toBe(expected.code);
}
}
}
},
Expand Down

0 comments on commit 1437239

Please sign in to comment.