Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix(babel-types): add validator for TSLiteralType to accept UnaryExpression seems that the generated files is missing typings fix #13327 * fix(babel-types): add allowlist for TSLiteralType now generated types seems correct * fix(babel-types): use oneOfNodeTypes instead of oneOf
- Loading branch information
Showing
5 changed files
with
129 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/babel-types/test/builders/typescript/__snapshots__/tsLiteralType.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
52
packages/babel-types/test/builders/typescript/tsLiteralType.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"', | ||
); | ||
}); | ||
}); | ||
}); |