Skip to content

Commit

Permalink
Merge pull request #21 from bdo/issue-20
Browse files Browse the repository at this point in the history
feat: docstring prettier format support
  • Loading branch information
jdeniau authored Jun 14, 2024
2 parents 5d90895 + 27bc857 commit 628e42e
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 22 deletions.
41 changes: 20 additions & 21 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -636,37 +636,36 @@ const gherkinAstPrinter: Printer<TypedGherkinNode<GherkinNode>> = {
if (node instanceof TypedDocString) {
const { content, mediaType } = node;

if (mediaType === 'xml') {
return async (textToDoc): Promise<Doc> => {
return [
node.delimiter,
mediaType,
printHardline(),
await textToDoc(content, { ...options, parser: 'html' }),
printHardline(),
node.delimiter,
];
};
}
return async (textToDoc): Promise<Doc|undefined> => {
let doc : doc.builders.Doc | null = null;

if (mediaType) {
// try applying the prettier parser for the media type
doc = await textToDoc(content, { ...options, parser: mediaType }).catch(() => null);

try {
JSON.parse(content);
// if the parser failed for xml, try with the html parser
if (!doc && mediaType === 'xml') {
doc = await textToDoc(content, { ...options, parser: 'html' }).catch(() => null);
}
}

// try applying the json parser
if (!doc) {
doc = await textToDoc(content, { ...options, parser: 'json' }).catch(() => null);
}

return async (textToDoc): Promise<Doc> => {
if (doc) {
return [
node.delimiter,
mediaType ?? '',
printHardline(),
await textToDoc(content, { ...options, parser: 'json' }),
doc,
printHardline(),
node.delimiter,
];
};
} catch (e) {
// igonre non-JSON content
}
}
};
}

return null;
},
};
Expand Down
166 changes: 166 additions & 0 deletions tests/plugin/__snapshots__/jsfmt.spec.mjs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,171 @@ Feature: A data table with PHP Fully Qualified Classnames
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;

exports[`docstring.feature 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Feature: Test different embed
Scenario: XML
When I receive the following feed from the bank:
"""xml
<feed>
<account number="123456789">
<entry>
<type>credit</type>
<amount>1000</amount>
<label>My company salary</label>
</entry>
</account>
</feed>
"""
Scenario: JSON
When I receive the following feed from the bank:
"""json
{ "account":
"1234",
"amount": 1000}
"""
Scenario: JSON if no language is specified (default fallback)
When I receive the following feed from the bank:
"""
{ "account":
"1234",
"amount": 1000}
"""
Scenario: GraphQL
When I make the following query to the API:
"""graphql
union Transaction = CreditTransaction|DebitTransaction
query AccountBalance($accountNumber:String!) {
account(accountNumber:$accountNumber) {
balance
transactions { amount
}
}
}
"""
Scenario: Unknown
When I make the following query to the API:
"""unknown-language
this language is not known from prettier
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
options: {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Feature: Test different embed
Scenario: XML
When I receive the following feed from the bank:
"""xml
<feed>
<account number="123456789">
<entry>
<type>credit</type>
<amount>1000</amount>
<label>My company salary</label>
</entry>
</account>
</feed>
"""
Scenario: JSON
When I receive the following feed from the bank:
"""json
{ "account": "1234", "amount": 1000 }
"""
Scenario: JSON if no language is specified (default fallback)
When I receive the following feed from the bank:
"""
{ "account": "1234", "amount": 1000 }
"""
Scenario: GraphQL
When I make the following query to the API:
"""graphql
union Transaction = CreditTransaction | DebitTransaction
query AccountBalance($accountNumber: String!) {
account(accountNumber: $accountNumber) {
balance
transactions {
amount
}
}
}
"""
Scenario: Unknown
When I make the following query to the API:
"""unknown-language
this language is not known from prettier
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
options: {
"forceNewlineBetweenStepBlocks": true
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Feature: Test different embed
Scenario: XML
When I receive the following feed from the bank:
"""xml
<feed>
<account number="123456789">
<entry>
<type>credit</type>
<amount>1000</amount>
<label>My company salary</label>
</entry>
</account>
</feed>
"""
Scenario: JSON
When I receive the following feed from the bank:
"""json
{ "account": "1234", "amount": 1000 }
"""
Scenario: JSON if no language is specified (default fallback)
When I receive the following feed from the bank:
"""
{ "account": "1234", "amount": 1000 }
"""
Scenario: GraphQL
When I make the following query to the API:
"""graphql
union Transaction = CreditTransaction | DebitTransaction
query AccountBalance($accountNumber: String!) {
account(accountNumber: $accountNumber) {
balance
transactions {
amount
}
}
}
"""
Scenario: Unknown
When I make the following query to the API:
"""unknown-language
this language is not known from prettier
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
`;

exports[`docstring_with_json.feature 1`] = `
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source
Expand Down Expand Up @@ -282,6 +447,7 @@ Feature: accountability
}
]
"""
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
options: {}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
51 changes: 51 additions & 0 deletions tests/plugin/docstring.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
Feature: Test different embed

Scenario: XML
When I receive the following feed from the bank:
"""xml
<feed>
<account number="123456789">
<entry>
<type>credit</type>
<amount>1000</amount>
<label>My company salary</label>
</entry>
</account>
</feed>
"""

Scenario: JSON
When I receive the following feed from the bank:
"""json
{ "account":
"1234",
"amount": 1000}
"""

Scenario: JSON if no language is specified (default fallback)
When I receive the following feed from the bank:
"""
{ "account":
"1234",
"amount": 1000}
"""

Scenario: GraphQL
When I make the following query to the API:
"""graphql
union Transaction = CreditTransaction|DebitTransaction
query AccountBalance($accountNumber:String!) {
account(accountNumber:$accountNumber) {
balance
transactions { amount
}
}
}
"""

Scenario: Unknown
When I make the following query to the API:
"""unknown-language
this language is not known from prettier
"""
2 changes: 1 addition & 1 deletion tests/plugin/example.feature
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ Feature: accountability
"credit": 5000
}
]
"""
"""

0 comments on commit 628e42e

Please sign in to comment.