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

Replace mocks/stubs in tests with LSP #21

Merged
merged 15 commits into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ jobs:
strategy:
matrix:
os:
- ubuntu-latest
# - ubuntu-latest
- windows-latest
node:
- lts/erbium
# - lts/erbium
- node
46 changes: 26 additions & 20 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,15 @@ function isValidUnistPoint(point) {
*/
function unistLocationToLspRange(position) {
if (position) {
if (isValidUnistPoint(position.start)) {
if (isValidUnistPoint(position.end)) {
return Range.create(
unistPointToLspPosition(position.start),
unistPointToLspPosition(position.end)
)
}

const start = unistPointToLspPosition(position.start)
return Range.create(start, start)
}

if (isValidUnistPoint(position.end)) {
const end = unistPointToLspPosition(position.end)
return Range.create(end, end)
const end = isValidUnistPoint(position.end)
? unistPointToLspPosition(position.end)
: undefined
const start = isValidUnistPoint(position.start)
? unistPointToLspPosition(position.start)
: end

if (start) {
return Range.create(start, end || start)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easier to reach coverage

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn’t catch the situation if end is a valid unist point, but start isn’t. I recall running into this in a real rule while testing manually, but I don’t remember which one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should work: see L72’s else case on L74!

(it’s a rather weird case you ran into btw, I don’t remember seeing something like that. Still: I’m all for very “defensive” / “safe” code for this 👍)

}
}

Expand All @@ -96,7 +90,7 @@ function unistLocationToLspRange(position) {
function vfileMessageToDiagnostic(message) {
const diagnostic = Diagnostic.create(
unistLocationToLspRange(message.position),
message.reason,
String(message.stack || message.reason),
wooorm marked this conversation as resolved.
Show resolved Hide resolved
message.fatal === true
? DiagnosticSeverity.Error
: message.fatal === false
Expand Down Expand Up @@ -175,7 +169,7 @@ export function configureUnifiedLanguageServer(
streamError: new PassThrough(),
streamOut: new PassThrough()
},
(error, code, context) => {
(error, _, context) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer using a proper name for variables, even if they are unused.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main reason for me is that TS in my editor is info’ing about it being unused otherwise.

I’d be 🤷‍♂️ otherwise

// An error never occur and can’t be reproduced. Thus us ab internal
// error in unified-engine. If a plugin throws, it’s reported as a
// vfile message.
Expand All @@ -185,7 +179,7 @@ export function configureUnifiedLanguageServer(
} else {
resolve((context && context.files) || [])
}
/* c8 ignore end */
/* c8 ignore stop */
wooorm marked this conversation as resolved.
Show resolved Hide resolved
}
)
})
Expand Down Expand Up @@ -227,8 +221,12 @@ export function configureUnifiedLanguageServer(
}
}))

connection.onDocumentFormatting(async ({textDocument: {uri}}) => {
connection.onDocumentFormatting(async (event) => {
const {uri} = event.textDocument
const document = documents.get(uri)
wooorm marked this conversation as resolved.
Show resolved Hide resolved

// `vscode-languageserver` crashes for commands to format unopen documents.
/* c8 ignore next 3 */
if (!document) {
return
}
Expand All @@ -243,13 +241,16 @@ export function configureUnifiedLanguageServer(
const start = Position.create(0, 0)
const end = document.positionAt(text.length)

// V8 coverage bug on Dubnium (Node 12).
wooorm marked this conversation as resolved.
Show resolved Hide resolved
/* c8 ignore next 2 */
return [TextEdit.replace(Range.create(start, end), result)]
})

documents.onDidChangeContent((event) => {
checkDocuments(event.document)
})

// Send empty diagnostics for closed files.
documents.onDidClose((event) => {
const {uri, version} = event.document
connection.sendDiagnostics({
Expand All @@ -259,15 +260,20 @@ export function configureUnifiedLanguageServer(
})
})

// Check everything again if something changes.
wooorm marked this conversation as resolved.
Show resolved Hide resolved
connection.onDidChangeWatchedFiles(() => {
checkDocuments(...documents.all())
})

connection.onCodeAction((event) => {
const {uri} = event.textDocument
/** @type {CodeAction[]} */
const codeActions = []

const document = documents.get(event.textDocument.uri)
const document = documents.get(uri)
wooorm marked this conversation as resolved.
Show resolved Hide resolved

// `vscode-languageserver` crashes for commands to act on unopen documents.
/* c8 ignore next 3 */
if (!document) {
return
}
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,12 @@
"vscode-languageserver-textdocument": "^1.0.0"
},
"devDependencies": {
"@types/sinon": "^10.0.0",
"@types/tape": "^4.0.0",
"c8": "^7.0.0",
"execa": "^6.0.0",
ChristianMurphy marked this conversation as resolved.
Show resolved Hide resolved
"prettier": "^2.0.0",
"remark-cli": "^10.0.0",
"remark-preset-wooorm": "^9.0.0",
"sinon": "^12.0.0",
wooorm marked this conversation as resolved.
Show resolved Hide resolved
"tape": "^5.0.0",
"type-coverage": "^2.0.0",
"typescript": "^4.0.0",
Expand All @@ -59,7 +58,7 @@
"build": "rimraf \"lib/**/*.d.ts\" \"test/**/*.d.ts\" \"*.d.ts\" && tsc && type-coverage",
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
"test-api": "node --unhandled-rejections=strict --conditions development test/index.js",
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api",
"test": "npm run build && npm run format && npm run test-coverage"
},
"prettier": {
Expand Down
Loading