Skip to content

Commit

Permalink
fix: 馃悰 unexpected directive match occur on inline directive
Browse files Browse the repository at this point in the history
  • Loading branch information
shufo committed Apr 3, 2022
1 parent 56e444c commit 854be88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
22 changes: 22 additions & 0 deletions __tests__/formatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2885,6 +2885,28 @@ describe('formatter', () => {
``,
].join('\n');

await util.doubleFormatCheck(content, expected);
});
test('directive inside component attribute', async () => {
const content = [
`@section('body')`,
` <x-alert :live="@env('production')" />`,
`@endsection`,
`<x-button ::class="{ danger: [1, 2, 3] }">`,
` Submit`,
`</x-button>`,
].join('\n');

const expected = [
`@section('body')`,
` <x-alert :live="@env('production')" />`,
`@endsection`,
`<x-button ::class="{ danger: [1, 2, 3] }">`,
` Submit`,
`</x-button>`,
``,
].join('\n');

await util.doubleFormatCheck(content, expected);
});
});
10 changes: 7 additions & 3 deletions src/formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import {
inlineFunctionTokens,
optionalStartWithoutEndTokens,
conditionalTokens,
directivePrefix,
indentStartTokensWithoutPrefix,
} from './indent';
import { nestedParenthesisRegex } from './regex';

Expand Down Expand Up @@ -254,10 +256,10 @@ export default class Formatter {
preserveInlineDirective(content: string): string {
// preserve inline directives inside html tag
const regex = new RegExp(
`(<[\\w]+?[^>]*?)(${indentStartTokens.join(
`(<[\\w]+?[^>]*?)${directivePrefix}(${indentStartTokensWithoutPrefix.join(
'|',
// eslint-disable-next-line max-len
)})(\\s*?)(${nestedParenthesisRegex})(.*?)(${indentEndTokens.join('|')})(.*?>)`,
)})(\\s*?)(${nestedParenthesisRegex})(.*?)(@end\\2)(.*?>)`,
'gims',
);
const replaced = _.replace(
Expand All @@ -274,7 +276,9 @@ export default class Formatter {
p7: string,
p8: string,
) => {
return `${p1}${this.storeInlineDirective(`${p2.trim()}${p3}${p4.trim()} ${p6.trim()} ${p7.trim()}`)}${p8}`;
return `${p1}${this.storeInlineDirective(
`${directivePrefix}${p2.trim()}${p3}${p4.trim()} ${p6.trim()} ${p7.trim()}`,
)}${p8}`;
},
);

Expand Down
4 changes: 4 additions & 0 deletions src/indent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import _ from 'lodash';

export const directivePrefix = '@';

export const indentStartTokens = [
'@alert',
'@push',
Expand Down Expand Up @@ -38,6 +40,8 @@ export const indentStartTokens = [
'@section',
];

export const indentStartTokensWithoutPrefix = _.map(indentStartTokens, (token) => token.substring(1));

export const indentEndTokens = [
'@endalert',
'@endpush',
Expand Down

0 comments on commit 854be88

Please sign in to comment.