forked from cucumber/cucumber-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript_snippet_syntax.js
45 lines (40 loc) · 1.33 KB
/
javascript_snippet_syntax.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const CALLBACK_NAME = 'callback'
export default class JavaScriptSnippetSyntax {
constructor(snippetInterface) {
this.snippetInterface = snippetInterface
}
build({ comment, generatedExpressions, functionName, stepParameterNames }) {
let functionKeyword = 'function '
if (this.snippetInterface === 'async-await') {
functionKeyword = 'async ' + functionKeyword
} else if (this.snippetInterface === 'generator') {
functionKeyword += '*'
}
let implementation
if (this.snippetInterface === 'callback') {
implementation = `${CALLBACK_NAME}(null, 'pending');`
} else {
implementation = "return 'pending';"
}
const definitionChoices = generatedExpressions.map(
(generatedExpression, index) => {
const prefix = index === 0 ? '' : '// '
const allParameterNames = generatedExpression.parameterNames.concat(
stepParameterNames
)
if (this.snippetInterface === 'callback') {
allParameterNames.push(CALLBACK_NAME)
}
return `${prefix + functionName}('${generatedExpression.source.replace(
/'/g,
"\\'"
)}', ${functionKeyword}(${allParameterNames.join(', ')}) {\n`
}
)
return (
`${definitionChoices.join('')} // ${comment}\n` +
` ${implementation}\n` +
`});`
)
}
}