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

Support for ??? "Empty Coalesce" operator #256

Open
acalvino4 opened this issue Aug 26, 2022 · 1 comment
Open

Support for ??? "Empty Coalesce" operator #256

acalvino4 opened this issue Aug 26, 2022 · 1 comment

Comments

@acalvino4
Copy link

My issue is exactly what Kyle says here in the context of prettier's twig formatter: trivago/prettier-plugin-twig-melody#47 (comment)

Is a custom syntax many people use in Craft, and while it makes sense to throw an error since it's not official twig syntax, I am wondering whether it is possible to, in preference order:
a) include support anyway
b) personally create a custom rule that makes this ok
c) disable twigcs for these lines

I understand (a) probably doesn't make sense until twig officially supports it. But I thought I'd put it out there since it probably wouldn't hurt anything either. (b) would be great too, but I'm not sure if it's possible. I would rather not do (c) though it's better than nothing, and I'm not sure if it is supported either.

As a side note, if (c) is not possible, I would think it an important feature request!

@acalvino4
Copy link
Author

I got a chance to look into this more.

Looks like all we'd need for option (a) is to modify RulesetBuilder.php:

        $ops = $this->using(self::OP_VARS, [
            ...
            ['$➀\?\?\?➁$', $this->binaryOpSpace('???')], // Add this, must appear before following line
            ['$➀\?\?➁$', $this->binaryOpSpace('??')],
            ...
        ];

For people who don't use that syntax, it wouldn't hurt them, though I understand reluctance to include unofficial syntax in an official ruleset. But I'm curious if I could get a maintainer's thoughts. If you're not opposed, I'll submit a PR.

In the meantime, I was able to extend the RegEngineRule rule using a custom ruleset builder to get what I need, though it's more convoluted than I would have hoped, and am wondering if we might consider making an easier way to add new operators to the list.

class CustomRulesetBuilder extends RulesetBuilder {
    public function build(): array
    {
        $newOps = $this->using(self::OP_VARS, [['$➀\?\?\?➁$', $this->binaryOpSpace('???')]]);

        $build = parent::build();
        $expr = $build['expr'];
        $build['expr'] = array_merge(
            $newOps,
            $expr,
        );
        return $build;
    }
}

class CustomRuleset implements RulesetInterface
{
    private int $twigMajorVersion;

    public function __construct(int $twigMajorVersion)
    {
        $this->twigMajorVersion = $twigMajorVersion;
    }

    public function getRules()
    {
        $configurator = new RulesetConfigurator();
        $configurator->setTwigMajorVersion($this->twigMajorVersion);
        $builder = new CustomRulesetBuilder($configurator);

        return [
            new Rule\RegEngineRule(Violation::SEVERITY_ERROR, $builder->build()),
            new Rule\TrailingSpace(Violation::SEVERITY_ERROR),
            new Rule\UnusedMacro(Violation::SEVERITY_WARNING, $this->resolver),
            new Rule\UnusedVariable(Violation::SEVERITY_WARNING, $this->resolver),
        ];
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant