Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(babel-types): accept UnaryExpression in TSLiteralType #13525

Merged
merged 3 commits into from Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/babel-types/src/ast-types/generated/index.ts
Expand Up @@ -1905,7 +1905,7 @@ export interface TSMappedType extends BaseNode {

export interface TSLiteralType extends BaseNode {
type: "TSLiteralType";
literal: NumericLiteral | StringLiteral | BooleanLiteral | BigIntLiteral;
literal: any;
colinaaa marked this conversation as resolved.
Show resolved Hide resolved
}

export interface TSExpressionWithTypeArguments extends BaseNode {
Expand Down
8 changes: 1 addition & 7 deletions packages/babel-types/src/builders/generated/index.ts
Expand Up @@ -1323,13 +1323,7 @@ export function tsMappedType(
return builder("TSMappedType", ...arguments);
}
export { tsMappedType as tSMappedType };
export function tsLiteralType(
literal:
| t.NumericLiteral
| t.StringLiteral
| t.BooleanLiteral
| t.BigIntLiteral,
): t.TSLiteralType {
export function tsLiteralType(literal: any): t.TSLiteralType {
return builder("TSLiteralType", ...arguments);
}
export { tsLiteralType as tSLiteralType };
Expand Down
34 changes: 28 additions & 6 deletions packages/babel-types/src/definitions/typescript.ts
Expand Up @@ -15,6 +15,7 @@ import {
functionDeclarationCommon,
classMethodOrDeclareMethodCommon,
} from "./core";
import is from "../validators/is";

const bool = assertValueType("boolean");

Expand Down Expand Up @@ -335,12 +336,33 @@ defineType("TSLiteralType", {
aliases: ["TSType", "TSBaseType"],
visitor: ["literal"],
fields: {
literal: validateType([
"NumericLiteral",
"StringLiteral",
"BooleanLiteral",
"BigIntLiteral",
]),
literal: {
validate: (function () {
const unaryExpression = assertNodeType(
"NumericLiteral",
"BigIntLiteral",
);
const unaryOperator = assertOneOf("-");

const literal = assertNodeType(
"NumericLiteral",
"StringLiteral",
"BooleanLiteral",
"BigIntLiteral",
);
return function validator(parent, key: string, node) {
// type A = -1 | 1;
if (is("UnaryExpression", node)) {
// check operator first
unaryOperator(node, "operator", node.operator);
unaryExpression(node, "argument", node.argument);
} else {
// type A = 'foo' | 'bar' | false | 1;
literal(parent, key, node);
}
};
})(),
},
},
});

Expand Down
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`builders typescript tsLiteralType accept unary expression 1`] = `
Object {
"literal": Object {
"argument": Object {
"type": "NumericLiteral",
"value": 1,
},
"operator": "-",
"prefix": true,
"type": "UnaryExpression",
},
"type": "TSLiteralType",
}
`;

exports[`builders typescript tsLiteralType accept unary expression 2`] = `
Object {
"literal": Object {
"argument": Object {
"type": "BigIntLiteral",
"value": "123456789",
},
"operator": "-",
"prefix": true,
"type": "UnaryExpression",
},
"type": "TSLiteralType",
}
`;
52 changes: 52 additions & 0 deletions packages/babel-types/test/builders/typescript/tsLiteralType.js
@@ -0,0 +1,52 @@
import * as t from "../../..";

describe("builders", function () {
describe("typescript", function () {
describe("tsLiteralType", function () {
it("accept unary expression", function () {
expect(
t.tsLiteralType(t.unaryExpression("-", t.numericLiteral(1))),
).toMatchSnapshot();
expect(
t.tsLiteralType(t.unaryExpression("-", t.bigIntLiteral("123456789"))),
).toMatchSnapshot();
});
it("throws with non-numeric argument", function () {
expect(() => {
t.tsLiteralType(t.unaryExpression("-", t.stringLiteral(1)));
}).toThrow("Property value expected type of string but got number");
expect(() => {
t.tsLiteralType(t.unaryExpression("-", t.objectExpression([])));
}).toThrow(
'Property argument of UnaryExpression expected node to be of a type ["NumericLiteral","BigIntLiteral"] but instead got "ObjectExpression"',
);
});
});
it("throws with bad operator", function () {
expect(() => {
t.tsLiteralType(t.unaryExpression("+", t.numericLiteral(1)));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "+"',
);

// should check operator first since it appears first
expect(() => {
t.tsLiteralType(t.unaryExpression("+", t.objectExpression([])));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "+"',
);

expect(() => {
t.tsLiteralType(t.unaryExpression("~", t.numericLiteral(1)));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "~"',
);

expect(() => {
t.tsLiteralType(t.unaryExpression("void", t.numericLiteral(1)));
}).toThrow(
'Property operator expected value to be one of ["-"] but got "void"',
);
});
});
});