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

feat: dim @ignore fields and @@ignore models #1285

Merged
merged 7 commits into from
Nov 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions packages/language-server/src/MessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
printLogMessage,
isRelationField,
} from './rename/renameUtil'
import { createDiagnosticsForIgnore } from './diagnosticsHandler'

export function handleDiagnosticsRequest(
document: TextDocument,
Expand Down Expand Up @@ -124,6 +125,10 @@ export function handleDiagnosticsRequest(
}
}

const lines = convertDocumentTextToTrimmedLineArray(document)
const diagnosticsForIgnore = createDiagnosticsForIgnore(lines)
diagnostics.push(...diagnosticsForIgnore)

return diagnostics
}

Expand Down
42 changes: 41 additions & 1 deletion packages/language-server/src/__test__/linting.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TextDocument } from 'vscode-languageserver-textdocument'
import { handleDiagnosticsRequest } from '../MessageHandler'
import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver'
import { Diagnostic, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver'
import * as assert from 'assert'
import { getTextDocument } from './helper'

Expand All @@ -15,12 +15,15 @@ function assertLinting(expected: Diagnostic[], fixturePath: string): void {
assert.strictEqual(actualDiagnostic.message, expectedDiagnostic.message)
assert.deepStrictEqual(actualDiagnostic.range, expectedDiagnostic.range)
assert.strictEqual(actualDiagnostic.severity, expectedDiagnostic.severity)
assert.deepStrictEqual(actualDiagnostic.tags, expectedDiagnostic.tags)
})
}

suite('Linting', () => {
const fixturePathMissingArgument = './linting/missingArgument.prisma'
const fixturePathWrongType = './linting/wrongType.prisma'
const fixturePathFieldIgnore = './linting/@ignore.prisma'
const fixturePathModelIgnore = './linting/@@ignore.prisma'

test('Missing argument', () => {
assertLinting(
Expand All @@ -37,6 +40,7 @@ suite('Linting', () => {
fixturePathMissingArgument,
)
})

test('Wrong type', () => {
assertLinting(
[
Expand All @@ -52,4 +56,40 @@ suite('Linting', () => {
fixturePathWrongType,
)
})

test('@ignore : Field', () => {
assertLinting(
[
{
range: {
start: { line: 12, character: 0 },
end: { line: 12, character: Number.MAX_VALUE },
},
message:
'@ignore: When using Prisma Migrate, this field will be kept in sync with the database schema, however, it will not be exposed in Prisma Client.',
tags: [DiagnosticTag.Unnecessary],
severity: DiagnosticSeverity.Hint,
},
],
fixturePathFieldIgnore,
)
})

test('@@ignore : Model', () => {
assertLinting(
[
{
range: {
start: { line: 9, character: 0 },
end: { line: 17, character: 1 },
},
message:
'@@ignore: When using Prisma Migrate, this model will be kept in sync with the database schema, however, it will not be exposed in Prisma Client.',
tags: [DiagnosticTag.Unnecessary],
severity: DiagnosticSeverity.Hint,
},
],
fixturePathModelIgnore,
)
})
})
42 changes: 42 additions & 0 deletions packages/language-server/src/diagnosticsHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Diagnostic, DiagnosticSeverity, DiagnosticTag } from 'vscode-languageserver/node'
import { getBlockAtPosition } from './util'

export function createDiagnosticsForIgnore(lines: string[]): Diagnostic[] {
const diagnostics: Diagnostic[] = []

lines.map((currElement, index) => {
if (currElement.includes('@@ignore')) {
const block = getBlockAtPosition(index, lines)
if (block) {
diagnostics.push({
range: { start: block.range.start, end: block.range.end },
message:
'@@ignore: When using Prisma Migrate, this model will be kept in sync with the database schema, however, it will not be exposed in Prisma Client.',
tags: [DiagnosticTag.Unnecessary],
Druue marked this conversation as resolved.
Show resolved Hide resolved
severity: DiagnosticSeverity.Hint,
code: '@@ignore documentation',
codeDescription: {
href: 'https://pris.ly/d/schema-reference#ignore-1',
},
})
}
} else if (currElement.includes('@ignore')) {
diagnostics.push({
range: {
start: { line: index, character: 0 },
end: { line: index, character: Number.MAX_VALUE },
},
message:
'@ignore: When using Prisma Migrate, this field will be kept in sync with the database schema, however, it will not be exposed in Prisma Client.',
tags: [DiagnosticTag.Unnecessary],
severity: DiagnosticSeverity.Hint,
code: '@ignore documentation',
codeDescription: {
href: 'https://pris.ly/d/schema-reference#ignore',
},
})
}
})

return diagnostics
}
24 changes: 24 additions & 0 deletions packages/language-server/test/fixtures/linting/@@ignore.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
authorId Int?

@@ignore
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
24 changes: 24 additions & 0 deletions packages/language-server/test/fixtures/linting/@ignore.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

generator client {
provider = "prisma-client-js"
}

model Post {
id Int @id @default(autoincrement())
title String
content String? @ignore
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}