Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

TSMappedType - TSTypeParameter.name issue #109

Closed
@JamesHenry

Description

@JamesHenry

Considering the following source:

type C = {
    [/* commentC */ c in C]: string
}

Prior to v8 we didn't have any test coverage for mapped types.

@armano2 added good test coverage, and at the same time explicit conversion logic (rather than using the deeplyCopy() fallback which had implicitly been used up until that point) in the PR which would become v8.

Because of not having the test coverage in there prior to the code change, we didn't explicitly point out a change which is probably worthy of discussion:

The lowercase c (TSTypeParameter.name) was previously an Identifier - now it is just a string value (i.e. it has no node in the AST).

I discovered this when trying to update Prettier here to the latest: prettier/prettier#5728

The final failing test case is around prettier being unable to attach the comment /* commentC */ in the way it expects. Naturally this is because it is trying to attached it to the TSTypeParameter.name, which used to be a node, and no longer is.

Without this being reinstated to an Identifier node, I'm not sure how we could preserve Prettier's behaviour here?

We are not ignoring the new mapped type test sources in the ast-alignment-tests, so we must be aligned with the babel AST here, but maybe they both need to change in this case?

Would love to know your thoughts: @uniqueiniquity @armano2 @ikatyang

Full ASTs in each case:

Before v8 (c is an Identifier)
{
    "body": [
        {
            "id": {
                "loc": {
                    "end": {
                        "column": 6,
                        "line": 1
                    },
                    "start": {
                        "column": 5,
                        "line": 1
                    }
                },
                "name": "C",
                "range": [
                    5,
                    6
                ],
                "type": "Identifier"
            },
            "loc": {
                "end": {
                    "column": 1,
                    "line": 3
                },
                "start": {
                    "column": 0,
                    "line": 1
                }
            },
            "range": [
                0,
                48
            ],
            "type": "TSTypeAliasDeclaration",
            "typeAnnotation": {
                "loc": {
                    "end": {
                        "column": 1,
                        "line": 3
                    },
                    "start": {
                        "column": 9,
                        "line": 1
                    }
                },
                "range": [
                    9,
                    48
                ],
                "type": "TSMappedType",
                "typeAnnotation": {
                    "loc": {
                        "end": {
                            "column": 35,
                            "line": 2
                        },
                        "start": {
                            "column": 27,
                            "line": 2
                        }
                    },
                    "range": [
                        38,
                        46
                    ],
                    "type": "TSTypeAnnotation",
                    "typeAnnotation": {
                        "loc": {
                            "end": {
                                "column": 35,
                                "line": 2
                            },
                            "start": {
                                "column": 29,
                                "line": 2
                            }
                        },
                        "range": [
                            40,
                            46
                        ],
                        "type": "TSStringKeyword"
                    }
                },
                "typeParameter": {
                    "constraint": {
                        "loc": {
                            "end": {
                                "column": 26,
                                "line": 2
                            },
                            "start": {
                                "column": 25,
                                "line": 2
                            }
                        },
                        "range": [
                            36,
                            37
                        ],
                        "type": "TSTypeReference",
                        "typeName": {
                            "loc": {
                                "end": {
                                    "column": 26,
                                    "line": 2
                                },
                                "start": {
                                    "column": 25,
                                    "line": 2
                                }
                            },
                            "name": "C",
                            "range": [
                                36,
                                37
                            ],
                            "type": "Identifier"
                        }
                    },
                    "loc": {
                        "end": {
                            "column": 26,
                            "line": 2
                        },
                        "start": {
                            "column": 20,
                            "line": 2
                        }
                    },
                    "name": {
                        "loc": {
                            "end": {
                                "column": 21,
                                "line": 2
                            },
                            "start": {
                                "column": 20,
                                "line": 2
                            }
                        },
                        "name": "c",
                        "range": [
                            31,
                            32
                        ],
                        "type": "Identifier"
                    },
                    "range": [
                        31,
                        37
                    ],
                    "type": "TSTypeParameter"
                }
            }
        }
    ],
    "loc": {
        "end": {
            "column": 1,
            "line": 3
        },
        "start": {
            "column": 0,
            "line": 1
        }
    },
    "range": [
        0,
        48
    ],
    "sourceType": "script",
    "tokens": [
        {
            "loc": {
                "end": {
                    "column": 4,
                    "line": 1
                },
                "start": {
                    "column": 0,
                    "line": 1
                }
            },
            "range": [
                0,
                4
            ],
            "type": "Identifier",
            "value": "type"
        },
        {
            "loc": {
                "end": {
                    "column": 6,
                    "line": 1
                },
                "start": {
                    "column": 5,
                    "line": 1
                }
            },
            "range": [
                5,
                6
            ],
            "type": "Identifier",
            "value": "C"
        },
        {
            "loc": {
                "end": {
                    "column": 8,
                    "line": 1
                },
                "start": {
                    "column": 7,
                    "line": 1
                }
            },
            "range": [
                7,
                8
            ],
            "type": "Punctuator",
            "value": "="
        },
        {
            "loc": {
                "end": {
                    "column": 10,
                    "line": 1
                },
                "start": {
                    "column": 9,
                    "line": 1
                }
            },
            "range": [
                9,
                10
            ],
            "type": "Punctuator",
            "value": "{"
        },
        {
            "loc": {
                "end": {
                    "column": 5,
                    "line": 2
                },
                "start": {
                    "column": 4,
                    "line": 2
                }
            },
            "range": [
                15,
                16
            ],
            "type": "Punctuator",
            "value": "["
        },
        {
            "loc": {
                "end": {
                    "column": 21,
                    "line": 2
                },
                "start": {
                    "column": 20,
                    "line": 2
                }
            },
            "range": [
                31,
                32
            ],
            "type": "Identifier",
            "value": "c"
        },
        {
            "loc": {
                "end": {
                    "column": 24,
                    "line": 2
                },
                "start": {
                    "column": 22,
                    "line": 2
                }
            },
            "range": [
                33,
                35
            ],
            "type": "Keyword",
            "value": "in"
        },
        {
            "loc": {
                "end": {
                    "column": 26,
                    "line": 2
                },
                "start": {
                    "column": 25,
                    "line": 2
                }
            },
            "range": [
                36,
                37
            ],
            "type": "Identifier",
            "value": "C"
        },
        {
            "loc": {
                "end": {
                    "column": 27,
                    "line": 2
                },
                "start": {
                    "column": 26,
                    "line": 2
                }
            },
            "range": [
                37,
                38
            ],
            "type": "Punctuator",
            "value": "]"
        },
        {
            "loc": {
                "end": {
                    "column": 28,
                    "line": 2
                },
                "start": {
                    "column": 27,
                    "line": 2
                }
            },
            "range": [
                38,
                39
            ],
            "type": "Punctuator",
            "value": ":"
        },
        {
            "loc": {
                "end": {
                    "column": 35,
                    "line": 2
                },
                "start": {
                    "column": 29,
                    "line": 2
                }
            },
            "range": [
                40,
                46
            ],
            "type": "Identifier",
            "value": "string"
        },
        {
            "loc": {
                "end": {
                    "column": 1,
                    "line": 3
                },
                "start": {
                    "column": 0,
                    "line": 3
                }
            },
            "range": [
                47,
                48
            ],
            "type": "Punctuator",
            "value": "}"
        }
    ],
    "type": "Program"
}

Current (c is a string value)
{
    "body": [
        {
            "id": {
                "loc": {
                    "end": {
                        "column": 6,
                        "line": 1
                    },
                    "start": {
                        "column": 5,
                        "line": 1
                    }
                },
                "name": "C",
                "range": [
                    5,
                    6
                ],
                "type": "Identifier"
            },
            "loc": {
                "end": {
                    "column": 1,
                    "line": 3
                },
                "start": {
                    "column": 0,
                    "line": 1
                }
            },
            "range": [
                0,
                48
            ],
            "type": "TSTypeAliasDeclaration",
            "typeAnnotation": {
                "loc": {
                    "end": {
                        "column": 1,
                        "line": 3
                    },
                    "start": {
                        "column": 9,
                        "line": 1
                    }
                },
                "range": [
                    9,
                    48
                ],
                "type": "TSMappedType",
                "typeAnnotation": {
                    "loc": {
                        "end": {
                            "column": 35,
                            "line": 2
                        },
                        "start": {
                            "column": 29,
                            "line": 2
                        }
                    },
                    "range": [
                        40,
                        46
                    ],
                    "type": "TSStringKeyword"
                },
                "typeParameter": {
                    "constraint": {
                        "loc": {
                            "end": {
                                "column": 26,
                                "line": 2
                            },
                            "start": {
                                "column": 25,
                                "line": 2
                            }
                        },
                        "range": [
                            36,
                            37
                        ],
                        "type": "TSTypeReference",
                        "typeName": {
                            "loc": {
                                "end": {
                                    "column": 26,
                                    "line": 2
                                },
                                "start": {
                                    "column": 25,
                                    "line": 2
                                }
                            },
                            "name": "C",
                            "range": [
                                36,
                                37
                            ],
                            "type": "Identifier"
                        }
                    },
                    "loc": {
                        "end": {
                            "column": 26,
                            "line": 2
                        },
                        "start": {
                            "column": 20,
                            "line": 2
                        }
                    },
                    "name": "c",
                    "range": [
                        31,
                        37
                    ],
                    "type": "TSTypeParameter"
                }
            }
        }
    ],
    "loc": {
        "end": {
            "column": 1,
            "line": 3
        },
        "start": {
            "column": 0,
            "line": 1
        }
    },
    "range": [
        0,
        48
    ],
    "sourceType": "script",
    "tokens": [
        {
            "loc": {
                "end": {
                    "column": 4,
                    "line": 1
                },
                "start": {
                    "column": 0,
                    "line": 1
                }
            },
            "range": [
                0,
                4
            ],
            "type": "Identifier",
            "value": "type"
        },
        {
            "loc": {
                "end": {
                    "column": 6,
                    "line": 1
                },
                "start": {
                    "column": 5,
                    "line": 1
                }
            },
            "range": [
                5,
                6
            ],
            "type": "Identifier",
            "value": "C"
        },
        {
            "loc": {
                "end": {
                    "column": 8,
                    "line": 1
                },
                "start": {
                    "column": 7,
                    "line": 1
                }
            },
            "range": [
                7,
                8
            ],
            "type": "Punctuator",
            "value": "="
        },
        {
            "loc": {
                "end": {
                    "column": 10,
                    "line": 1
                },
                "start": {
                    "column": 9,
                    "line": 1
                }
            },
            "range": [
                9,
                10
            ],
            "type": "Punctuator",
            "value": "{"
        },
        {
            "loc": {
                "end": {
                    "column": 5,
                    "line": 2
                },
                "start": {
                    "column": 4,
                    "line": 2
                }
            },
            "range": [
                15,
                16
            ],
            "type": "Punctuator",
            "value": "["
        },
        {
            "loc": {
                "end": {
                    "column": 21,
                    "line": 2
                },
                "start": {
                    "column": 20,
                    "line": 2
                }
            },
            "range": [
                31,
                32
            ],
            "type": "Identifier",
            "value": "c"
        },
        {
            "loc": {
                "end": {
                    "column": 24,
                    "line": 2
                },
                "start": {
                    "column": 22,
                    "line": 2
                }
            },
            "range": [
                33,
                35
            ],
            "type": "Keyword",
            "value": "in"
        },
        {
            "loc": {
                "end": {
                    "column": 26,
                    "line": 2
                },
                "start": {
                    "column": 25,
                    "line": 2
                }
            },
            "range": [
                36,
                37
            ],
            "type": "Identifier",
            "value": "C"
        },
        {
            "loc": {
                "end": {
                    "column": 27,
                    "line": 2
                },
                "start": {
                    "column": 26,
                    "line": 2
                }
            },
            "range": [
                37,
                38
            ],
            "type": "Punctuator",
            "value": "]"
        },
        {
            "loc": {
                "end": {
                    "column": 28,
                    "line": 2
                },
                "start": {
                    "column": 27,
                    "line": 2
                }
            },
            "range": [
                38,
                39
            ],
            "type": "Punctuator",
            "value": ":"
        },
        {
            "loc": {
                "end": {
                    "column": 35,
                    "line": 2
                },
                "start": {
                    "column": 29,
                    "line": 2
                }
            },
            "range": [
                40,
                46
            ],
            "type": "Identifier",
            "value": "string"
        },
        {
            "loc": {
                "end": {
                    "column": 1,
                    "line": 3
                },
                "start": {
                    "column": 0,
                    "line": 3
                }
            },
            "range": [
                47,
                48
            ],
            "type": "Punctuator",
            "value": "}"
        }
    ],
    "type": "Program"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions