MCPcopy Index your code
hub / github.com/nodejs/node / CheckBraces

Function CheckBraces

tools/cpplint.py:4820–4997  ·  view source on GitHub ↗

Looks for misplaced braces (e.g. at the end of line). Args: filename: The name of the current file. clean_lines: A CleansedLines instance containing the file. linenum: The number of the line to check. error: The function to call with any errors found.

(filename, clean_lines, linenum, error)

Source from the content-addressed store, hash-verified

4818
4819
4820def CheckBraces(filename, clean_lines, linenum, error):
4821 """Looks for misplaced braces (e.g. at the end of line).
4822
4823 Args:
4824 filename: The name of the current file.
4825 clean_lines: A CleansedLines instance containing the file.
4826 linenum: The number of the line to check.
4827 error: The function to call with any errors found.
4828 """
4829
4830 line = clean_lines.elided[linenum] # get rid of comments and strings
4831
4832 if re.match(r"\s*{\s*$", line):
4833 # We allow an open brace to start a line in the case where someone is using
4834 # braces in a block to explicitly create a new scope, which is commonly used
4835 # to control the lifetime of stack-allocated variables. Braces are also
4836 # used for brace initializers inside function calls. We don't detect this
4837 # perfectly: we just don't complain if the last non-whitespace character on
4838 # the previous non-blank line is ',', ';', ':', '(', '{', or '}', or if the
4839 # previous line starts a preprocessor block. We also allow a brace on the
4840 # following line if it is part of an array initialization and would not fit
4841 # within the 80 character limit of the preceding line.
4842 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
4843 if (
4844 not re.search(r"[,;:}{(]\s*$", prevline)
4845 and not re.match(r"\s*#", prevline)
4846 and not (GetLineWidth(prevline) > _line_length - 2 and "[]" in prevline)
4847 ):
4848 error(
4849 filename,
4850 linenum,
4851 "whitespace/braces",
4852 4,
4853 "{ should almost always be at the end of the previous line",
4854 )
4855
4856 # An else clause should be on the same line as the preceding closing brace.
4857 if last_wrong := re.match(r"\s*else\b\s*(?:if\b|\{|$)", line):
4858 prevline = GetPreviousNonBlankLine(clean_lines, linenum)[0]
4859 if re.match(r"\s*}\s*$", prevline):
4860 error(
4861 filename,
4862 linenum,
4863 "whitespace/newline",
4864 4,
4865 "An else should appear on the same line as the preceding }",
4866 )
4867 else:
4868 last_wrong = False
4869
4870 # If braces come on one side of an else, they should be on both.
4871 # However, we have to worry about "else if" that spans multiple lines!
4872 if re.search(r"else if\s*\(", line): # could be multi-line if
4873 brace_on_left = bool(re.search(r"}\s*else if\s*\(", line))
4874 # find the ( after the if
4875 pos = line.find("else if")
4876 pos = line.find("(", pos)
4877 if pos > 0:

Callers 1

CheckStyleFunction · 0.85

Calls 9

GetPreviousNonBlankLineFunction · 0.85
GetLineWidthFunction · 0.85
CloseExpressionFunction · 0.85
GetIndentLevelFunction · 0.85
matchMethod · 0.65
findMethod · 0.65
errorFunction · 0.50
searchMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…