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

[Python] Use Fantomas formatting and check CI #2738

Merged
merged 5 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
12 changes: 12 additions & 0 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"fantomas-tool": {
"version": "4.6.0",
"commands": [
"fantomas"
]
}
}
}
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ indent_style = space
# TS files
[*.ts]
indent_size = 2

# Fantomas (see https://github.com/fsprojects/fantomas/blob/master/docs/Documentation.md)
[*.fs]
indent_size=4
max_line_length=140
fsharp_space_before_uppercase_invocation=false
nojaf marked this conversation as resolved.
Show resolved Hide resolved
fsharp_space_before_member=false
fsharp_space_around_delimiter=true
fsharp_multiline_block_brackets_on_same_column=false
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ jobs:
with:
dotnet-version: '6.0.x'

- name: Setup dotnet tools
run: dotnet tool restore

- name: Check formatting
run: dotnet fantomas src/Fable.Transforms/Python -check

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
Expand Down
3,260 changes: 2,053 additions & 1,207 deletions src/Fable.Transforms/Python/Fable2Python.fs

Large diffs are not rendered by default.

112 changes: 79 additions & 33 deletions src/Fable.Transforms/Python/Prelude.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,34 @@ module Naming =
open System.Text.RegularExpressions

let lowerFirst (s: string) =
s.Substring(0,1).ToLowerInvariant() + s.Substring(1)
s.Substring(0, 1).ToLowerInvariant()
+ s.Substring(1)

let upperFirst (s: string) =
s.Substring(0,1).ToUpperInvariant() + s.Substring(1)
s.Substring(0, 1).ToUpperInvariant()
+ s.Substring(1)

let private dashify (separator: string) (input: string) =
Regex.Replace(input, "[a-z]?[A-Z]", fun m ->
if m.Value.Length = 1 then m.Value.ToLowerInvariant()
else m.Value.Substring(0,1) + separator + m.Value.Substring(1,1).ToLowerInvariant())
Regex.Replace(
input,
"[a-z]?[A-Z]",
fun m ->
if m.Value.Length = 1 then
m.Value.ToLowerInvariant()
else
m.Value.Substring(0, 1)
+ separator
+ m.Value.Substring(1, 1).ToLowerInvariant()
)

let applyCaseRule caseRule name =
match caseRule with
| CaseRules.LowerFirst -> lowerFirst name
| CaseRules.SnakeCase -> dashify "_" name
| CaseRules.SnakeCaseAllCaps -> (dashify "_" name).ToUpperInvariant()
| CaseRules.KebabCase -> dashify "-" name
| CaseRules.None | _ -> name
| CaseRules.None
| _ -> name

let toSnakeCase (name: string) =
if name.Length > 0 && Char.IsLower(name.[0]) then
Expand All @@ -33,8 +44,10 @@ module Naming =
name

let cleanNameAsPyIdentifier (name: string) =
if name = ".ctor" then "_ctor"
else name.Replace('.','_').Replace('`','_')
if name = ".ctor" then
"_ctor"
else
name.Replace('.', '_').Replace('`', '_')

let pyKeywords =
// https://docs.python.org/3/reference/lexical_analysis.html#keywords
Expand Down Expand Up @@ -103,57 +116,90 @@ module Naming =
// Other names
"self"
]

let reflectionSuffix = "_reflection"


let preventConflicts conflicts originalName =
let rec check originalName n =
let name = if n > 0 then originalName + "_" + (string n) else originalName
if not (conflicts name) then name else check originalName (n+1)
let name =
if n > 0 then
originalName + "_" + (string n)
else
originalName

if not (conflicts name) then
name
else
check originalName (n + 1)

check originalName 0

let isIdentChar index (c: char) =
let code = int c

c = '_'
|| (65 <= code && code <= 90) // a-z
|| (97 <= code && code <= 122) // A-Z
|| (65 <= code && code <= 90) // a-z
|| (97 <= code && code <= 122) // A-Z
// Digits are not allowed in first position, see #1397
|| (index > 0 && 48 <= code && code <= 57) // 0-9

let hasIdentForbiddenChars (ident: string) =
let mutable found = false

for i = 0 to ident.Length - 1 do
found <- found || not(isIdentChar i ident.[i])
found <- found || not (isIdentChar i ident.[i])

found

let sanitizeIdentForbiddenChars (ident: string) =
if hasIdentForbiddenChars ident then
System.String.Concat(seq {
for i = 0 to (ident.Length - 1) do
let c = ident.[i]
if isIdentChar i c
then string c
elif c = '$' || c = '_' || c = ' ' || c = '*' || c = '.'
then "_"
else "_" + System.String.Format("{0:X}", int c).PadLeft(4, '0')
})
else ident
System.String.Concat(
seq {
for i = 0 to (ident.Length - 1) do
let c = ident.[i]

if isIdentChar i c then
string c
elif c = '$'
|| c = '_'
|| c = ' '
|| c = '*'
|| c = '.' then
"_"
else
"_"
+ System
.String
.Format("{0:X}", int c)
.PadLeft(4, '0')
}
)
else
ident

let checkPyKeywords name =
if pyKeywords.Contains name
then name + "_"
else name
if pyKeywords.Contains name then
name + "_"
else
name

let private printPart sanitize separator part overloadSuffix =
(if part = "" then "" else separator + (sanitize part)) +
(if overloadSuffix = "" then "" else "_" + overloadSuffix)
(if part = "" then
""
else
separator + (sanitize part))
+ (if overloadSuffix = "" then
""
else
"_" + overloadSuffix)

let private buildName sanitize name part =
(sanitize name) +
(match part with
| Naming.InstanceMemberPart(s, i) -> printPart sanitize "__" s i
| Naming.StaticMemberPart(s, i) -> printPart sanitize "_" s i
| Naming.NoMemberPart -> "")
(sanitize name)
+ (match part with
| Naming.InstanceMemberPart (s, i) -> printPart sanitize "__" s i
| Naming.StaticMemberPart (s, i) -> printPart sanitize "_" s i
| Naming.NoMemberPart -> "")

let sanitizeIdent conflicts name part =
// Replace Forbidden Chars
Expand Down
Loading