Skip to content
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

Feature/add comment in ast #2471

Open
wants to merge 2 commits 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
32 changes: 14 additions & 18 deletions src/language/__tests__/lexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ describe('Lexer', () => {
'{ kind: "Name", value: "foo", line: 1, column: 1 }',
);
});

it('skips whitespace and comments', () => {
it('skips whitespace and commas', () => {
expect(
lexOne(`

Expand All @@ -144,18 +143,6 @@ describe('Lexer', () => {
value: 'foo',
});

expect(
lexOne(`
#comment
foo#comment
`),
).to.contain({
kind: TokenKind.NAME,
start: 18,
end: 21,
value: 'foo',
});

expect(lexOne(',,,foo,,,')).to.contain({
kind: TokenKind.NAME,
start: 3,
Expand All @@ -181,7 +168,6 @@ describe('Lexer', () => {
4 |
`);
});

it('updates line numbers in error for file context', () => {
let caughtError;
try {
Expand Down Expand Up @@ -218,6 +204,19 @@ describe('Lexer', () => {
| ^
`);
});
it('lexes comments', () => {
expect(
lexOne(
dedent`#this is a comment
a{}`,
),
).to.contain({
kind: TokenKind.COMMENT,
start: 0,
end: 18,
value: 'this is a comment',
});
});

it('lexes strings', () => {
expect(lexOne('""')).to.contain({
Expand Down Expand Up @@ -877,9 +876,6 @@ describe('Lexer', () => {
let endToken;
do {
endToken = lexer.advance();
// Lexer advances over ignored comment tokens to make writing parsers
// easier, but will include them in the linked list result.
expect(endToken.kind).to.not.equal(TokenKind.COMMENT);
} while (endToken.kind !== TokenKind.EOF);

expect(startToken.prev).to.equal(null);
Expand Down
79 changes: 79 additions & 0 deletions src/language/__tests__/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,83 @@ describe('Parser', () => {
);
});

it('Add single comments in AST', () => {
const ast = parse(dedent`
#This comment has a \u0A0A multi-byte character.
type alpha{ field(arg: string):string }
`);

expect(toJSONDeep(ast.comments)).to.deep.equal([
{
kind: 'Comment',
loc: { start: 0, end: 43 },
value: 'This comment has a ਊ multi-byte character.',
},
]);
});

it('Ignore comments that comes when we peek for a token in AST', () => {
const ast = parse(dedent`
type #This is a comment that gets ignored
alpha{ field(arg: string):string }
`);

expect(toJSONDeep(ast.comments)).to.deep.equal([
{
kind: 'Comment',
loc: {
end: 41,
start: 5,
},
value: 'This is a comment that gets ignored',
},
]);
});

it('Add empty comments from in AST', () => {
const ast = parse(dedent`
#
type alpha{ field(arg: string):string }
`);

expect(toJSONDeep(ast.comments)).to.deep.equal([
{
kind: 'Comment',
loc: { start: 0, end: 1 },
value: '',
},
]);
});

it('Add multiple comments in AST', () => {
const ast = parse(dedent`
#This is top comment
type alpha{
#This comment is demo comment.
field(arg: string):string
#This is another demo comment having # inside
}
`);

expect(toJSONDeep(ast.comments)).to.deep.equal([
{
kind: 'Comment',
loc: { start: 0, end: 20 },
value: 'This is top comment',
},
{
kind: 'Comment',
loc: { start: 35, end: 65 },
value: 'This comment is demo comment.',
},
{
kind: 'Comment',
loc: { start: 97, end: 142 },
value: 'This is another demo comment having # inside',
},
]);
});

it('parses kitchen sink', () => {
expect(() => parse(kitchenSinkQuery)).to.not.throw();
});
Expand Down Expand Up @@ -231,6 +308,7 @@ describe('Parser', () => {

expect(toJSONDeep(result)).to.deep.equal({
kind: Kind.DOCUMENT,
comments: [],
loc: { start: 0, end: 41 },
definitions: [
{
Expand Down Expand Up @@ -321,6 +399,7 @@ describe('Parser', () => {

expect(toJSONDeep(result)).to.deep.equal({
kind: Kind.DOCUMENT,
comments: [],
loc: { start: 0, end: 30 },
definitions: [
{
Expand Down
29 changes: 29 additions & 0 deletions src/language/__tests__/schema-parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -176,6 +177,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeExtension',
Expand All @@ -201,6 +203,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeExtension',
Expand All @@ -219,6 +222,7 @@ describe('Schema Parser', () => {
const doc = parse('extend interface Hello implements Greeting');
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InterfaceTypeExtension',
Expand All @@ -242,6 +246,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeExtension',
Expand Down Expand Up @@ -304,6 +309,7 @@ describe('Schema Parser', () => {
`);
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InterfaceTypeExtension',
Expand Down Expand Up @@ -376,6 +382,7 @@ describe('Schema Parser', () => {
const doc = parse(body);
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'SchemaExtension',
Expand All @@ -400,6 +407,7 @@ describe('Schema Parser', () => {
const doc = parse(body);
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'SchemaExtension',
Expand Down Expand Up @@ -442,6 +450,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -471,6 +480,7 @@ describe('Schema Parser', () => {
const doc = parse('interface Hello implements World { field: String }');
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InterfaceTypeDefinition',
Expand All @@ -497,6 +507,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand All @@ -523,6 +534,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -551,6 +563,7 @@ describe('Schema Parser', () => {
const doc = parse('interface Hello implements Wo & rld { field: String }');
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InterfaceTypeDefinition',
Expand Down Expand Up @@ -580,6 +593,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -610,6 +624,7 @@ describe('Schema Parser', () => {
);
expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InterfaceTypeDefinition',
Expand Down Expand Up @@ -639,6 +654,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'EnumTypeDefinition',
Expand All @@ -658,6 +674,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'EnumTypeDefinition',
Expand All @@ -684,6 +701,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InterfaceTypeDefinition',
Expand Down Expand Up @@ -714,6 +732,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -752,6 +771,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -794,6 +814,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -836,6 +857,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ObjectTypeDefinition',
Expand Down Expand Up @@ -876,6 +898,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'UnionTypeDefinition',
Expand All @@ -895,6 +918,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'UnionTypeDefinition',
Expand All @@ -917,6 +941,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'UnionTypeDefinition',
Expand Down Expand Up @@ -967,6 +992,7 @@ describe('Schema Parser', () => {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'ScalarTypeDefinition',
Expand All @@ -988,6 +1014,7 @@ input Hello {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'InputObjectTypeDefinition',
Expand Down Expand Up @@ -1026,6 +1053,7 @@ input Hello {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'DirectiveDefinition',
Expand Down Expand Up @@ -1062,6 +1090,7 @@ input Hello {

expect(toJSONDeep(doc)).to.deep.equal({
kind: 'Document',
comments: [],
definitions: [
{
kind: 'DirectiveDefinition',
Expand Down
Loading