Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added handleTSIndexedAccessType. #369

Merged
merged 1 commit into from Jul 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/utils/__tests__/getTSType-test.js
Expand Up @@ -315,6 +315,38 @@ describe('getTSType', () => {
});
});

it('detects indexed access', () => {
const typePath = statement(`
var x: A["x"] = 2;

interface A { x: string };
`)
.get('declarations', 0)
.get('id')
.get('typeAnnotation')
.get('typeAnnotation');
expect(getTSType(typePath)).toEqual({
name: 'A["x"]',
raw: 'A["x"]',
});
});

it('resolves indexed access', () => {
const typePath = statement(`
var x: A["x"] = 2;

type A = { x: string };
`)
.get('declarations', 0)
.get('id')
.get('typeAnnotation')
.get('typeAnnotation');
expect(getTSType(typePath)).toEqual({
name: 'string',
raw: 'A["x"]',
});
});

it('resolves types in scope', () => {
const typePath = statement(`
var x: MyType = 2;
Expand Down
36 changes: 36 additions & 0 deletions src/utils/getTSType.js
Expand Up @@ -19,6 +19,7 @@ import getTypeParameters, {
import type {
FlowElementsType,
FlowFunctionArgumentType,
FlowLiteralType,
FlowObjectSignatureType,
FlowSimpleType,
FlowTypeDescriptor,
Expand Down Expand Up @@ -53,6 +54,7 @@ const namedTypes = {
TSTupleType: handleTSTupleType,
TSTypeQuery: handleTSTypeQuery,
TSTypeOperator: handleTSTypeOperator,
TSIndexedAccessType: handleTSIndexedAccessType,
};

function handleTSArrayType(
Expand Down Expand Up @@ -318,6 +320,40 @@ function handleTSTypeOperator(path: NodePath): ?FlowTypeDescriptor {
}
}

function handleTSIndexedAccessType(
path: NodePath,
typeParams: ?TypeParameters,
): FlowSimpleType {
// eslint-disable-next-line no-undef
const objectType: $Shape<FlowObjectSignatureType> = getTSTypeWithResolvedTypes(
path.get('objectType'),
typeParams,
);
// eslint-disable-next-line no-undef
const indexType: $Shape<FlowLiteralType> = getTSTypeWithResolvedTypes(
path.get('indexType'),
typeParams,
);

// We only get the signature if the objectType is a type (vs interface)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not for interfaces?

Copy link
Contributor Author

@thchia thchia Jun 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devongovett actually I'm not 100% sure why, but I believe in my trial and error when defining the objectType as an interface, there is no signature property on it. I'm not sure if it is related to the discussion here?

Edit:

// getTSType.js Line 185

function handleTSInterfaceDeclaration(path: NodePath): FlowSimpleType {
  // Interfaces are handled like references which would be documented separately,
  // rather than inlined like type aliases.
  return {
    name: path.node.id.name,
  };
}

if (!objectType.signature)
return {
name: `${objectType.name}[${indexType.value.toString()}]`,
raw: printValue(path),
};
const resolvedType = objectType.signature.properties.find(p => {
// indexType.value = "'foo'"
return p.key === indexType.value.replace(/['"]+/g, '');
});
if (!resolvedType) {
return { name: 'unknown' };
}
return {
name: resolvedType.value.name,
raw: printValue(path),
};
}

let visitedTypes = {};

function getTSTypeWithResolvedTypes(
Expand Down