Skip to content

Commit

Permalink
fix: Fix for expressionTo with Spread and Methods
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/utils/expressionTo.js
  • Loading branch information
danez committed Jul 2, 2022
1 parent 8b34e26 commit 0c3c38f
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`resolveGenericTypeAnnotation resolves type 1`] = `
Node {
"callProperties": Array [],
"end": 57,
"exact": false,
"indexers": Array [],
"inexact": false,
"internalSlots": Array [],
"loc": SourceLocation {
"end": Position {
"column": 34,
"line": 3,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 21,
"line": 3,
},
},
"properties": Array [
Node {
"end": 55,
"key": Node {
"end": 47,
"loc": SourceLocation {
"end": Position {
"column": 24,
"line": 3,
},
"filename": undefined,
"identifierName": "x",
"start": Position {
"column": 23,
"line": 3,
},
},
"name": "x",
"start": 46,
"type": "Identifier",
},
"kind": "init",
"loc": SourceLocation {
"end": Position {
"column": 32,
"line": 3,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 23,
"line": 3,
},
},
"method": false,
"optional": false,
"proto": false,
"start": 46,
"static": false,
"type": "ObjectTypeProperty",
"value": Node {
"end": 55,
"loc": SourceLocation {
"end": Position {
"column": 32,
"line": 3,
},
"filename": undefined,
"identifierName": undefined,
"start": Position {
"column": 26,
"line": 3,
},
},
"start": 49,
"type": "StringTypeAnnotation",
},
"variance": null,
},
],
"start": 44,
"type": "ObjectTypeAnnotation",
}
`;
20 changes: 20 additions & 0 deletions src/utils/__tests__/isUnreachableFlowType-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expression, statement } from '../../../tests/utils';
import isUnreachableFlowType from '../isUnreachableFlowType';

describe('isUnreachableFlowType', () => {
it('considers Identifier as unreachable', () => {
expect(isUnreachableFlowType(expression('foo'))).toBe(true);
});

it('considers ImportDeclaration as unreachable', () => {
expect(isUnreachableFlowType(statement('import x from "";'))).toBe(true);
});

it('considers CallExpression as unreachable', () => {
expect(isUnreachableFlowType(expression('foo()'))).toBe(true);
});

it('considers VariableDeclaration not as unreachable', () => {
expect(isUnreachableFlowType(statement('const x = 1;'))).toBe(false);
});
});
23 changes: 23 additions & 0 deletions src/utils/__tests__/resolveGenericTypeAnnotations-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { statement, noopImporter } from '../../../tests/utils';
import resolveGenericTypeAnnotation from '../resolveGenericTypeAnnotation';

describe('resolveGenericTypeAnnotation', () => {
it('resolves type', () => {
const code = `
var x: Props;
type Props = { x: string };
`;
expect(
resolveGenericTypeAnnotation(
statement(code).get(
'declarations',
0,
'id',
'typeAnnotation',
'typeAnnotation',
),
noopImporter,
),
).toMatchSnapshot();
});
});
15 changes: 12 additions & 3 deletions src/utils/expressionTo.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ function toArray(path: NodePath): Array<string> {
} else if (t.Literal.check(node)) {
result.push(node.raw);
continue;
} else if (t.FunctionExpression.check(node)) {
result.push('<function>');
continue;
} else if (t.ThisExpression.check(node)) {
result.push('this');
continue;
} else if (t.ObjectExpression.check(node)) {
const properties = path.get('properties').map(function (property) {
return (
toString(property.get('key')) + ': ' + toString(property.get('value'))
);
if (t.SpreadElement.check(property.node)) {
return `...${toString(property.get('argument'))}`;
} else {
return (
toString(property.get('key')) +
': ' +
toString(property.get('value'))
);
}
});
result.push('{' + properties.join(', ') + '}');
continue;
Expand Down

0 comments on commit 0c3c38f

Please sign in to comment.