-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Organize and add test and documentation for printIdentifier helper (#…
…3792) This is something that is quite useful for any external tool that want to write some typespec code(Openapi3 to tsp converter for example or api view) Edit: actually that was already exported as that exact name directly from the formatter. This then just adds tests
- Loading branch information
1 parent
6b79334
commit 2ebacf5
Showing
10 changed files
with
79 additions
and
53 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,6 @@ | ||
--- | ||
changeKind: internal | ||
packages: | ||
- "@typespec/compiler" | ||
--- | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { isIdentifierContinue, isIdentifierStart, utf16CodeUnits } from "../charcode.js"; | ||
import { Keywords } from "../scanner.js"; | ||
|
||
/** | ||
* Print a string as a TypeSpec identifier. If the string is a valid identifier, return it as is otherwise wrap it into backticks. | ||
* @param sv Identifier string value. | ||
* @returns Identifier string as it would be represented in a TypeSpec file. | ||
* | ||
* @example | ||
* ```ts | ||
* printIdentifier("foo") // foo | ||
* printIdentifier("foo bar") // `foo bar` | ||
* ``` | ||
*/ | ||
export function printIdentifier(sv: string) { | ||
if (needBacktick(sv)) { | ||
const escapedString = sv | ||
.replace(/\\/g, "\\\\") | ||
.replace(/\n/g, "\\n") | ||
.replace(/\r/g, "\\r") | ||
.replace(/\t/g, "\\t") | ||
.replace(/`/g, "\\`"); | ||
return `\`${escapedString}\``; | ||
} else { | ||
return sv; | ||
} | ||
} | ||
|
||
function needBacktick(sv: string) { | ||
if (sv.length === 0) { | ||
return false; | ||
} | ||
if (Keywords.has(sv)) { | ||
return true; | ||
} | ||
let cp = sv.codePointAt(0)!; | ||
if (!isIdentifierStart(cp)) { | ||
return true; | ||
} | ||
let pos = 0; | ||
do { | ||
pos += utf16CodeUnits(cp); | ||
} while (pos < sv.length && isIdentifierContinue((cp = sv.codePointAt(pos)!))); | ||
return pos < sv.length; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { expect, it } from "vitest"; | ||
import { printIdentifier } from "../../../src/index.js"; | ||
|
||
it.each([ | ||
["foo", "foo"], | ||
["foo-bar", "`foo-bar`"], | ||
["9test", "`9test`"], | ||
["foo bar", "`foo bar`"], | ||
["foo\nbar", "`foo\\nbar`"], | ||
])("%s -> %s", (a, b) => { | ||
expect(printIdentifier(a)).toEqual(b); | ||
}); |
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