Merge pull request #4156 from Cryrivers/master

feat(typescript): support TSMinusToken and TSPlusToken
master
Brian Ng 2018-03-21 09:06:06 -05:00 committed by GitHub
commit 4553b88bd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 6 deletions

View File

@ -55,7 +55,7 @@
"resolve": "1.5.0",
"semver": "5.4.1",
"string-width": "2.1.1",
"typescript": "2.8.0-dev.20180222",
"typescript": "2.8.0-rc",
"typescript-eslint-parser": "14.0.0",
"unicode-regex": "1.0.1",
"unified": "6.1.6"

View File

@ -303,6 +303,15 @@ function formatTernaryOperator(path, options, print, operatorOptions) {
);
}
function getTypeScriptMappedTypeModifier(tokenNode, keyword) {
if (tokenNode.type === "TSPlusToken") {
return "+" + keyword;
} else if (tokenNode.type === "TSMinusToken") {
return "-" + keyword;
}
return keyword;
}
function printPathNoParens(path, options, print, args) {
const n = path.getValue();
const semi = options.semi ? ";" : "";
@ -2729,13 +2738,21 @@ function printPathNoParens(path, options, print, args) {
concat([
options.bracketSpacing ? line : softline,
n.readonlyToken
? concat([path.call(print, "readonlyToken"), " "])
? concat([
getTypeScriptMappedTypeModifier(
n.readonlyToken,
"readonly"
),
" "
])
: "",
printTypeScriptModifiers(path, options, print),
"[",
path.call(print, "typeParameter"),
"]",
n.questionToken ? "?" : "",
n.questionToken
? getTypeScriptMappedTypeModifier(n.questionToken, "?")
: "",
": ",
path.call(print, "typeAnnotation")
])

View File

@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`minustoken.ts 1`] = `
type MutableRequired<T> = {
-readonly [P in keyof T]-?:T[P]
}; // Remove readonly and ?
type ReadonlyPartial<T> = {
+readonly [P in keyof T]+?:T[P]
}; // Add readonly and ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
type MutableRequired<T> = { -readonly [P in keyof T]-?: T[P] }; // Remove readonly and ?
type ReadonlyPartial<T> = { +readonly [P in keyof T]+?: T[P] }; // Add readonly and ?
`;
exports[`question.ts 1`] = `
var x: {
[A in keyof B]?: any;

View File

@ -0,0 +1,7 @@
type MutableRequired<T> = {
-readonly [P in keyof T]-?:T[P]
}; // Remove readonly and ?
type ReadonlyPartial<T> = {
+readonly [P in keyof T]+?:T[P]
}; // Add readonly and ?

View File

@ -4784,9 +4784,9 @@ typescript-eslint-parser@14.0.0:
lodash.unescape "4.0.1"
semver "5.5.0"
typescript@2.8.0-dev.20180222:
version "2.8.0-dev.20180222"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.0-dev.20180222.tgz#50ee5fd5c76f2c9817e949803f946d74a559fc01"
typescript@2.8.0-rc:
version "2.8.0-rc"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.8.0-rc.tgz#a0256b7d1d39fb7493ba0403f55e95d31e8bc374"
ua-parser-js@^0.7.9:
version "0.7.17"