babel/babylon

Pattern matching - Stage 0

Closed this issue · 3 comments

xtuc commented

This issue is to keep track of the implementation in the parser of this proposal.

Champions: Brian Terlson (Microsoft, @bterlson), Sebastian Markbåge (Facebook, @sebmarkbage)
Spec Repo: https://github.com/tc39/proposal-pattern-matching

Remember that proposals are just that; subject to change until Stage 4!

Examples

let length = vector => match (vector) {
    { x, y, z }: Math.sqrt(x ** 2 + y ** 2 + z ** 2),
    { x, y }:   Math.sqrt(x ** 2 + y ** 2),
    [...]:      vector.length,
    else: {
        throw new Error("Unknown vector type");
    }
}
let isVerbose = config => match (config) {
    { output: { verbose: true } }: true,
    else: false
}

let outOfBoundsPoint = p => match (p) {
    { x > 100, y }: true,
    { x, y > 100 }: true,
    else: false
}

You can find more example in the spec repo.

Parsing

  • Babylon plugin: patternMatching
  • Babel AST changes
xtuc commented

@babel-bot move to babel/proposals

hzoo commented

Hey @xtuc! I've moved your issue to the correct repository. Please make sure to keep an eye on the new issue for the latest information.