@sqldoc/ns-comment 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@sqldoc/ns-comment",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "description": "Comment namespace for sqldoc -- generates COMMENT ON statements",
6
6
  "exports": {
7
7
  ".": {
@@ -14,15 +14,16 @@
14
14
  "types": "./src/index.ts",
15
15
  "files": [
16
16
  "src",
17
+ "!src/__tests__",
17
18
  "package.json"
18
19
  ],
19
20
  "peerDependencies": {
20
- "@sqldoc/core": "0.0.1"
21
+ "@sqldoc/core": "0.0.2"
21
22
  },
22
23
  "devDependencies": {
23
24
  "typescript": "^5.9.3",
24
25
  "vitest": "^4.1.0",
25
- "@sqldoc/core": "0.0.1"
26
+ "@sqldoc/core": "0.0.2"
26
27
  },
27
28
  "scripts": {
28
29
  "test": "vitest run"
@@ -1,180 +0,0 @@
1
- import type { CompilerContext } from '@sqldoc/core'
2
- import { describe, expect, it } from 'vitest'
3
- import plugin from '../index'
4
-
5
- function makeCtx(overrides: Partial<CompilerContext> = {}): CompilerContext {
6
- return {
7
- target: 'table',
8
- objectName: 'users',
9
- tag: { name: '$self', args: ['A table for user accounts'] },
10
- namespaceTags: [],
11
- siblingTags: [],
12
- fileTags: [],
13
- astNode: null,
14
- fileStatements: [],
15
- config: {},
16
- filePath: 'test.sql',
17
- ...overrides,
18
- }
19
- }
20
-
21
- describe('ns-comment plugin', () => {
22
- it('exports apiVersion === 1', () => {
23
- expect(plugin.apiVersion).toBe(1)
24
- })
25
-
26
- it('exports name === "comment"', () => {
27
- expect(plugin.name).toBe('comment')
28
- })
29
-
30
- it('has $self tag definition', () => {
31
- expect(plugin.tags).toHaveProperty('$self')
32
- })
33
-
34
- it('$self tag accepts positional string arg', () => {
35
- const def = plugin.tags.$self!
36
- expect(def.args).toEqual([{ type: 'string' }])
37
- })
38
-
39
- describe('generateSQL', () => {
40
- it('generates COMMENT ON TABLE for table target', () => {
41
- const ctx = makeCtx({
42
- target: 'table',
43
- objectName: 'users',
44
- tag: { name: '$self', args: ['A table for user accounts'] },
45
- })
46
- const result = plugin.generateSQL!(ctx)
47
- expect(result).toEqual([{ sql: `COMMENT ON TABLE "users" IS 'A table for user accounts';` }])
48
- })
49
-
50
- it('generates COMMENT ON COLUMN for column target', () => {
51
- const ctx = makeCtx({
52
- target: 'column',
53
- objectName: 'users',
54
- columnName: 'email',
55
- tag: { name: '$self', args: ['The user email address'] },
56
- })
57
- const result = plugin.generateSQL!(ctx)
58
- expect(result).toEqual([{ sql: `COMMENT ON COLUMN "users"."email" IS 'The user email address';` }])
59
- })
60
-
61
- it('generates COMMENT ON VIEW for view target', () => {
62
- const ctx = makeCtx({
63
- target: 'view',
64
- objectName: 'active_users',
65
- tag: { name: '$self', args: ['View of currently active users'] },
66
- })
67
- const result = plugin.generateSQL!(ctx)
68
- expect(result).toEqual([{ sql: `COMMENT ON VIEW "active_users" IS 'View of currently active users';` }])
69
- })
70
-
71
- it('generates COMMENT ON FUNCTION for function target', () => {
72
- const ctx = makeCtx({
73
- target: 'function',
74
- objectName: 'get_user_by_id',
75
- tag: { name: '$self', args: ['Retrieves a user by their ID'] },
76
- })
77
- const result = plugin.generateSQL!(ctx)
78
- expect(result).toEqual([{ sql: `COMMENT ON FUNCTION "get_user_by_id" IS 'Retrieves a user by their ID';` }])
79
- })
80
-
81
- it('generates COMMENT ON TYPE for type target', () => {
82
- const ctx = makeCtx({
83
- target: 'type',
84
- objectName: 'user_role',
85
- tag: { name: '$self', args: ['Enum of possible user roles'] },
86
- })
87
- const result = plugin.generateSQL!(ctx)
88
- expect(result).toEqual([{ sql: `COMMENT ON TYPE "user_role" IS 'Enum of possible user roles';` }])
89
- })
90
-
91
- it('generates COMMENT ON INDEX for index target', () => {
92
- const ctx = makeCtx({
93
- target: 'index',
94
- objectName: 'idx_users_email',
95
- tag: { name: '$self', args: ['Index for fast email lookups'] },
96
- })
97
- const result = plugin.generateSQL!(ctx)
98
- expect(result).toEqual([{ sql: `COMMENT ON INDEX "idx_users_email" IS 'Index for fast email lookups';` }])
99
- })
100
-
101
- it('escapes single quotes in description', () => {
102
- const ctx = makeCtx({
103
- target: 'table',
104
- objectName: 'users',
105
- tag: { name: '$self', args: ["The user's primary table"] },
106
- })
107
- const result = plugin.generateSQL!(ctx)
108
- expect(result).toEqual([{ sql: `COMMENT ON TABLE "users" IS 'The user''s primary table';` }])
109
- })
110
-
111
- it('handles null tag name the same as $self', () => {
112
- const ctx = makeCtx({
113
- target: 'table',
114
- objectName: 'orders',
115
- tag: { name: null, args: ['Order records'] },
116
- })
117
- const result = plugin.generateSQL!(ctx)
118
- expect(result).toEqual([{ sql: `COMMENT ON TABLE "orders" IS 'Order records';` }])
119
- })
120
-
121
- it('returns undefined when no description is provided', () => {
122
- const ctx = makeCtx({
123
- target: 'table',
124
- objectName: 'users',
125
- tag: { name: '$self', args: [] },
126
- })
127
- const result = plugin.generateSQL!(ctx)
128
- expect(result).toBeUndefined()
129
- })
130
-
131
- it('returns undefined for column target without columnName', () => {
132
- const ctx = makeCtx({
133
- target: 'column',
134
- objectName: 'users',
135
- tag: { name: '$self', args: ['Some description here'] },
136
- })
137
- const result = plugin.generateSQL!(ctx)
138
- expect(result).toBeUndefined()
139
- })
140
-
141
- it('returns undefined for unknown tag names', () => {
142
- const ctx = makeCtx({
143
- tag: { name: 'unknown', args: ['Some text here'] },
144
- })
145
- const result = plugin.generateSQL!(ctx)
146
- expect(result).toBeUndefined()
147
- })
148
- })
149
-
150
- describe('validation', () => {
151
- it('returns info diagnostic for short comments (< 10 chars)', () => {
152
- const def = plugin.tags.$self!
153
- const result = def.validate!({
154
- target: 'table',
155
- lines: [],
156
- siblingTags: [],
157
- fileTags: [],
158
- argValues: ['Short'],
159
- objectName: 'users',
160
- })
161
- expect(result).toEqual({
162
- message: 'Consider a more descriptive comment',
163
- severity: 'info',
164
- })
165
- })
166
-
167
- it('returns undefined for sufficiently long comments', () => {
168
- const def = plugin.tags.$self!
169
- const result = def.validate!({
170
- target: 'table',
171
- lines: [],
172
- siblingTags: [],
173
- fileTags: [],
174
- argValues: ['This is a sufficiently long comment'],
175
- objectName: 'users',
176
- })
177
- expect(result).toBeUndefined()
178
- })
179
- })
180
- })