Skip to content

Commit

Permalink
Merge pull request #7 from tidbcloud/chore/extensions-linter
Browse files Browse the repository at this point in the history
chore: add extension database-linter
  • Loading branch information
sanshuiyijing authored Jun 17, 2024
2 parents f7b9fed + b4871a6 commit cc86721
Show file tree
Hide file tree
Showing 12 changed files with 1,810 additions and 2,094 deletions.
2 changes: 1 addition & 1 deletion packages/extensions/basic-setup/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tidbcloud/tisqleditor-extensions-basic-setup",
"version": "0.0.1",
"version": "1.0.0",
"description": "tisqleditor extensions",
"type": "module",
"main": "dist/index.js",
Expand Down
47 changes: 47 additions & 0 deletions packages/extensions/database-linter/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@tidbcloud/tisqleditor-extensions-database-linter",
"version": "1.0.0",
"description": "tisqleditor extensions",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*.js",
"dist/*.ts",
"package.json",
"README.md"
],
"scripts": {
"tsc:watch": "tsc --watch",
"rollup:watch": "rollup -c --watch",
"dev": "concurrently --kill-others \"pnpm tsc:watch\" \"pnpm rollup:watch\"",
"build": "tsc && rollup -c"
},
"keywords": [
"tidbcloud",
"sql",
"editor",
"extensions",
"database linter"
],
"author": "",
"license": "MIT",
"devDependencies": {
"@codemirror/lint": "^6.8.0",
"@codemirror/state": "^6.4.1",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^4.18.0",
"tslib": "^2.6.3",
"typescript": "^5.4.5"
},
"peerDependencies": {
"@codemirror/lint": "^6.8.0",
"@codemirror/state": "^6.4.1",
"@codemirror/view": "^6.26.3"
},
"dependencies": {
"@tidbcloud/tisqleditor-extensions-sql-parser": "workspace:^",
"@tidbcloud/tisqleditor-extensions-grammer-linter": "workspace:^"
}
}
47 changes: 47 additions & 0 deletions packages/extensions/database-linter/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { linter, Diagnostic } from '@codemirror/lint'

import { getSqlStatements } from '@tidbcloud/tisqleditor-extensions-sql-parser'
import {
hintEle,
linterBaseTheme
} from '@tidbcloud/tisqleditor-extensions-grammer-linter'

export type DBLinterOptions = {
level?: 'error' | 'warning'
title?: string
message?: string
preCheck?: () => string
}

const databaseLinter = (config: DBLinterOptions) =>
linter((view) => {
const diagnostics: Diagnostic[] = []

if (config.preCheck && config.preCheck()) {
return diagnostics
}

getSqlStatements(view.state).forEach((statement) => {
if (statement.database === '' && statement.type !== 'ddl') {
diagnostics.push({
from: statement.from,
to: statement.to,
severity: config.level || 'warning',
message: '',
renderMessage: () => {
return hintEle(
config.title || '',
config.message ||
'No database selected by using `USE {database}` statement, this statement may run failed.'
)
}
})
}
})

return diagnostics
})

export function dbLinter(config: DBLinterOptions) {
return [databaseLinter(config), linterBaseTheme]
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../tsconfig.json",
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tidbcloud/tisqleditor-extensions-grammer-check",
"name": "@tidbcloud/tisqleditor-extensions-grammer-linter",
"version": "1.0.0",
"description": "tisqleditor extensions",
"type": "module",
Expand All @@ -23,7 +23,7 @@
"sql",
"editor",
"extensions",
"grammer check"
"grammer linter"
],
"author": "",
"license": "MIT",
Expand Down
10 changes: 10 additions & 0 deletions packages/extensions/grammer-linter/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import typescript from '@rollup/plugin-typescript'

export default {
input: './src/index.ts',
output: {
dir: './dist',
format: 'es'
},
plugins: [typescript()]
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { linter, Diagnostic } from '@codemirror/lint'
import { Extension } from '@codemirror/state'
import { EditorView } from '@codemirror/view'

export interface GrammerCheckConfig {
export interface GrammerLinterConfig {
title?: string
content?: string
}
Expand All @@ -13,7 +13,7 @@ export interface RegexpItem {
message: string
}

const linterBaseTheme: Extension = EditorView.baseTheme({
export const linterBaseTheme: Extension = EditorView.baseTheme({
'.cm-tooltip-hover': {
marginTop: '4px',
borderRadius: '8px'
Expand Down Expand Up @@ -50,7 +50,7 @@ const linterBaseTheme: Extension = EditorView.baseTheme({
}
})

const hintEle = (title?: string, content?: string) => {
export const hintEle = (title?: string, content?: string) => {
const ele = document.createDocumentFragment()
const titleEle = document.createElement('div')
titleEle.textContent = title ?? 'Invalid Character'
Expand All @@ -64,7 +64,7 @@ const hintEle = (title?: string, content?: string) => {
return ele
}

const chineseCharacterCheckParser = (config: GrammerCheckConfig) => {
const chineseCharacterCheckParser = (config: GrammerLinterConfig) => {
return linter((view) => {
const diagnostics: Diagnostic[] = []
view.visibleRanges.forEach((range) => {
Expand Down Expand Up @@ -139,7 +139,7 @@ const regexMatchParser = (regs: RegexpItem[]) => {
})
}

export const chineseCharacterLinter = (config: GrammerCheckConfig) => {
export const chineseCharacterLinter = (config: GrammerLinterConfig) => {
return [chineseCharacterCheckParser(config), linterBaseTheme]
}

Expand Down
7 changes: 7 additions & 0 deletions packages/extensions/grammer-linter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist"
},
"include": ["./src"]
}
8 changes: 7 additions & 1 deletion packages/extensions/lang-sql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
"dev": "concurrently --kill-others \"pnpm tsc:watch\" \"pnpm rollup:watch\"",
"build": "tsc && rollup -c"
},
"keywords": [],
"keywords": [
"tidbcloud",
"sql",
"editor",
"extensions",
"lang sql"
],
"author": "",
"license": "MIT",
"devDependencies": {
Expand Down
8 changes: 7 additions & 1 deletion packages/extensions/sql-parser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@
"dev": "concurrently --kill-others \"pnpm tsc:watch\" \"pnpm rollup:watch\"",
"build": "tsc && rollup -c"
},
"keywords": [],
"keywords": [
"tidbcloud",
"sql",
"editor",
"extensions",
"sql parser"
],
"author": "",
"license": "MIT",
"devDependencies": {
Expand Down
Loading

0 comments on commit cc86721

Please sign in to comment.