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: Convert to TypeScript #1718

Merged
merged 40 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
bce2cb1
chore: install @types packages + remove flow decls
aminya Jan 4, 2021
4f9d3ca
chore: use prettier.config.js
aminya Jan 4, 2021
697b1ae
feat: convert js files to typescript using @khanacademy/flow-to-ts
aminya Jan 4, 2021
552f592
chore: remove flow deps
aminya Jan 4, 2021
c4d8ddb
chore: install typescript
aminya Jan 4, 2021
de12771
chore: remove babel
aminya Jan 4, 2021
83f7a3e
chore: add tsconfig
aminya Jan 4, 2021
c4d064f
chore: add tsc scripts + update rollup script
aminya Jan 4, 2021
9b59ae2
chore: add prettier ingore and separate format script
aminya Jan 4, 2021
83ae365
fix: import types manually
aminya Jan 4, 2021
a8f6ccd
chore: update test script
aminya Jan 4, 2021
727d894
test: test the built files
aminya Jan 4, 2021
08dfff7
chore: update eslint
aminya Jan 4, 2021
ee9a41c
chore: do not clean before rollup build automatically
aminya Jan 4, 2021
d6a561c
chore: use Record<string, any> instead of the verbose syntax
aminya Jan 4, 2021
af06566
chore: install types for lodash and minimatch
aminya Jan 4, 2021
339f826
chore: add disposed property for CompositeDisposable
aminya Jan 4, 2021
cf0ccef
chore: add windows requestIdleCallback types
aminya Jan 4, 2021
27899bb
chore: type for manifest
aminya Jan 4, 2021
98b3f2a
chore: type for oldRemoved
aminya Jan 4, 2021
776e4e5
chore: type for minimatch
aminya Jan 4, 2021
b7240d0
chore: type for solution
aminya Jan 4, 2021
2ccadd9
chore: type for indie
aminya Jan 4, 2021
34e70b6
chore: use Message type instead of Object
aminya Jan 4, 2021
8940c63
chore: add getPendingItem type
aminya Jan 4, 2021
c9a9095
chore: add messages type
aminya Jan 4, 2021
2c72414
chore: add getOptions type
aminya Jan 4, 2021
3d2f492
chore: use more strict tsconfig
aminya Jan 4, 2021
05f7b6a
chore: types for formatItem and sortByName
aminya Jan 4, 2021
1c757bd
chore: make SelectListView types any
aminya Jan 4, 2021
d440485
chore: fix types of subscriptiveObserve
aminya Jan 4, 2021
c5222c6
chore: fix getMessages types
aminya Jan 4, 2021
ea4863a
chore: fix update types
aminya Jan 4, 2021
9277451
chore: tsconfig: noImplicitAny
aminya Jan 4, 2021
47cbeb8
chore: use lambda to prevent this shadowing
aminya Jan 4, 2021
9e0220a
fix: move subscriptiveObserve to EditorLinter to fix unkown `this`
aminya Jan 4, 2021
5055289
chore: tsconfig noImplicitThis
aminya Jan 4, 2021
3ebcebc
chore: fix lint types
aminya Jan 4, 2021
f66d54e
chore: tsconfig strict
aminya Jan 4, 2021
0665a84
chore: remove unused eslint-disable
aminya Jan 4, 2021
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
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

17 changes: 4 additions & 13 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
{
"root": true,
"globals": {
"atom": true
},
"extends": "steelbrain",
"extends": "eslint-config-atomic/react",
"ignorePatterns": ["dist/", "node_modules/"],
"rules": {
"global-require": "off",
"no-param-reassign": "off",
"no-underscore-dangle": "off",
"no-duplicate-imports": "off",
"import/prefer-default-export": "off",
"import/no-unresolved": ["error", {
"ignore": ["atom"]
}]
"@typescript-eslint/no-inferrable-types": "off",
"@typescript-eslint/no-non-null-assertion": "off"
}
}
10 changes: 0 additions & 10 deletions .flowconfig

This file was deleted.

8 changes: 8 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
package.json
package-lock.json
pnpm-lock.yaml
changelog.md
coverage
build
dist
6 changes: 0 additions & 6 deletions .prettierrc

This file was deleted.

10 changes: 0 additions & 10 deletions babel.config.js

This file was deleted.

19 changes: 0 additions & 19 deletions decls/atom.js

This file was deleted.

11 changes: 0 additions & 11 deletions decls/jasmine.js

This file was deleted.

23 changes: 9 additions & 14 deletions lib/commands.js → lib/commands.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
/* @flow */

import { CompositeDisposable, Emitter } from 'atom'
import type { Disposable } from 'atom'

import * as Helpers from './helpers'
import type { Linter, UI } from './types'
import type IndieDelegate from './indie-delegate'

let manifest
let manifest: {version: number}

function formatItem(item) {
let itemName
function formatItem(item: {name: string} | string) {
let itemName: string
if (item && typeof item === 'object' && typeof item.name === 'string') {
itemName = item.name
} else if (typeof item === 'string') {
Expand All @@ -20,7 +18,7 @@ function formatItem(item) {
}
return ` - ${itemName}`
}
function sortByName(item1, item2) {
function sortByName(item1: {name: string}, item2: {name: string}) {
return item1.name.localeCompare(item2.name)
}

Expand Down Expand Up @@ -84,10 +82,7 @@ export default class Commands {

const ignoreGlob = atom.config.get('linter.ignoreGlob')
const ignoreVCSIgnoredPaths = atom.config.get('core.excludeVcsIgnoredPaths')
const disabledLinters = atom.config
.get('linter.disabledProviders')
.map(formatItem)
.join('\n')
const disabledLinters = atom.config.get('linter.disabledProviders').map(formatItem).join('\n')
const filePathIgnored = Helpers.isPathIgnored(textEditor.getPath(), ignoreGlob, ignoreVCSIgnoredPaths)

atom.notifications.addInfo('Linter Debug Info', {
Expand All @@ -108,16 +103,16 @@ export default class Commands {
dismissable: true,
})
}
onShouldLint(callback: Function): Disposable {
onShouldLint(callback: (...args: Array<any>) => any): Disposable {
return this.emitter.on('should-lint', callback)
}
onShouldDebug(callback: Function): Disposable {
onShouldDebug(callback: (...args: Array<any>) => any): Disposable {
return this.emitter.on('should-debug', callback)
}
onShouldToggleActiveEditor(callback: Function): Disposable {
onShouldToggleActiveEditor(callback: (...args: Array<any>) => any): Disposable {
return this.emitter.on('should-toggle-active-editor', callback)
}
onShouldToggleLinter(callback: Function): Disposable {
onShouldToggleLinter(callback: (...args: Array<any>) => any): Disposable {
return this.emitter.on('should-toggle-linter', callback)
}
dispose() {
Expand Down
32 changes: 26 additions & 6 deletions lib/editor-linter.js → lib/editor-linter.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/* @flow */

import { Emitter, CompositeDisposable, Disposable } from 'atom'
import debounce from 'lodash/debounce'
import type { TextEditor } from 'atom'
import { subscriptiveObserve } from './helpers'

export default class EditorLinter {
editor: TextEditor
Expand Down Expand Up @@ -36,7 +33,7 @@ export default class EditorLinter {
// NOTE: TextEditor::onDidChange immediately invokes the callback if the text editor was *just* created
// Using TextBuffer::onDidChange doesn't have the same behavior so using it instead.
this.subscriptions.add(
subscriptiveObserve(atom.config, 'linter.lintOnChangeInterval', interval =>
this.subscriptiveObserve(atom.config, 'linter.lintOnChangeInterval', interval =>
editorBuffer.onDidChange(
debounce(() => {
this.emitter.emit('should-lint', true)
Expand All @@ -51,15 +48,38 @@ export default class EditorLinter {
lint(onChange: boolean = false) {
this.emitter.emit('should-lint', onChange)
}
onShouldLint(callback: Function): Disposable {
onShouldLint(callback: (...args: Array<any>) => any): Disposable {
return this.emitter.on('should-lint', callback)
}
onDidDestroy(callback: Function): Disposable {
onDidDestroy(callback: (...args: Array<any>) => any): Disposable {
return this.emitter.on('did-destroy', callback)
}
dispose() {
this.emitter.emit('did-destroy')
this.subscriptions.dispose()
this.emitter.dispose()
}


subscriptiveObserve(
object: Record<string, any>,
eventName: string,
callback: (...args: Array<any>) => any,
): Disposable {
let subscription: Disposable | null = null
const eventSubscription = object.observe(eventName, (props: Record<string, any>) => {
if (subscription) {
subscription.dispose()
}
// TODO what is this?!
subscription = callback.call(this, props)
})

return new Disposable(function () {
eventSubscription.dispose()
if (subscription) {
subscription.dispose()
}
})
}
}
4 changes: 1 addition & 3 deletions lib/editor-registry.js → lib/editor-registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* @flow */

import { Emitter, CompositeDisposable } from 'atom'
import type { Disposable, TextEditor } from 'atom'
import EditorLinter from './editor-linter'
Expand Down Expand Up @@ -29,7 +27,7 @@ class EditorRegistry {
}),
)
}
get(textEditor: TextEditor): ?EditorLinter {
get(textEditor: TextEditor): EditorLinter | null | undefined {
return this.editorLinters.get(textEditor)
}
createFromTextEditor(textEditor: TextEditor): EditorLinter {
Expand Down
40 changes: 10 additions & 30 deletions lib/helpers.js → lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* @flow */

import arrayUnique from 'lodash/uniq'
import { Disposable, Range, Point } from 'atom'
import { Range, Point } from 'atom'
import type { TextEditor } from 'atom'
import type { Linter, Message } from './types'

Expand All @@ -14,7 +12,7 @@ export function shouldTriggerLinter(linter: Linter, wasTriggeredOnChange: boolea
if (wasTriggeredOnChange && !linter.lintsOnChange) {
return false
}
return scopes.some(function(scope) {
return scopes.some(function (scope) {
return linter.grammarScopes.includes(scope)
})
}
Expand All @@ -25,8 +23,8 @@ export function getEditorCursorScopes(textEditor: TextEditor): Array<string> {
)
}

let minimatch
export function isPathIgnored(filePath: ?string, ignoredGlob: string, ignoredVCS: boolean): boolean {
let minimatch: typeof import('minimatch')
export function isPathIgnored(filePath: string | null | undefined, ignoredGlob: string, ignoredVCS: boolean): boolean {
if (!filePath) {
return true
}
Expand All @@ -52,30 +50,12 @@ export function isPathIgnored(filePath: ?string, ignoredGlob: string, ignoredVCS
return minimatch(normalizedFilePath, ignoredGlob)
}

export function subscriptiveObserve(object: Object, eventName: string, callback: Function): Disposable {
let subscription = null
const eventSubscription = object.observe(eventName, function(props) {
if (subscription) {
subscription.dispose()
}
subscription = callback.call(this, props)
})

return new Disposable(function() {
eventSubscription.dispose()
if (subscription) {
subscription.dispose()
}
})
}

export function updateMessageKey(message: Message) {
const { reference, location } = message
message.key = [
`$LINTER:${message.linterName}`,
`$LOCATION:${location.file}$${location.position.start.row}$${location.position.start.column}$${
location.position.end.row
}$${location.position.end.column}`,
`$LOCATION:${location.file}$${location.position.start.row}$${location.position.start.column}$${location.position.end.row}$${location.position.end.column}`,
reference
? `$REFERENCE:${reference.file}$${reference.position ? `${reference.position.row}$${reference.position.column}` : ''}`
: '$REFERENCE:null',
Expand All @@ -98,7 +78,7 @@ export function normalizeMessages(linterName: string, messages: Array<Message>)
reference.position = Point.fromObject(reference.position)
}
if (message.solutions && message.solutions.length) {
for (let j = 0, _length = message.solutions.length, solution; j < _length; j++) {
for (let j = 0, _length = message.solutions.length, solution: Message['solutions'][0]; j < _length; j++) {
solution = message.solutions[j]
if (Array.isArray(solution.position)) {
solution.position = Range.fromObject(solution.position)
Expand Down Expand Up @@ -131,9 +111,9 @@ export function createKeyMessageMap(messages: Array<Message>): Map<string, Messa
}

interface FlaggedMessages {
oldKept: Array<Message>;
oldRemoved: Array<Message>;
newAdded: Array<Message>;
oldKept: Array<Message>
oldRemoved: Array<Message>
newAdded: Array<Message>
}

// This fast function returns the new messages and old messages by comparing their key against the cache.
Expand Down Expand Up @@ -181,7 +161,7 @@ export function flagMessages(inputs: Array<Message>, oldMessages: Array<Message>

const oldRemovedKeys = cacheKeys.filter(x => !oldKeptKeys.includes(x))

const oldRemoved = new Set()
const oldRemoved: Set<Message> = new Set()
for (let iRemoved = 0, RemovedKeysLen = oldRemovedKeys.length; iRemoved < RemovedKeysLen; iRemoved++) {
oldRemoved.add(cache.get(oldRemovedKeys[iRemoved]))
}
Expand Down
10 changes: 4 additions & 6 deletions lib/index.js → lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* @flow */

import { CompositeDisposable, Disposable } from 'atom'

import Linter from './main'
import type { UI, Linter as LinterProvider } from './types'
import type { UI, Linter as LinterProvider, Indie } from './types'

// Internal variables
let instance: Linter
Expand All @@ -16,7 +14,7 @@ export function activate() {
subscriptions.add(instance)

subscriptions.add(
atom.packages.onDidActivateInitialPackages(function() {
atom.packages.onDidActivateInitialPackages(function () {
if (!atom.inSpecMode()) {
require('atom-package-deps').install('linter', true)
}
Expand Down Expand Up @@ -45,8 +43,8 @@ export function consumeUI(ui: UI): Disposable {
}
})
}
export function provideIndie(): Object {
return indie => instance.addIndie(indie)
export function provideIndie() {
return (indie: Indie) => instance.addIndie(indie)
}
export function deactivate() {
subscriptions.dispose()
Expand Down
Loading