ast-grep

Is it possible to make the rule match only the outermost obj and not the ones inside it?


Is it possible to make the rule match only the outermost obj and not the ones inside it?, I read the ast-grep documentation, and it seems it can be achieved using stopBy. this is a playground of rule

expect: 2 match

// match 1
{
    a: 1,
    b: {
        c: 1
    }
}

// match 2
{
    d: 1,
    e: {
        f: 1
    },
    g: {
        h: 0
    }
}

Solution

  • You can use not + inside + stopBy: end.

    rule:
      kind: object
      pattern: $A
      not:
        inside:
          kind: object
          stopBy: end
    

    Example Playground