-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeEditor] add support of triple quotes (#112656)
* [CodeEditor] add support of triple quotes * add tests for grammar * an escaped quote can be appended to the end of triple quotes' Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
- Loading branch information
1 parent
31e7428
commit 079fbce
Showing
7 changed files
with
274 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { createParser } from './grammar'; | ||
|
||
describe('createParser', () => { | ||
let parser: ReturnType<typeof createParser>; | ||
|
||
beforeEach(() => { | ||
parser = createParser(); | ||
}); | ||
|
||
test('should create a xjson grammar parser', () => { | ||
expect(createParser()).toBeInstanceOf(Function); | ||
}); | ||
|
||
test('should return no annotations in case of valid json', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": "file", | ||
"value": "File", | ||
"quotes": "'\\"", | ||
"popup": { | ||
"actions": [ | ||
"new", | ||
"open", | ||
"close" | ||
], | ||
"menuitem": [ | ||
{"value": "New"}, | ||
{"value": "Open"}, | ||
{"value": "Close"} | ||
] | ||
} | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [], | ||
} | ||
`); | ||
}); | ||
|
||
test('should support triple quotes', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": """ | ||
file | ||
""", | ||
"value": "File" | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [], | ||
} | ||
`); | ||
}); | ||
|
||
test('triple quotes should be correctly closed', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": """" | ||
file | ||
"", | ||
"value": "File" | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [ | ||
Object { | ||
"at": 36, | ||
"text": "Expected ',' instead of '\\"'", | ||
"type": "error", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
test('an escaped quote can be appended to the end of triple quotes', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": """ | ||
file | ||
\\"""", | ||
"value": "File" | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [], | ||
} | ||
`); | ||
}); | ||
|
||
test('text values should be wrapper into quotes', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": id, | ||
"value": "File" | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [ | ||
Object { | ||
"at": 36, | ||
"text": "Unexpected 'i'", | ||
"type": "error", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
test('check for close quotes', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": "id, | ||
"value": "File" | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [ | ||
Object { | ||
"at": 52, | ||
"text": "Expected ',' instead of 'v'", | ||
"type": "error", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
test('no duplicate keys', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": "id", | ||
"id": "File" | ||
}} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [ | ||
Object { | ||
"at": 53, | ||
"text": "Duplicate key \\"id\\"", | ||
"type": "warning", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
|
||
test('all curly quotes should be closed', () => { | ||
expect( | ||
parser(` | ||
{"menu": { | ||
"id": "id", | ||
"name": "File" | ||
} | ||
`) | ||
).toMatchInlineSnapshot(` | ||
Object { | ||
"annotations": Array [ | ||
Object { | ||
"at": 82, | ||
"text": "Expected ',' instead of ''", | ||
"type": "error", | ||
}, | ||
], | ||
} | ||
`); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.