From c2ec63d4fd9faffcf95fc0318f5939eac5c4da7d Mon Sep 17 00:00:00 2001 From: Tom PERRILLAT-COLLOMB Date: Wed, 25 Jan 2023 23:16:06 +0100 Subject: [PATCH] feat(er): allow leading underscore for attributes name --- .../src/diagrams/er/parser/erDiagram.jison | 4 +-- .../src/diagrams/er/parser/erDiagram.spec.js | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/packages/mermaid/src/diagrams/er/parser/erDiagram.jison b/packages/mermaid/src/diagrams/er/parser/erDiagram.jison index 15ca0c72f..1e3972a3b 100644 --- a/packages/mermaid/src/diagrams/er/parser/erDiagram.jison +++ b/packages/mermaid/src/diagrams/er/parser/erDiagram.jison @@ -32,7 +32,7 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multili \s+ /* skip whitespace in block */ \b((?:PK)|(?:FK)|(?:UK))\b return 'ATTRIBUTE_KEY' (.*?)[~](.*?)*[~] return 'ATTRIBUTE_WORD'; -[A-Za-z][A-Za-z0-9\-_\[\]\(\)]* return 'ATTRIBUTE_WORD' +[A-Za-z_][A-Za-z0-9\-_\[\]\(\)]* return 'ATTRIBUTE_WORD' \"[^"]*\" return 'COMMENT'; [\n]+ /* nothing */ "}" { this.popState(); return 'BLOCK_STOP'; } @@ -81,7 +81,7 @@ start document : /* empty */ { $$ = [] } - | document line {$1.push($2);$$ = $1} + | document line {$1.push($2);$$ = $1} ; line diff --git a/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js b/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js index 40ec28ada..ca497a2ac 100644 --- a/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js +++ b/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js @@ -135,6 +135,37 @@ describe('when parsing ER diagram it...', function () { }); }); + describe('attribute name', () => { + it('should allow alphanumeric characters, dashes, underscores and brackets (not leading chars)', function () { + const entity = 'BOOK'; + const attribute1 = 'string myBookTitle'; + const attribute2 = 'string MYBOOKSUBTITLE_1'; + const attribute3 = 'string author-ref[name](1)'; + + erDiagram.parser.parse( + `erDiagram\n${entity} {\n${attribute1}\n${attribute2}\n${attribute3}\n}` + ); + const entities = erDb.getEntities(); + + expect(Object.keys(entities).length).toBe(1); + expect(entities[entity].attributes.length).toBe(3); + expect(entities[entity].attributes[0].attributeName).toBe('myBookTitle'); + expect(entities[entity].attributes[1].attributeName).toBe('MYBOOKSUBTITLE_1'); + expect(entities[entity].attributes[2].attributeName).toBe('author-ref[name](1)'); + }); + + it('should not allow leading numbers, dashes or brackets', function () { + const entity = 'BOOK'; + const nonLeadingChars = '0-[]()'; + [...nonLeadingChars].forEach((nonLeadingChar) => { + expect(() => { + const attribute = `string ${nonLeadingChar}author`; + erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute}\n}`); + }).toThrow(); + }); + }); + }); + it('should allow an entity with a single attribute to be defined', function () { const entity = 'BOOK'; const attribute = 'string title';