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 SQL Linter using Squawk #94

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Pinterest.
- [Python isort](#python-isort)
- [Python Requirements](#python-requirements)
- [Spectral](#spectral)
- [Squawk](#squawk)
- [ThriftCheck](#thriftcheck)
- [YamlLinter](#yamllinter)

Expand Down Expand Up @@ -310,6 +311,18 @@ Lints OpenAPI documents using [Spectral](https://stoplight.io/spectral).
}
```

### Squawk

Lints SQL files using [Squawk](https://squawkhq.com/).

```json
{
"type": "squawk",
"include": "(\\.sql)",
"squawk.config": ".squawk.toml"
}
```

### ThriftCheck

Lints Thrift IDL files using [ThriftCheck](https://github.com/pinterest/thriftcheck).
Expand Down
2 changes: 2 additions & 0 deletions __phutil_library_map__.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
'PythonIsortLinter' => 'src/PythonIsortLinter.php',
'PythonRequirementsLinter' => 'src/PythonRequirementsLinter.php',
'SpectralLinter' => 'src/SpectralLinter.php',
'SquawkLinter' => 'src/SquawkLinter.php',
'ThriftCheckLinter' => 'src/ThriftCheckLinter.php',
'YamlLinter' => 'src/YamlLinter.php',
),
Expand Down Expand Up @@ -58,6 +59,7 @@
'PythonIsortLinter' => 'PythonExternalLinter',
'PythonRequirementsLinter' => 'ArcanistLinter',
'SpectralLinter' => 'NodeExternalLinter',
'SquawkLinter' => 'NodeExternalLinter',
'ThriftCheckLinter' => 'PinterestExternalLinter',
'YamlLinter' => 'ArcanistLinter',
),
Expand Down
157 changes: 157 additions & 0 deletions src/SquawkLinter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* Lints SQL files using Squawk
*/
final class SquawkLinter extends NodeExternalLinter {

private $config = null;

public function getInfoName() {
return 'Squawk';
}

public function getInfoURI() {
return 'https://squawkhq.com/';
}

public function getInfoDescription() {
return 'Lint SQL using Squawk';
}

public function getLinterName() {
return 'SQUAWK';
}

public function getLinterConfigurationName() {
return 'squawk';
}

public function getVersion() {
list($err, $stdout, $stderr) = exec_manual('%C --version', $this->getExecutableCommand());
return trim($stdout);
}

public function getLinterConfigurationOptions() {
$options = array(
'squawk.config' => array(
'type' => 'string',
'help' => pht('Configuration file to use'),
),
);
return $options + parent::getLinterConfigurationOptions();
}

public function setLinterConfigurationValue($key, $value) {
switch ($key) {
case 'squawk.config':
$this->config = $value;
return;
}
return parent::setLinterConfigurationValue($key, $value);
}

public function getNodeBinary() {
return 'squawk';
}

public function getNpmPackageName() {
return 'squawk-cli';
}

protected function getMandatoryFlags() {
return array('-c', $this->config);
}

protected function getDefaultFlags() {
return array('--reporter', 'Json');
}

public function shouldExpectCommandErrors() {
return true;
}

public function getLintSeverityMap() {
return array(
'Error' => ArcanistLintSeverity::SEVERITY_ERROR,
'Warning' => ArcanistLintSeverity::SEVERITY_WARNING
);
}

protected function parseLinterOutput($path, $err, $stdout, $stderr) {
/*
[
{
"file": "path/to.sql",
"line": 0,
"column": 0,
"level": "Error",
"messages": [
{
"Note": "Postgres failed to parse query: syntax error at or near \"some_random_stuffs\""
},
{
"Help": "Modify your Postgres statement to use valid syntax."
}
],
"rule_name": "invalid-statement"
},
{
"file": "path/to_second.sql",
"line": 1,
"column": 0,
"level": "Warning",
"messages": [
{
"Note": "Dropping a column may break existing clients."
}
],
"rule_name": "ban-drop-column"
}
]
*/
$json = json_decode($stdout, true);

$messages = array();
foreach ($json as $item) {
$level = $item['level'];
$rulename = $item['rule_name'];
$description = "$level ($rulename): ";

foreach($item['messages'] as $message) {
$value = '';

if (array_key_exists('Note', $message)) {
$value = $message['Note'];
}

if (array_key_exists('Help', $message)) {
$value = $message['Help'];
}

$description.="\n$value";
}

$line = $item['line'];
$column = $item['column'];

// $line=0 means the file has broken syntax
// otherwise sum the $line and $ column to get the first row of error
if ($line == '0') {
$line = 1;
} else if ($column != '0') {
$line = $line + $column - 1;
}

$messages[] = id(new ArcanistLintMessage())
->setName($this->getLinterName())
->setCode($rulename)
->setPath(idx($item, 'file', $path))
->setLine($line)
->setChar(1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have expecting $column to be used here to set the "character" offset, but it looks like it's being used to adjust the $line above?

Copy link
Contributor Author

@febrianyuwono febrianyuwono Nov 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the $column here is showing how many rows are being warned by the linter
for example you have drop column like this:

   1 ALTER TABLE
   2   table_name
   3 DROP COLUMN
   4   column_name;
   5 
   6 -- test some column with spaces
   7               ALTER TABLE table_name DROP COLUMN column_name;
   8 
   9 
  10 -- test column with comment
  11 /* bla3 */ ALTER TABLE table_name DROP column column_name;

the first one will have line 1 column 0
the second one will have line 5 column 2 (so I added line + column - 1)
the third one will have line 8 column 3 (8+3-1 = 10)

so the calculated line is used to show which line is the starting one (includes comment)

and the char offset is not shown in the linter, so it's put as one

->setDescription($description)
->setSeverity($this->getLintMessageSeverity($level));
}

return $messages;
}
}