Skip to content

feat: support laravel/pennant @feature and @featureany directives #120

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion formatter/plugin.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/analyzers/pairManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export class PairManager {
'canany',
'hasSection',
'production',
'feature',
'featureany',
];

public static customIfs: Map<string, number> = new Map();
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/compilers/endFeature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AbstractNode } from '../../nodes/nodes.js';
import { NodeCompiler } from '../nodeCompiler.js';

export class EndFeatureCompiler implements NodeCompiler {
compile(node: AbstractNode): string {
return '<?php endif; ?>';
}
}
8 changes: 8 additions & 0 deletions src/compiler/compilers/endFeatureAny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AbstractNode } from '../../nodes/nodes.js';
import { NodeCompiler } from '../nodeCompiler.js';

export class EndFeatureAnyCompiler implements NodeCompiler {
compile(node: AbstractNode): string {
return '<?php endif; ?>';
}
}
10 changes: 10 additions & 0 deletions src/compiler/compilers/feature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AbstractNode, DirectiveNode } from '../../nodes/nodes.js';
import { NodeCompiler } from '../nodeCompiler.js';

export class FeatureCompiler implements NodeCompiler {
compile(node: AbstractNode): string {
const directive = node as DirectiveNode;

return '<?php if(Laravel\\Pennant\\Feature::active(' + directive.getInnerContent() + ')): ?>'
}
}
10 changes: 10 additions & 0 deletions src/compiler/compilers/featureAny.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AbstractNode, DirectiveNode } from '../../nodes/nodes.js';
import { NodeCompiler } from '../nodeCompiler.js';

export class FeatureAnyCompiler implements NodeCompiler {
compile(node: AbstractNode): string {
const directive = node as DirectiveNode;

return '<?php if(Laravel\\Pennant\\Feature::someAreActive(' + directive.getInnerContent() + ')): ?>'
}
}
8 changes: 8 additions & 0 deletions src/compiler/compilers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ import { EndComponentCompiler } from './endComponent.js';
import { SlotCompiler } from './slot.js';
import { EndSlotCompiler } from './endSlot.js';
import { EndComponentClassCompiler } from './endComponentClass.js';
import { FeatureCompiler } from './feature.js';
import { EndFeatureCompiler } from './endFeature.js';
import { FeatureAnyCompiler } from './featureAny.js';
import { EndFeatureAnyCompiler } from './endFeatureAny.js';

export {
AppendCompiler,
Expand Down Expand Up @@ -190,4 +194,8 @@ export {
SlotCompiler,
EndSlotCompiler,
EndComponentClassCompiler,
FeatureCompiler,
EndFeatureCompiler,
FeatureAnyCompiler,
EndFeatureAnyCompiler,
}
4 changes: 4 additions & 0 deletions src/compiler/phpCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export class PhpCompiler {
this.registerCompiler('slot', new Compilers.SlotCompiler());
this.registerCompiler('endslot', new Compilers.EndSlotCompiler());
this.registerCompiler('endcomponentClass', new Compilers.EndComponentClassCompiler(componentCompiler));
this.registerCompiler('feature', new Compilers.FeatureCompiler());
this.registerCompiler('endfeature', new Compilers.EndFeatureCompiler());
this.registerCompiler('featureany', new Compilers.FeatureAnyCompiler());
this.registerCompiler('endfeatureany', new Compilers.EndFeatureAnyCompiler());
}

setCompileComponentTagBlade(compileBlade: boolean) {
Expand Down
15 changes: 15 additions & 0 deletions src/test/compiler_feature.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert';
import { PhpCompiler } from '../compiler/phpCompiler.js';

suite('Blade Feature Statements', () => {
test('feature statements are compiled', () => {
assert.strictEqual(
PhpCompiler.compileString(`@feature("api")
breeze
@endfeature`),
`<?php if(Laravel\\Pennant\\Feature::active("api")): ?>
breeze
<?php endif; ?>`
);
});
});
15 changes: 15 additions & 0 deletions src/test/compiler_featureany.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import assert from 'assert';
import { PhpCompiler } from '../compiler/phpCompiler.js';

suite('Blade Feature Statements', () => {
test('featureany statements are compiled', () => {
assert.strictEqual(
PhpCompiler.compileString(`@featureany(["api-v1","api-v2"])
breeze
@endfeatureany`),
`<?php if(Laravel\\Pennant\\Feature::someAreActive(["api-v1","api-v2"])): ?>
breeze
<?php endif; ?>`
);
});
});
22 changes: 22 additions & 0 deletions src/test/formatter_directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,28 @@ asdf
assert.strictEqual(await formatBladeString(input), out);
});

test('it pairs feature', async () => {
const input = `@feature("api")
<div></div>
@endfeature`
const out = `@feature("api")
<div></div>
@endfeature
`;
assert.strictEqual(await formatBladeString(input), out);
});

test('it pairs featureany', async () => {
const input = `@featureany(["api-v1","api-v2"])
<div></div>
@endfeatureany`
const out = `@featureany(["api-v1", "api-v2"])
<div></div>
@endfeatureany
`;
assert.strictEqual(await formatBladeString(input), out);
});

test('it pairs env', async () => {
const input = `@env
<div></div>
Expand Down
22 changes: 22 additions & 0 deletions src/test/formatter_pint_directives.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,28 @@ asdf
assert.strictEqual(await formatBladeStringWithPint(input), out);
});

test('pint: it pairs feature', async () => {
const input = `@feature("api")
<div></div>
@endfeature`
const out = `@feature('api')
<div></div>
@endfeature
`;
assert.strictEqual(await formatBladeStringWithPint(input), out);
});

test('pint: it pairs featureany', async () => {
const input = `@featureany(["api-v1","api-v2"])
<div></div>
@endfeatureany`
const out = `@featureany(['api-v1', 'api-v2'])
<div></div>
@endfeatureany
`;
assert.strictEqual(await formatBladeStringWithPint(input), out);
});

test('pint: it pairs env', async () => {
const input = `@env
<div></div>
Expand Down