cucumber / gherkin

A parser and compiler for the Gherkin language.
MIT License
175 stars 54 forks source link

Keep original keywords when feature file is parsed (js) #143

Closed amiceli closed 1 year ago

amiceli commented 1 year ago

🤔 What's the problem you're trying to solve?

I'm using @cucumber/messages and @cucumber/gherkin in a project in my comparny. We are building a tool for us who need to convert feature file to json.

Parsing to JSON is OK but we need to keep original keywords like And, When, Then who are translated to Action, Action etc.

✨ What's your proposed solution?

Currently I don't know if it's possible to customize parser for keyword. I read README.md many times but it's hard to understand.

⛏ Have you considered any alternatives or workarounds?

Currently I don't found a workaround.

📚 Any additional context?

A example of feature file content with keywords we use :

Feature: Awesome feature

    Scenario: ...
        Given ...
        When ...
        And ...
        Then ...

This text was originally generated from a template, then edited by hand. You can modify the template here.

mpkorstanje commented 1 year ago

I didn't see you include any json. Are you perhaps confusing the keyword and keywordType fields?

amiceli commented 1 year ago

Maybe I'm confused ^^. This is my JS script :

const fs = require('fs/promises')
const Gherkin = require('@cucumber/gherkin')
const Messages = require('@cucumber/messages')

async function main() {
    const content = await fs.readFile('./index.feature')

    const uuidFn = Messages.IdGenerator.uuid()
    const builder = new Gherkin.AstBuilder(uuidFn)
    const matcher = new Gherkin.GherkinClassicTokenMatcher() // or Gherkin.GherkinInMarkdownTokenMatcher()

    const parser = new Gherkin.Parser(builder, matcher)
    const gherkinDocument = parser.parse(content.toString())
    const pickles = Gherkin.compile(gherkinDocument, './index.feature', uuidFn)

    const json = JSON.stringify(pickles)

    await fs.writeFile('./index.json', json)
}

main().then(() => {
    console.debug('ok')
})

After I run it, index.json file is generated. For example it contains :

{
    "id": "7d6c07bd-6618-4602-9222-ad3806f7aaf3",
    "text": "...",
    "type": "Action",
    "astNodeIds": [
        "ac60a9e6-3a7c-478c-92fa-23ac83e79acd"
    ]
}

And is it possible to have When, And, When etc instead of Action, Outcome etc ?

mpkorstanje commented 1 year ago

You're looking at the JSON for the Pickles. Not the Gherkin document.

The purpose of the Pickles is explained here: https://github.com/cucumber/gherkin#pickles. If you do want to link a step in a Pickle back to the Gherkin document you can use the astNodeIds to do so.

But you'll probably want to look at the Gherkin document.

amiceli commented 1 year ago

Thanks for your help.