Skip to content

Commit

Permalink
Format TSAsExpression with same logic as other binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Sep 6, 2019
1 parent b6c8ee8 commit 3086b23
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 58 deletions.
11 changes: 10 additions & 1 deletion src/language-js/postprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@

const { getLast } = require("../common/util");

// fix unexpected locEnd caused by --no-semi style
function postprocess(ast, options) {
visitNode(ast, node => {
switch (node.type) {
// Fix unexpected locEnd caused by --no-semi style
case "VariableDeclaration": {
const lastDeclaration = getLast(node.declarations);
if (lastDeclaration && lastDeclaration.init) {
overrideLocEnd(node, lastDeclaration);
}
break;
}
// Convert TSAsExpression to BinaryExpression interface
case "TSAsExpression": {
node.left = node.expression;
node.operator = "as";
node.right = node.typeAnnotation;
delete node.expression;
delete node.typeAnnotation;
break;
}
}
});

Expand Down
29 changes: 4 additions & 25 deletions src/language-js/printer-estree.js
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,8 @@ function printPathNoParens(path, options, print, args) {
);
case "BinaryExpression":
case "LogicalExpression":
case "NGPipeExpression": {
case "NGPipeExpression":
case "TSAsExpression": {
const parent = path.getParentNode();
const parentParent = path.getParentNode(1);
const isInsideParenthesis =
Expand Down Expand Up @@ -3094,29 +3095,6 @@ function printPathNoParens(path, options, print, args) {
return "unknown";
case "TSVoidKeyword":
return "void";
case "TSAsExpression": {
const typeText = options.originalText.slice(
options.locStart(n.typeAnnotation),
options.locEnd(n.typeAnnotation)
);
// Defer to expansion logic for objects, arrays, and annotations wrapped in parentheses.
const mightBeMultiline = /^[({[]/.test(typeText.trim());

return conditionalGroup([
concat([
path.call(print, "expression"),
" as ",
path.call(print, "typeAnnotation")
]),
concat([
path.call(print, "expression"),
" as ",
mightBeMultiline
? path.call(print, "typeAnnotation")
: indent(concat([softline, path.call(print, "typeAnnotation")]))
])
]);
}
case "TSArrayType":
return concat([path.call(print, "elementType"), "[]"]);
case "TSPropertySignature": {
Expand Down Expand Up @@ -5823,7 +5801,8 @@ function isBinaryish(node) {
return (
node.type === "BinaryExpression" ||
node.type === "LogicalExpression" ||
node.type === "NGPipeExpression"
node.type === "NGPipeExpression" ||
node.type === "TSAsExpression"
);
}

Expand Down
44 changes: 20 additions & 24 deletions tests/typescript_as/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ const state = JSON.stringify({
(foo.bar as any)++;
const value1 = thisIsAReallyReallyReallyReallyReallyLongIdentifier as SomeInterface;
const value1 = thisIsAnIdentifier as thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value2 = thisIsAReallyLongIdentifier as (SomeInterface | SomeOtherInterface);
const value3 = thisIsAReallyLongIdentifier as { prop1: string, prop2: number, prop3: number }[];
const value4 = thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyLongIdentifier as [string, number];
const value2 = thisIsAnIdentifier as thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value3 = thisIsAReallyLongIdentifier as (SomeInterface | SomeOtherInterface);
const value4 = thisIsAReallyLongIdentifier as { prop1: string, prop2: number, prop3: number }[];
const value5 = thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyLongIdentifier as [string, number];
(bValue as boolean) ? 0 : -1;
<boolean>bValue ? 0 : -1;
=====================================output=====================================
const name = (description as DescriptionObject).name || (description as string);
const name = (description as DescriptionObject).name || description as string;
this.isTabActionBar((e.target || e.srcElement) as HTMLElement);
(originalError ? wrappedError(errMsg, originalError) : Error(errMsg)) as
InjectionError;
"current" in (props.pagination as Object);
start + (yearSelectTotal as number);
scrollTop > (visibilityHeight as number);
"current" in props.pagination as Object;
start + yearSelectTotal as number;
scrollTop > visibilityHeight as number;
export default class Column<T> extends (RcTable.Column as
React.ComponentClass<
ColumnProps<T>,
Expand All @@ -75,23 +75,19 @@ const state = JSON.stringify({
(foo.bar as Baz) = [bar];
(foo.bar as any)++;
const value1 = thisIsAReallyReallyReallyReallyReallyLongIdentifier as
SomeInterface;
const value1 = thisIsAnIdentifier as
const value1 =
thisIsAReallyReallyReallyReallyReallyLongIdentifier as SomeInterface;
const value2 =
thisIsAnIdentifier as
thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value2 = thisIsAReallyLongIdentifier as (
| SomeInterface
| SomeOtherInterface
);
const value3 = thisIsAReallyLongIdentifier as {
prop1: string;
prop2: number;
prop3: number;
}[];
const value4 = thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyLongIdentifier as [
string,
number
];
const value3 =
thisIsAReallyLongIdentifier as (SomeInterface | SomeOtherInterface);
const value4 =
thisIsAReallyLongIdentifier as
{ prop1: string; prop2: number; prop3: number }[];
const value5 =
thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyLongIdentifier as
[string, number];
(bValue as boolean) ? 0 : -1;
<boolean>bValue ? 0 : -1;
Expand Down
8 changes: 4 additions & 4 deletions tests/typescript_as/as.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ const state = JSON.stringify({
(foo.bar as any)++;

const value1 = thisIsAReallyReallyReallyReallyReallyLongIdentifier as SomeInterface;
const value1 = thisIsAnIdentifier as thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value2 = thisIsAReallyLongIdentifier as (SomeInterface | SomeOtherInterface);
const value3 = thisIsAReallyLongIdentifier as { prop1: string, prop2: number, prop3: number }[];
const value4 = thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyLongIdentifier as [string, number];
const value2 = thisIsAnIdentifier as thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyReallyLongInterface;
const value3 = thisIsAReallyLongIdentifier as (SomeInterface | SomeOtherInterface);
const value4 = thisIsAReallyLongIdentifier as { prop1: string, prop2: number, prop3: number }[];
const value5 = thisIsAReallyReallyReallyReallyReallyReallyReallyReallyReallyLongIdentifier as [string, number];

(bValue as boolean) ? 0 : -1;
<boolean>bValue ? 0 : -1;
6 changes: 2 additions & 4 deletions tests/typescript_union/__snapshots__/jsfmt.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,8 @@ type State = {
| { discriminant: "BAZ"; baz: any }
);
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
| string
| undefined
)[];
const foo =
[abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (string | undefined)[];
const foo: (
| AAAAAAAAAAAAAAAAAAAAAA
Expand Down

0 comments on commit 3086b23

Please sign in to comment.