Skip to content

Commit

Permalink
Fix false positive on regex flag validation
Browse files Browse the repository at this point in the history
  • Loading branch information
msujew committed Aug 14, 2024
1 parent 48e67d6 commit dba1969
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions packages/langium/src/grammar/validation/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,13 @@ export class LangiumGrammarValidator {
}

checkDirectlyUsedRegexFlags(token: ast.RegexToken, accept: ValidationAcceptor): void {
if (!ast.isTerminalRule(token.$container)) {
const regex = token.regex;
if (!ast.isTerminalRule(token.$container) && regex) {
const slashIndex = regex.lastIndexOf('/');
const flags = regex.substring(slashIndex + 1);
const range = this.getFlagRange(token);
if (range) {
accept('warning', 'Regular expression flags are only applied if the terminal is not a composition', {
if (range && flags) {
accept('warning', 'Regular expression flags are only applied if the terminal is not a composition.', {
node: token,
range
});
Expand Down
18 changes: 18 additions & 0 deletions packages/langium/test/grammar/grammar-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,24 @@ describe('Langium grammar validation', () => {
const validationResult = await validate(grammar);
expectNoIssues(validationResult);
});

test('Composite terminal regex flags', async () => {
const grammar = `
terminal Test: 'Test' /test/i;
`;
const validationResult = await validate(grammar);
expectWarning(validationResult, 'Regular expression flags are only applied if the terminal is not a composition.', {
node: undefined
});
});

test('Composite terminal regex flags - negative', async () => {
const grammar = `
terminal Test: /test/i;
`;
const validationResult = await validate(grammar);
expectNoIssues(validationResult);
});
});

describe('Data type rule return type', () => {
Expand Down

0 comments on commit dba1969

Please sign in to comment.