python

package
v0.0.0-...-22a4c2b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 1, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AttributeError = lib.ErrorTemplate{
	Name:    "AttributeError",
	Pattern: `AttributeError: '(?P<typeName>\S+)' object has no attribute '(?P<method>\S+)'`,
	OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {

		gen.Add(`Method "%s" does not exist in "%s" type`, cd.Variables["method"], cd.Variables["typeName"])
	},
	OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {

	},
}
View Source
var IndentationError = lib.ErrorTemplate{
	Name:    "IndentationError",
	Pattern: compileTimeError("IndentationError: unindent does not match any outer indentation level"),
	OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
		gen.Add("This error occurs when there is a mismatch in the indentation levels in the code.")
	},
	OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
		gen.Add("Correct the indentation", func(s *lib.BugFixSuggestion) {

			prevSibling := cd.MainError.Nearest.PrevNamedSibling()
			if prevSibling.Type() == "function_definition" {
				prevSibling = prevSibling.ChildByFieldName("body").LastNamedChild()
			}

			spaces := cd.MainError.Document.LineAt(prevSibling.StartPosition().Line)[:prevSibling.StartPosition().Column]

			s.AddStep("Ensure consistent indentation by using the correct spacing for each level of indentation.").
				AddFix(lib.FixSuggestion{
					NewText: "",
					StartPosition: lib.Position{
						Line: cd.MainError.Nearest.StartPosition().Line,
					},
					EndPosition: cd.MainError.Nearest.StartPosition(),
				}).
				AddFix(lib.FixSuggestion{
					NewText: spaces,
					StartPosition: lib.Position{
						Line: cd.MainError.Nearest.StartPosition().Line,
					},
					EndPosition: lib.Position{
						Line: cd.MainError.Nearest.StartPosition().Line,
					},
				})
		})
	},
}
View Source
var NameError = lib.ErrorTemplate{
	Name:    "NameError",
	Pattern: `NameError: name '(?P<variable>\S+)' is not defined`,
	OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
		for q := m.Nearest.Query(`((identifier) @name (#eq? @name "%s"))`, cd.Variables["variable"]); q.Next(); {
			m.Nearest = q.CurrentNode()
			break
		}
	},
	OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
		gen.Add("This error occurs when trying to use a variable (`%s`) or name that has not been defined in the current scope.", cd.Variables["variable"])
	},
	OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
		gen.Add("Define the variable before using it", func(s *lib.BugFixSuggestion) {

			parent := cd.MainError.Nearest.Parent()
			for !parent.IsNull() && parent.Type() != "module" && parent.Type() != "block" {
				parent = parent.Parent()
			}

			spaces := cd.MainError.Document.LineAt(parent.StartPosition().Line)[:parent.StartPosition().Column]

			s.AddStep("Make sure to define the variable `%s` before using it.", cd.Variables["variable"]).
				AddFix(lib.FixSuggestion{
					NewText: spaces + fmt.Sprintf("%s = \"Hello!\"\n", cd.Variables["variable"]),
					StartPosition: lib.Position{
						Line: parent.StartPosition().Line,
					},
				})
		})
	},
}
View Source
var SyntaxError = lib.ErrorTemplate{
	Name:    "SyntaxError",
	Pattern: compileTimeError("SyntaxError: '(?P<character>.+)' was never closed"),
	OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
		for q := m.Nearest.Query(`(ERROR) @err`); q.Next(); {
			m.Nearest = q.CurrentNode()
			break
		}
	},
	OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
		text := fmt.Sprintf("the '%s'", cd.Variables["character"])
		if word, ok := charToWord[cd.Variables["character"]]; ok {
			text = fmt.Sprintf("the %s `%s`", word, cd.Variables["character"])
		}

		gen.Add(
			"This error occurs when there is a syntax error in the code, and %s is not closed properly.",
			text)
	},
	OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {

		if pair, ok := charPairs[cd.Variables["character"]]; ok {
			text := fmt.Sprintf("the `%s`", cd.Variables["character"])
			if word, ok := charToWord[cd.Variables["character"]]; ok {
				text = fmt.Sprintf("the %s", word)
			}

			gen.Add("Close "+text, func(s *lib.BugFixSuggestion) {
				text = fmt.Sprintf("%s (`%s`)", text, cd.Variables["character"])

				s.AddStep("Ensure that %s is closed properly.", text).
					AddFix(lib.FixSuggestion{
						NewText:       pair,
						StartPosition: cd.MainError.Nearest.EndPosition(),
						EndPosition:   cd.MainError.Nearest.EndPosition(),
					})
			})
		}
	},
}
View Source
var ValueError = lib.ErrorTemplate{
	Name:    "ValueError",
	Pattern: "ValueError: (?P<reason>.+)",
	OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
		vCtx := valueErrorCtx{}
		reason := cd.Variables["reason"]
		query := ""

		if strings.HasPrefix(reason, "invalid literal for int() with base 10") {
			vCtx.kind = valueErrorKindInt
			query = "((call function: (identifier) @func) @call (#eq? @func \"int\"))"
		} else {
			vCtx.kind = valueErrorKindUnknown
		}

		for q := m.Nearest.Query(query); q.Next(); {
			node := q.CurrentNode()
			vCtx.callNode = node
			m.Nearest = node.ChildByFieldName("arguments").FirstNamedChild()
			break
		}

		m.Context = vCtx
	},
	OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {
		ctx := cd.MainError.Context.(valueErrorCtx)

		switch ctx.kind {
		case valueErrorKindInt:
			gen.Add("This error occurs when you try to convert a value to `int`, but the value is not a valid `int`.")
		default:
			gen.Add("This error occurs when you try to convert a value to another type, but the value is not a valid value for that type.")
		}
	},
	OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
		ctx := cd.MainError.Context.(valueErrorCtx)

		switch ctx.kind {
		case valueErrorKindInt:
			gen.Add("Use a valid integer string", func(s *lib.BugFixSuggestion) {
				s.AddStep("Make sure the value you're trying to convert is a valid integer string.").
					AddFix(lib.FixSuggestion{
						NewText:       `"123"`,
						StartPosition: cd.MainError.Nearest.StartPosition(),
						EndPosition:   cd.MainError.Nearest.EndPosition(),
					})
			})
		}

		gen.Add("Add error handling", func(s *lib.BugFixSuggestion) {
			parent := ctx.callNode.Parent()
			for !parent.IsNull() && parent.Type() != "block" && parent.Type() != "module" {
				parent = parent.Parent()
			}

			spaces := cd.MainError.Document.LineAt(parent.StartPosition().Line)[:parent.StartPosition().Column]
			origSpaces := strings.Clone(spaces)

			if len(spaces) == 0 {
				spaces = "\t"
			}

			s.AddStep("To handle invalid inputs gracefully, you can use a try-except block.").
				AddFix(lib.FixSuggestion{
					NewText: origSpaces + "try:\n",
					StartPosition: lib.Position{
						Line: parent.StartPosition().Line,
					},
					EndPosition: lib.Position{
						Line: parent.StartPosition().Line,
					},
				}).
				AddFix(lib.FixSuggestion{
					NewText:       spaces,
					StartPosition: parent.StartPosition(),
					EndPosition:   parent.StartPosition(),
				}).
				AddFix(lib.FixSuggestion{
					NewText:       origSpaces + "except ValueError as e:\n" + spaces + "print(f\"Error: {e}\")",
					StartPosition: parent.EndPosition(),
					EndPosition:   parent.EndPosition(),
				})
		})
	},
}
View Source
var ZeroDivisionError = lib.ErrorTemplate{
	Name:    "ZeroDivisionError",
	Pattern: "ZeroDivisionError: division by zero",
	OnAnalyzeErrorFn: func(cd *lib.ContextData, m *lib.MainError) {
		for q := m.Nearest.Query("(binary_operator right: (_) @right)"); q.Next(); {
			m.Nearest = q.CurrentNode()
			break
		}
	},
	OnGenExplainFn: func(cd *lib.ContextData, gen *lib.ExplainGenerator) {

		gen.Add("This error occurs when there is an attempt to divide a number by zero.")
	},
	OnGenBugFixFn: func(cd *lib.ContextData, gen *lib.BugFixGenerator) {
		gen.Add("Avoid division by zero", func(s *lib.BugFixSuggestion) {
			recommendedNumber := 2

			s.AddStep("Ensure that the denominator in a division operation is not zero.").
				AddFix(lib.FixSuggestion{
					NewText:       fmt.Sprintf("%d", recommendedNumber),
					StartPosition: cd.MainError.Nearest.StartPosition(),
					EndPosition:   cd.MainError.Nearest.EndPosition(),
				})
		})
	},
}

Functions

func LoadErrorTemplates

func LoadErrorTemplates(errorTemplates *lib.ErrorTemplates)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL