Skip to content

Commit

Permalink
Fix(VSCode): Cannot read properties of undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
1aron committed May 7, 2024
1 parent a54db39 commit 0028f73
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ export default class MasterCSS {
}
if (keys.length) {
EachRule.keys = keys
EachRule.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:`)
EachRule.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:.`)
}
})
}
Expand Down Expand Up @@ -467,7 +467,7 @@ export default class MasterCSS {
? this.styles[className].map((eachSyntax) => this.create(eachSyntax))
: [this.create(className)]
)
.filter(eachRule => eachRule && eachRule.text) as Rule[]
.filter(eachRule => eachRule && eachRule?.text) as Rule[]
}

/**
Expand Down
14 changes: 5 additions & 9 deletions packages/language-server/src/core.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createConnection, TextDocuments, InitializeParams, DidChangeConfigurationNotification, WorkspaceFolder, IPCMessageReader, IPCMessageWriter, Disposable, NotificationType } from 'vscode-languageserver/node'
import { createConnection, TextDocuments, InitializeParams, DidChangeConfigurationNotification, WorkspaceFolder, Disposable } from 'vscode-languageserver/node'
import { TextDocument } from 'vscode-languageserver-textdocument'
import path from 'path'
import { fileURLToPath } from 'url'
Expand All @@ -9,6 +9,7 @@ import extend from '@techor/extend'
import settings from './settings'
import { Config } from '@master/css'
import { SERVER_CAPABILITIES } from '@master/css-language-service'
import interceptLogs from './utils/intercept-logs'

export default class CSSLanguageServer {
workspaceFolders: WorkspaceFolder[] = []
Expand All @@ -23,16 +24,11 @@ export default class CSSLanguageServer {
public customSettings?: Settings
) {
if (process.argv.includes('--stdio')) {
console.log = (...args: any[]) => {
console.warn(...args)
}
this.connection = createConnection(process.stdin, process.stdout)
} else {
this.connection = createConnection(
new IPCMessageReader(process),
new IPCMessageWriter(process)
)
this.connection = createConnection()
}
interceptLogs(console, this.connection)
this.documents = new TextDocuments(TextDocument)
this.disposables.push(
this.documents.onDidSave(async change => {
Expand Down Expand Up @@ -187,4 +183,4 @@ export default class CSSLanguageServer {
this.disposables.forEach((disposable) => disposable.dispose())
this.disposables.length = 0
}
}
}
15 changes: 15 additions & 0 deletions packages/language-server/src/utils/intercept-logs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import type { Connection } from 'vscode-languageserver'
import { format } from 'node:util'

function formatMessages(params: any[]): string {
return params.map((item) => format(item)).join(' ')
}

export default function interceptLogs(console: Console, connection: Connection) {
console.debug = (...params: any[]) => connection.console.info(formatMessages(params))
console.error = (...params: any[]) => connection.console.error(formatMessages(params))
console.warn = (...params: any[]) => connection.console.warn(formatMessages(params))
console.info = (...params: any[]) => connection.console.info(formatMessages(params))
console.log = (...params: any[]) => connection.console.log(formatMessages(params))
return console
}

0 comments on commit 0028f73

Please sign in to comment.