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 support for the dart language #861

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
6 changes: 6 additions & 0 deletions internal/core/format.go
Original file line number Diff line number Diff line change
@@ -57,6 +57,11 @@ var CommentsByNormedExt = map[string]map[string]string{
"blockStart": `(^=begin)`,
"blockEnd": `(^=end)`,
},
".dart": {
"inline": `(/{2,3}.+)`,
"blockStart": `(\/\*{1,2}.*)`,
"blockEnd": `(.*\*{1,2}\/)`,
},
}

// FormatByExtension associates a file extension with its "normed" extension
@@ -67,6 +72,7 @@ var FormatByExtension = map[string][]string{
`\.(?:cpp|cc|c|cp|cxx|c\+\+|h|hpp|h\+\+)$`: {".c", "code"},
`\.(?:cs|csx)$`: {".c", "code"},
`\.(?:css)$`: {".css", "code"},
`\.(?:dart)$`: {".dart", "code"},
`\.(?:go)$`: {".c", "code"},
`\.(?:html|htm|shtml|xhtml)$`: {".html", "markup"},
`\.(?:rb|Gemfile|Rakefile|Brewfile|gemspec)$`: {".rb", "code"},
21 changes: 21 additions & 0 deletions internal/lint/comments.go
Original file line number Diff line number Diff line change
@@ -41,6 +41,20 @@ var patterns = map[string]map[string][]*regexp.Regexp{
regexp.MustCompile(`(.*\*/)`),
},
},
".dart": {
"inline": []*regexp.Regexp{
regexp.MustCompile(`(?s)/{2,3}\s*(.+)`),
},
"blockStart": []*regexp.Regexp{
regexp.MustCompile(`(?ms)/\*{1,2}\s*(.+)`),
},
"blockEnd": []*regexp.Regexp{
regexp.MustCompile(`(.*\*{1,2}/)`),
},
"inBlock": []*regexp.Regexp{
regexp.MustCompile(`(?ms)\s*\*?\s*(.*)`),
},
},
".rs": {
"inline": []*regexp.Regexp{
regexp.MustCompile(`(?s)/{3}!(.+)`),
@@ -194,6 +208,13 @@ func getComments(content, ext string) []Comment {
block.Reset()
inBlock = false
} else {
if byLang["inBlock"] != nil {
if match := doMatch(byLang["inBlock"], line); len(match) > 0 {
block.WriteString(match)
continue
}
}

block.WriteString(strings.TrimLeft(line, " "))
}
} else if match := doMatch(byLang["inline"], line); len(match) > 0 {
141 changes: 141 additions & 0 deletions testdata/comments/in/4.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/// This is a documentation comment for the entire Dart file.
///
/// This file demonstrates the usage of various types of comments
/// in Dart code, including:
/// - Documentation comments (using `///`)
/// - Block comments (using `/* */`)
/// - Inline comments (using `//`)
///
/// Each type of comment is used in different contexts to provide
/// explanations and annotations for the code.
/// A class representing a simple calculator.
///
/// The `Calculator` class provides basic arithmetic operations
/// such as addition, subtraction, multiplication, and division.
class Calculator {
/*
* The result of the last calculation.
* This private field is used to store the result of the most recent
* arithmetic operation performed by the calculator. It is not accessible
* directly outside the class.
*/
double _result = 0.0;

/// Adds two numbers and updates the result.
///
/// This method takes two `double` parameters, adds them together,
/// and stores the result in the `_result` variable. It also prints
/// the result to the console.
///
/// [a] The first number to add.
/// [b] The second number to add.
void add(double a, double b) {
// Calculate the sum of a and b
_result = a + b;
// Print the result
print('Addition result: $_result');
}

/// Subtracts the second number from the first number and updates the result.
///
/// This method performs subtraction and updates the internal result.
///
/// [a] The number to subtract from.
/// [b] The number to subtract.
void subtract(double a, double b) {
// Calculate the difference between a and b
_result = a - b;
// Print the result
print('Subtraction result: $_result');
}

/// Multiplies two numbers and updates the result.
///
/// This method performs multiplication and updates the internal result.
///
/// [a] The first number to multiply.
/// [b] The second number to multiply.
void multiply(double a, double b) {
// Calculate the product of a and b
_result = a * b;
// Print the result
print('Multiplication result: $_result');
}

/// Divides the first number by the second number and updates the result.
///
/// This method performs division and handles division by zero cases.
///
/// [a] The numerator.
/// [b] The denominator.
/// Throws an [ArgumentError] if [b] is zero.
void divide(double a, double b) {
if (b == 0) {
/*
* Handle division by zero error.
* Since division by zero is undefined, an ArgumentError is thrown
* to indicate that this operation is not allowed. This prevents
* the program from crashing unexpectedly.
*/
throw ArgumentError('Cannot divide by zero.');
}
// Calculate the quotient of a and b
_result = a / b;
// Print the result
print('Division result: $_result');
}

/*
* Returns the last result of the calculation.
* This method provides access to the last computed result. It is
* a getter that allows external code to retrieve the value of `_result`
* without modifying it. This ensures encapsulation while providing
* read-only access to the result.
*
* Returns:
* - A [double] representing the last result.
*/
double get result => _result;
}

/// A simple function to demonstrate the use of the `Calculator` class.
///
/// This function creates an instance of the `Calculator`, performs
/// some arithmetic operations, and prints the results to the console.
void main() {
// Create a new instance of the Calculator class
Calculator calc = Calculator();

/*
* Perform various arithmetic operations using the Calculator instance.
* The following operations are demonstrated:
* - Addition
* - Subtraction
* - Multiplication
* - Division (including division by zero to show error handling)
*/
// Perform addition
calc.add(10, 5);

// Perform subtraction
calc.subtract(10, 5);

// Perform multiplication
calc.multiply(10, 5);

// Perform division
try {
calc.divide(10, 0); // This will throw an exception
} catch (e) {
/*
* Catch and print the exception if division by zero occurs.
* The catch block handles the error thrown during division and
* provides feedback to the user or developer about the error.
*/
print('Caught an error: $e');
}

// Output the last result
print('Last result: ${calc.result}');
}
90 changes: 90 additions & 0 deletions testdata/comments/out/4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
This is a documentation comment for the entire Dart file.

This file demonstrates the usage of various types of comments
in Dart code, including:
- Documentation comments (using `///`)
- Block comments (using `/* */`)
- Inline comments (using `//`)

Each type of comment is used in different contexts to provide
explanations and annotations for the code.
A class representing a simple calculator.

The `Calculator` class provides basic arithmetic operations
such as addition, subtraction, multiplication, and division.

The result of the last calculation.
This private field is used to store the result of the most recent
arithmetic operation performed by the calculator. It is not accessible
directly outside the class.
Adds two numbers and updates the result.

This method takes two `double` parameters, adds them together,
and stores the result in the `_result` variable. It also prints
the result to the console.

[a] The first number to add.
[b] The second number to add.
Calculate the sum of a and b
Print the result
Subtracts the second number from the first number and updates the result.

This method performs subtraction and updates the internal result.

[a] The number to subtract from.
[b] The number to subtract.
Calculate the difference between a and b
Print the result
Multiplies two numbers and updates the result.

This method performs multiplication and updates the internal result.

[a] The first number to multiply.
[b] The second number to multiply.
Calculate the product of a and b
Print the result
Divides the first number by the second number and updates the result.

This method performs division and handles division by zero cases.

[a] The numerator.
[b] The denominator.
Throws an [ArgumentError] if [b] is zero.

Handle division by zero error.
Since division by zero is undefined, an ArgumentError is thrown
to indicate that this operation is not allowed. This prevents
the program from crashing unexpectedly.
Calculate the quotient of a and b
Print the result

Returns the last result of the calculation.
This method provides access to the last computed result. It is
a getter that allows external code to retrieve the value of `_result`
without modifying it. This ensures encapsulation while providing
read-only access to the result.
*
Returns:
- A [double] representing the last result.
A simple function to demonstrate the use of the `Calculator` class.

This function creates an instance of the `Calculator`, performs
some arithmetic operations, and prints the results to the console.
Create a new instance of the Calculator class

Perform various arithmetic operations using the Calculator instance.
The following operations are demonstrated:
- Addition
- Subtraction
- Multiplication
- Division (including division by zero to show error handling)
Perform addition
Perform subtraction
Perform multiplication
Perform division
This will throw an exception

Catch and print the exception if division by zero occurs.
The catch block handles the error thrown during division and
provides feedback to the user or developer about the error.
Output the last result