Skip to content

Commit

Permalink
✅ Add specs for LinterRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
Steel Brain committed Aug 28, 2016
1 parent a56dd6c commit 83ca985
Show file tree
Hide file tree
Showing 5 changed files with 456 additions and 30 deletions.
1 change: 1 addition & 0 deletions decls/jasmine.js
Expand Up @@ -3,6 +3,7 @@
declare var jasmine: Object;
declare function it(name: string, callback: (() => void)): void;
declare function fit(name: string, callback: (() => void)): void;
declare function spyOn(object: Object, property: string): Object;
declare function expect(value: any): Object;
declare function describe(name: string, callback: (() => void)): void;
declare function fdescribe(name: string, callback: (() => void)): void;
Expand Down
39 changes: 22 additions & 17 deletions lib/linter-registry.js
Expand Up @@ -61,17 +61,14 @@ export default class LinterRegistry {
async lint({ onChange, editor } : { onChange: boolean, editor: TextEditor }): Promise<boolean> {
const filePath = editor.getPath()

/* eslint-disable no-multi-spaces */
if (
(onChange && !this.lintOnFly) || // Lint-on-fly mismatch
!filePath || // Not saved anywhere yet
editor !== atom.workspace.getActiveTextEditor() || // Not active
isPathIgnored(editor.getPath(), this.ignoreGlob, this.ignoreVCS) || // Ignored by VCS or Glob
(onChange && !this.lintOnFly) || // Lint-on-fly mismatch
!filePath || // Not saved anywhere yet
isPathIgnored(editor.getPath(), this.ignoreGlob, this.ignoreVCS) || // Ignored by VCS or Glob
(!this.lintPreviewTabs && editor.hasTerminatedPendingState === false) // Ignore Preview tabs
) {
return false
}
/* eslint-enable no-multi-spaces */

const scopes = editor.scopeDescriptorForBufferPosition(editor.getCursorBufferPosition()).getScopesArray()
scopes.push('*')
Expand All @@ -87,28 +84,36 @@ export default class LinterRegistry {

this.emitter.emit('did-begin-linting', { number, linter, filePath: statusFilePath })
promises.push(new Promise(function(resolve) {
// $FlowIgnore: Type too complex for flow?
// $FlowIgnore: Type too complex, duh
resolve(linter.lint(editor))
}).then(messages => {
this.emitter.emit('did-finish-linting', { number, linter, filePath: statusFilePath })
// TODO: Add spec to make sure if linter is per buffer and buffer is terminated before linter finished, we ignore it's results
if (linter[$requestLastReceived] >= number || !linter[$activated] || (statusBuffer && !statusBuffer.isAlive())) {
return
}
linter[$requestLastReceived] = number
if (statusBuffer && !statusBuffer.isAlive()) {
return
}

let validity = Array.isArray(messages) && (statusBuffer === null || statusBuffer.isAlive())
if (validity && atom.inDevMode()) {
if (!Array.isArray(messages)) {
this.emitter.emit('did-update-messages', { messages: [], linter, buffer: statusBuffer })
return
}

let validity = true
if (atom.inDevMode()) {
validity = linter[$version] === 2 ? validateMessages(linter.name, messages) : validateMessagesLegacy(linter.name, messages)
}
if (validity) {
if (linter[$version] === 2) {
normalizeMessages(linter.name, messages)
} else {
normalizeMessagesLegacy(linter.name, messages)
}
if (!validity) {
return
}
if (linter[$version] === 2) {
normalizeMessages(linter.name, messages)
} else {
normalizeMessagesLegacy(linter.name, messages)
}
this.emitter.emit('did-update-messages', { messages: messages || [], linter, buffer: statusBuffer })
this.emitter.emit('did-update-messages', { messages, linter, buffer: statusBuffer })
}, error => {
this.emitter.emit('did-finish-linting', { number, linter, filePath: statusFilePath })
atom.notifications.addError(`[Linter] Error running ${linter.name}`, {
Expand Down
25 changes: 19 additions & 6 deletions spec/common.js
@@ -1,19 +1,32 @@
/* @flow */

import Path from 'path'
import { normalizeMessages, normalizeMessagesLegacy } from '../lib/helpers'

export function getMessageLegacy(normalized: boolean = true): Object {
const message: Object = { type: 'Error', filePath: '/tmp/passwd', range: [[0, 1], [1, 0]], text: String(Math.random()) }
if (normalized) {
normalizeMessagesLegacy('Some Linter', [message])
export function getLinter(): Object {
return {
name: 'Some Linter',
scope: 'project',
grammarScopes: ['source.js'],
lint() {
return []
},
}
return message
}

export function getMessage(normalized: boolean = true): Object {
const message: Object = { severity: 'error', excerpt: String(Math.random()), location: { file: __filename, position: [[0, 0], [0, 0]] } }
if (normalized) {
normalizeMessages('Some Linter', [message])
}
return message
}
export function getMessageLegacy(normalized: boolean = true): Object {
const message: Object = { type: 'Error', filePath: '/tmp/passwd', range: [[0, 1], [1, 0]], text: String(Math.random()) }
if (normalized) {
normalizeMessagesLegacy('Some Linter', [message])
}
return message
}
export function getFixturesPath(path: string): string {
return Path.join(__dirname, 'fixtures', path)
}
8 changes: 1 addition & 7 deletions spec/helpers-spec.js
@@ -1,18 +1,12 @@
/* @flow */

import Path from 'path'
import { Disposable } from 'atom'
import { it, wait } from 'jasmine-fix'
import * as Helpers from '../lib/helpers'
import { getMessage, getMessageLegacy } from './common'
import { getFixturesPath, getMessage, getMessageLegacy } from './common'

describe('Helpers', function() {
// NOTE: Did *not* add specs for messageKey and messageKeyLegacy on purpose

function getFixturesPath(path: string): string {
return Path.join(__dirname, 'fixtures', path)
}

describe('shouldTriggerLinter', function() {
function shouldTriggerLinter(a: any, b: any, c: any) {
return Helpers.shouldTriggerLinter(a, b, c)
Expand Down

0 comments on commit 83ca985

Please sign in to comment.