🎩 You're Invited:Meet the Socket team at Black Hat in Las Vegas, August 3-6.RSVP
Sign In

@fc-components/monaco-editor

Package Overview
Dependencies
Maintainers
5
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@fc-components/monaco-editor - npm Package Compare versions

Comparing version
0.5.3
to
0.5.4
+29
src/sql/__tests__/format.test.ts
import { formatSql } from '../format';
describe('formatSql', () => {
it('formats SQL with Grafana template variables', () => {
const sql =
"SELECT _wstart as ts, last(usage_idle) * -1 + 100 FROM $database.cpu WHERE (host = '$server' and cpu = 'cpu-total') and _ts >= $from and _ts <= $to interval($interval) fill(null)";
expect(formatSql(sql)).toBe(
[
'SELECT',
' _wstart AS ts,',
' last (usage_idle) * -1 + 100',
'FROM',
' $database.cpu',
'WHERE',
' (',
" host = '$server'",
" AND cpu = 'cpu-total'",
' )',
' AND _ts >= $from',
' AND _ts <= $to interval($interval) fill (NULL)',
].join('\n')
);
});
it('keeps Grafana macro calls intact', () => {
expect(formatSql('select $__timeFilter(ts) from metric')).toBe(['SELECT', ' $__timeFilter(ts)', 'FROM', ' metric'].join('\n'));
});
});
+1
-1

@@ -6,3 +6,3 @@ {

},
"version": "0.5.3",
"version": "0.5.4",
"license": "MIT",

@@ -9,0 +9,0 @@ "main": "dist/index.js",

import { format } from 'sql-formatter';
const GRAFANA_TEMPLATE_VARIABLE_REGEX = String.raw`\$[a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)*`;
const GRAFANA_MACRO_REGEX = String.raw`\$__[a-zA-Z_][a-zA-Z0-9_]*`;
export function formatSql(sql: string): string {

@@ -8,6 +11,6 @@ try {

keywordCase: 'upper',
// Treat custom $__ prefixed macros (like $__timeFilter, $__timeRange, etc.)
// as parameter placeholders so the formatter doesn't choke on them.
// Treat Grafana-style variables/macros as placeholders so the formatter can
// parse expressions like $database.cpu and $__timeFilter(ts).
paramTypes: {
custom: [{ regex: String.raw`\$__[a-zA-Z_][a-zA-Z0-9_]*` }],
custom: [{ regex: GRAFANA_TEMPLATE_VARIABLE_REGEX }],
},

@@ -17,3 +20,3 @@ });

// Remove that extra space so $__timeFilter( stays intact as a function call.
return formatted.replace(/(\$__[a-zA-Z_][a-zA-Z0-9_]*)\s+\(/g, '$1(');
return formatted.replace(new RegExp(`(${GRAFANA_MACRO_REGEX})\\s+\\(`, 'g'), '$1(');
} catch {

@@ -20,0 +23,0 @@ // If formatting fails, return the original SQL

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display