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

Migrate from TSLint to ESLint #157

Merged
merged 7 commits into from
Jul 25, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ commands:
command: yarn check:ts

- run:
name: TSLint
command: yarn lint:ts
name: ESLint
command: yarn lint:js

- run:
name: stylelint
Expand Down
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage
lib
node_modules
36 changes: 36 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
extends: ['eslint:recommended', 'plugin:import/recommended', 'prettier'],
rules: {
'import/order': ['error', { alphabetize: { order: 'asc' } }],
},
settings: {
'import/resolver': { typescript: { alwaysTryTypes: true } },
},
overrides: [
{
files: ['**/*.ts'],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:import/typescript',
'prettier/@typescript-eslint',
],
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
},
},
{
files: ['**/*.test.js', '**/*.test.ts'],
plugins: ['jest'],
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
},
],
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

- Upgrade to [Marp Core v1.2.2](https://github.com/marp-team/marp-core/releases/v1.2.2) and [Marp CLI v0.20.0](https://github.com/marp-team/marp-cli/releases/v0.20.0) ([#151](https://github.com/marp-team/marp-vscode/pull/151))
- Upgrade development Node and dependent packages to the latest version ([#151](https://github.com/marp-team/marp-vscode/pull/151))
- Migrate from TSLint to ESLint ([#157](https://github.com/marp-team/marp-vscode/pull/157))

## v0.14.0 - 2020-06-14

Expand Down
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@
"format": "prettier \"**/*.{css,js,json,md,scss,ts,yaml,yml}\"",
"format:write": "yarn -s format --write",
"lint:css": "stylelint \"./*.{css,scss}\"",
"lint:ts": "tslint \"{src,test}/**/*.ts\"",
"lint:js": "eslint --ext .js,.ts --report-unused-disable-directives --cache .",
"package": "npx vsce package",
"preversion": "run-p check:* lint:* test:*:coverage",
"test:unit": "jest",
Expand All @@ -228,9 +228,16 @@
"@types/markdown-it": "^10.0.1",
"@types/vscode": "~1.43.0",
"@types/yaml": "^1.9.7",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"builtin-modules": "^3.1.0",
"codecov": "^3.7.2",
"dedent": "^0.7.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"eslint-import-resolver-typescript": "^2.0.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jest": "^23.18.0",
"jest": "^26.1.0",
"jest-junit": "^11.0.1",
"lodash.debounce": "^4.0.8",
Expand All @@ -247,9 +254,6 @@
"stylelint-config-prettier": "^8.0.2",
"stylelint-config-standard": "^20.0.0",
"ts-jest": "^26.1.3",
"tslint": "^6.1.2",
"tslint-config-airbnb": "^5.11.2",
"tslint-config-prettier": "^1.18.0",
"typescript": "^3.9.7",
"unified": "^9.1.0",
"unist-util-visit": "^2.0.3",
Expand Down
3 changes: 1 addition & 2 deletions src/__mocks__/vscode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* tslint:disable: variable-name */
type MockedConf = Record<string, any>

const defaultVSCodeVersion = 'v1.36.0'
Expand Down Expand Up @@ -54,7 +53,7 @@ export enum DiagnosticSeverity {
}

export class Position {
translate(lineDelta: number = 0, characterDelta: number = 0) {
translate(lineDelta = 0, characterDelta = 0) {
return new Position(this.line + lineDelta, this.character + characterDelta)
}

Expand Down
38 changes: 21 additions & 17 deletions src/commands/export.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { env, window, workspace } from 'vscode'
import * as exportModule from './export'
import * as marpCli from '../marp-cli'
import * as exportModule from './export'

const exportCommand = exportModule.default

jest.mock('vscode')

const setConfiguration: (conf?: object) => void = (workspace as any)
._setConfiguration
const setConfiguration: (
conf?: Record<string, unknown>
) => void = (workspace as any)._setConfiguration

describe('Export command', () => {
let saveDialog: jest.SpyInstance
Expand All @@ -20,33 +21,36 @@ describe('Export command', () => {
window.activeTextEditor = undefined

await exportCommand()
expect(saveDialog).not.toBeCalled()
expect(saveDialog).not.toHaveBeenCalled()
})

it('opens save dialog when active text editor is Markdown', async () => {
window.activeTextEditor = { document: { languageId: 'markdown' } } as any
const textEditor = { document: { languageId: 'markdown' } }
window.activeTextEditor = textEditor as any

await exportCommand()
expect(saveDialog).toBeCalledWith(window.activeTextEditor!.document)
expect(saveDialog).toHaveBeenCalledWith(textEditor.document)
})

describe('when active text editor is not Markdown', () => {
const textEditor = { document: { languageId: 'plaintext' } }

beforeEach(() => {
window.activeTextEditor = { document: { languageId: 'plaintext' } } as any
window.activeTextEditor = textEditor as any
})

it('shows warning notification', async () => {
await exportCommand()
expect(saveDialog).not.toBeCalled()
expect(window.showWarningMessage).toBeCalled()
expect(saveDialog).not.toHaveBeenCalled()
expect(window.showWarningMessage).toHaveBeenCalled()
})

it('continues exporting when reacted on the notification to continue', async () => {
const { showWarningMessage }: any = window
showWarningMessage.mockResolvedValue(exportModule.ITEM_CONTINUE_TO_EXPORT)

await exportCommand()
expect(saveDialog).toBeCalledWith(window.activeTextEditor!.document)
expect(saveDialog).toHaveBeenCalledWith(textEditor.document)
})
})
})
Expand All @@ -57,7 +61,7 @@ describe('#saveDialog', () => {
it('opens save dialog with default URI', async () => {
await exportModule.saveDialog(document)

expect(window.showSaveDialog).toBeCalledWith(
expect(window.showSaveDialog).toHaveBeenCalledWith(
expect.objectContaining({
defaultUri: expect.objectContaining({ fsPath: '/tmp/test' }),
})
Expand All @@ -68,7 +72,7 @@ describe('#saveDialog', () => {
setConfiguration({ 'markdown.marp.exportType': 'pptx' })

await exportModule.saveDialog(document)
expect(window.showSaveDialog).toBeCalled()
expect(window.showSaveDialog).toHaveBeenCalled()

const { filters } = (window.showSaveDialog as jest.Mock).mock.calls[0][0]
expect(Object.values(filters)[0]).toStrictEqual(['pptx'])
Expand All @@ -83,12 +87,12 @@ describe('#saveDialog', () => {
.mockImplementation()

await exportModule.saveDialog(document)
expect(window.withProgress).toBeCalledWith(
expect(window.withProgress).toHaveBeenCalledWith(
expect.objectContaining({ title: expect.stringContaining('PATH') }),
expect.any(Function)
)
;(window.withProgress as any).mock.calls[0][1]()
expect(doExportMock).toBeCalledWith(saveURI, document)
expect(doExportMock).toHaveBeenCalledWith(saveURI, document)
})
})

Expand All @@ -100,15 +104,15 @@ describe('#doExport', () => {
const runMarpCLI = jest.spyOn(marpCli, 'default').mockImplementation()

await exportModule.doExport(saveURI, document)
expect(runMarpCLI).toBeCalled()
expect(env.openExternal).toBeCalledWith(saveURI)
expect(runMarpCLI).toHaveBeenCalled()
expect(env.openExternal).toHaveBeenCalledWith(saveURI)
})

it('shows warning when Marp CLI throws error', async () => {
jest.spyOn(marpCli, 'default').mockRejectedValue(new Error('ERROR'))

await exportModule.doExport(saveURI, document)
expect(window.showErrorMessage).toBeCalledWith(
expect(window.showErrorMessage).toHaveBeenCalledWith(
expect.stringContaining('[Error] ERROR')
)
})
Expand Down
3 changes: 2 additions & 1 deletion src/commands/export.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import path from 'path'
import { env, ProgressLocation, TextDocument, Uri, window } from 'vscode'
import { marpConfiguration } from '../utils'
import marpCli, {
createConfigFile,
createWorkFile,
MarpCLIError,
} from '../marp-cli'
import { marpConfiguration } from '../utils'

export enum Types {
html = 'html',
Expand Down Expand Up @@ -64,6 +64,7 @@ export const doExport = async (uri: Uri, document: TextDocument) => {
export const saveDialog = async (document: TextDocument) => {
const { fsPath } = document.uri

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const defaultType = marpConfiguration().get<string>('exportType')!
const baseTypes = Object.keys(extensions)
const types = [...new Set<string>([defaultType, ...baseTypes])]
Expand Down
2 changes: 1 addition & 1 deletion src/commands/open-extension-settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('openExtensionSettings command', () => {
it('executes "workbench.action.openSettings" command with filter for Marp extension', async () => {
await openExtensionSettings()

expect(commands.executeCommand).toBeCalledWith(
expect(commands.executeCommand).toHaveBeenCalledWith(
'workbench.action.openSettings',
'@ext:marp-team.marp-vscode'
)
Expand Down
4 changes: 2 additions & 2 deletions src/commands/show-quick-pick.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ describe('showQuickPick command', () => {
}

await showQuickPick()
expect(window.showQuickPick).toBeCalledWith(
expect(window.showQuickPick).toHaveBeenCalledWith(
expect.arrayContaining([expectedItem]),
expect.anything()
)
Expand All @@ -24,6 +24,6 @@ describe('showQuickPick command', () => {
} as any)

await showQuickPick()
expect(commands.executeCommand).toBeCalledWith('example.command')
expect(commands.executeCommand).toHaveBeenCalledWith('example.command')
})
})
2 changes: 1 addition & 1 deletion src/commands/show-quick-pick.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { commands, QuickPickItem, window } from 'vscode'
import { contributes } from '../../package.json'
import { command as exportCommand } from './export'
import { command as toggleMarpPreviewCommand } from './toggle-marp-preview'
import { command as openExtensionSettingsCommand } from './open-extension-settings'
import { command as toggleMarpPreviewCommand } from './toggle-marp-preview'

export const cmdSymbol = Symbol()

Expand Down
28 changes: 15 additions & 13 deletions src/commands/toggle-marp-preview.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,27 @@ describe('toggleMarpPreview command', () => {
window.activeTextEditor = undefined

await toggleMarpPreviewCommand()
expect(toggleFunc).not.toBeCalled()
expect(toggleFunc).not.toHaveBeenCalled()
})

it('runs toggle function when active text editor is Markdown', async () => {
window.activeTextEditor = { document: { languageId: 'markdown' } } as any

await toggleMarpPreviewCommand()
expect(toggleFunc).toBeCalledWith(window.activeTextEditor)
expect(toggleFunc).toHaveBeenCalledWith(window.activeTextEditor)
})

describe('when active text editor is not Markdown', () => {
const textEditor = { document: { languageId: 'plaintext' } }

beforeEach(() => {
window.activeTextEditor = { document: { languageId: 'plaintext' } } as any
window.activeTextEditor = textEditor as any
})

it('shows warning notification', async () => {
await toggleMarpPreviewCommand()
expect(toggleFunc).not.toBeCalled()
expect(window.showWarningMessage).toBeCalled()
expect(toggleFunc).not.toHaveBeenCalled()
expect(window.showWarningMessage).toHaveBeenCalled()
})

it('changes editor language and continues process when reacted on the notification', async () => {
Expand All @@ -44,11 +46,11 @@ describe('toggleMarpPreview command', () => {
)

await toggleMarpPreviewCommand()
expect(languages.setTextDocumentLanguage).toBeCalledWith(
window.activeTextEditor!.document,
expect(languages.setTextDocumentLanguage).toHaveBeenCalledWith(
textEditor.document,
'markdown'
)
expect(toggleFunc).toBeCalledWith(window.activeTextEditor)
expect(toggleFunc).toHaveBeenCalledWith(window.activeTextEditor)
})
})
})
Expand Down Expand Up @@ -80,7 +82,7 @@ describe('#toggle', () => {
const editor = textEditor('')
await toggleMarpPreview.toggle(editor)

expect(editor._editBuilders[0].insert).toBeCalledWith(
expect(editor._editBuilders[0].insert).toHaveBeenCalledWith(
new Position(0, 0),
'---\nmarp: true\n---\n\n'
)
Expand All @@ -90,7 +92,7 @@ describe('#toggle', () => {
const editor = textEditor('---\ntest: abc\nfoo: bar\n---')
await toggleMarpPreview.toggle(editor)

expect(editor._editBuilders[0].insert).toBeCalledWith(
expect(editor._editBuilders[0].insert).toHaveBeenCalledWith(
new Position(3, 0),
'marp: true\n'
)
Expand All @@ -99,7 +101,7 @@ describe('#toggle', () => {
const editorEmptyFm = textEditor('---\n---')
await toggleMarpPreview.toggle(editorEmptyFm)

expect(editorEmptyFm._editBuilders[0].insert).toBeCalledWith(
expect(editorEmptyFm._editBuilders[0].insert).toHaveBeenCalledWith(
new Position(1, 0),
'marp: true\n'
)
Expand All @@ -110,7 +112,7 @@ describe('#toggle', () => {
const editorEnabled = textEditor('---\nmarp: true\n---')
await toggleMarpPreview.toggle(editorEnabled)

expect(editorEnabled._editBuilders[0].replace).toBeCalledWith(
expect(editorEnabled._editBuilders[0].replace).toHaveBeenCalledWith(
new Range(new Position(1, 6), new Position(1, 10)),
'false'
)
Expand All @@ -119,7 +121,7 @@ describe('#toggle', () => {
const editorDisabled = textEditor('---\nfoo: bar\nmarp: false\n---')
await toggleMarpPreview.toggle(editorDisabled)

expect(editorDisabled._editBuilders[0].replace).toBeCalledWith(
expect(editorDisabled._editBuilders[0].replace).toHaveBeenCalledWith(
new Range(new Position(2, 8), new Position(2, 13)),
'true'
)
Expand Down
12 changes: 6 additions & 6 deletions src/commands/toggle-marp-preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ export const toggle = async (editor: TextEditor) => {
line += 1
}

if (targetRange && toggleValue) {
await editor.edit((e) => e.replace(targetRange!, toggleValue!))
} else {
await editor.edit((e) =>
await editor.edit((e) => {
if (targetRange && toggleValue) {
e.replace(targetRange, toggleValue)
} else {
e.insert(new Position(line - 1, 0), 'marp: true\n')
)
}
}
})
} else {
await editor.edit((e) =>
e.insert(new Position(0, 0), '---\nmarp: true\n---\n\n')
Expand Down
Loading