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

Add a gpt linter #1

Open
eonist opened this issue Feb 25, 2025 · 1 comment
Open

Add a gpt linter #1

eonist opened this issue Feb 25, 2025 · 1 comment

Comments

@eonist
Copy link
Owner

eonist commented Feb 25, 2025

Certainly! Here's a bash script that calls the OpenAI API to check if a Swift file adheres to the rules specified in a .swiftlint.yml file:

#!/bin/bash

# Check if required arguments are provided
if [ $# -ne 3 ]; then
    echo "Usage: $0   "
    exit 1
fi

API_KEY=$1
SWIFT_FILE=$2
SWIFTLINT_FILE=$3

# Read the contents of the Swift file and SwiftLint configuration
SWIFT_CONTENT=$(cat "$SWIFT_FILE")
SWIFTLINT_CONTENT=$(cat "$SWIFTLINT_FILE")

# Prepare the API request payload
PAYLOAD=$(cat <<EOF
{
    "model": "gpt-4",
    "messages": [
        {
            "role": "system",
            "content": "You are a SwiftLint expert. Analyze the provided Swift code and ensure it adheres to the rules specified in the SwiftLint configuration."
        },
        {
            "role": "user",
            "content": "Here's the Swift code:\n\n$SWIFT_CONTENT\n\nAnd here's the SwiftLint configuration:\n\n$SWIFTLINT_CONTENT\n\nPlease analyze the code and provide a list of any violations or suggestions for improvement based on the SwiftLint rules."
        }
    ]
}
EOF
)

# Make the API request
RESPONSE=$(curl -s -X POST "https://api.openai.com/v1/chat/completions" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $API_KEY" \
    -d "$PAYLOAD")

# Extract and print the API response
echo "SwiftLint Analysis:"
echo "$RESPONSE" | jq -r '.choices[0].message.content'

This script does the following:

1. It takes three command-line arguments: the OpenAI API key, the path to the Swift file, and the path to the .swiftlint.yml file[1].

  1. It reads the contents of both the Swift file and the SwiftLint configuration file[1].

  2. It prepares a payload for the OpenAI API request, including the Swift code and SwiftLint configuration[4][7].

  3. It sends a POST request to the OpenAI API using curl, asking the model to analyze the code based on the SwiftLint rules[4][7].

  4. Finally, it extracts and prints the analysis results from the API response[4].

To use this script:

  1. Save it as a file, e.g., swiftlint_checker.sh.
  2. Make it executable: chmod +x swiftlint_checker.sh.
  3. Run it with your API key and file paths:
    ./swiftlint_checker.sh YOUR_API_KEY path/to/your/swift_file.swift path/to/your/.swiftlint.yml
    

This script provides a way to leverage OpenAI's language model to perform SwiftLint-like analysis on your Swift code. However, please note that this is not a replacement for the actual SwiftLint tool, as the AI model's understanding might not be as precise or up-to-date as the official SwiftLint rules[3][5].

For production use, it's recommended to use the official SwiftLint tool, which can be integrated directly into your Xcode project or CI/CD pipeline for more accurate and consistent results[5][8].

Citations:
[1] https://github.com/PBJI/openai-api-client-bash/
[2] https://www.delasign.com/blog/swiftlint-enabled-swift-xcode-project/
[3] https://stackoverflow.com/questions/43109593/how-do-i-create-swiftlint-yml-file-where-i-need-to-put-it/47574404
[4] https://pipedream.com/apps/openai/integrations/bash
[5] https://stackoverflow.com/questions/57461737/how-to-integrate-swiftlint-with-an-ios-app-using-swift-package-manager
[6] realm/SwiftLint#2673
[7] https://pipedream.com/apps/bash/integrations/openai
[8] https://canopas.com/swiftlint-integration-in-xcode-and-gitlab-ci-1ae9ef6e5d85
[9] https://platform.openai.com/docs/quickstart
[10] https://community.openai.com/t/best-way-to-create-shell-scripting-app/10613

@eonist
Copy link
Owner Author

eonist commented Feb 25, 2025

We could maybe instead add swift-guides. The ones from linkedin Airbnb etc. and if things deviate. We could make a PR. With suggested fixes. And let reviewer cherry pick etc? there is also some really good AI linter projects out there that can be used as inspo for this one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant