Skip to content

Commit

Permalink
feat(typescript): support TSPlusToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Wang Zhongliang committed Mar 16, 2018
1 parent 67277f7 commit 5eea8aa
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
19 changes: 15 additions & 4 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,16 @@ function formatTernaryOperator(path, options, print, operatorOptions) {
);
}

function getTypeScriptMappedTypeModifier(tokenNode, keyword) {
if (tokenNode.type === "TSPlusToken") {
return "+" + keyword;
} else if (tokenNode.type === "TSMinusToken") {
return "-" + keyword;
} else {
return keyword;
}
}

function printPathNoParens(path, options, print, args) {
const n = path.getValue();
const semi = options.semi ? ";" : "";
Expand Down Expand Up @@ -2723,9 +2733,10 @@ function printPathNoParens(path, options, print, args) {
options.bracketSpacing ? line : softline,
n.readonlyToken
? concat([
n.readonlyToken.type === "TSMinusToken"
? "-readonly"
: "readonly",
getTypeScriptMappedTypeModifier(
n.readonlyToken,
"readonly"
),
" "
])
: "",
Expand All @@ -2734,7 +2745,7 @@ function printPathNoParens(path, options, print, args) {
path.call(print, "typeParameter"),
"]",
n.questionToken
? n.questionToken.type === "TSMinusToken" ? "-?" : "?"
? getTypeScriptMappedTypeModifier(n.questionToken, "?")
: "",
": ",
path.call(print, "typeAnnotation")
Expand Down
14 changes: 10 additions & 4 deletions tests/typescript/custom/modifiers/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`minustoken.ts 1`] = `
type MutableAndRequired<T> = {
-readonly [P in keyof T]-?: T[P]
}
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 MutableAndRequired<T> = { -readonly [P in keyof T]-?: T[P] };
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 ?
`;
Expand Down
10 changes: 7 additions & 3 deletions tests/typescript/custom/modifiers/minustoken.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
type MutableAndRequired<T> = {
-readonly [P in keyof T]-?: T[P]
}
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 ?

0 comments on commit 5eea8aa

Please sign in to comment.