Skip to content

Commit

Permalink
fix(lint): also lint tests except fixtures
Browse files Browse the repository at this point in the history
Signed-off-by: Max <[email protected]>
  • Loading branch information
max-nextcloud committed Oct 9, 2024
1 parent ab40a49 commit a103e4f
Show file tree
Hide file tree
Showing 32 changed files with 397 additions and 404 deletions.
3 changes: 1 addition & 2 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: AGPL-3.0-or-later

src/tests/
*.d.ts
src/tests/fixtures
17 changes: 12 additions & 5 deletions src/markdownit/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ const DETAILS_START_REGEX = /^<details>\s*$/
const DETAILS_END_REGEX = /^<\/details>\s*$/
const SUMMARY_REGEX = /(?<=^<summary>).*(?=<\/summary>\s*$)/

/**
*
* @param state
* @param startLine
* @param endLine
* @param silent
*/
function parseDetails(state: StateBlock, startLine: number, endLine: number, silent: boolean) {
// let autoClosedBlock = false
let start = state.bMarks[startLine] + state.tShift[startLine]
Expand Down Expand Up @@ -76,20 +83,20 @@ function parseDetails(state: StateBlock, startLine: number, endLine: number, sil
state.parentType = 'reference'

// This will prevent lazy continuations from ever going past our end marker
state.lineMax = nextLine;
state.lineMax = nextLine

// Push tokens to the state

let token = state.push('details_open', 'details', 1)
token.block = true
token.info = detailsSummary
token.map = [ startLine, nextLine ]
token.map = [startLine, nextLine]

token = state.push('details_summary', 'summary', 1)
token.block = false

// Parse and push summary to preserve markup
let tokens: Token[] = []
const tokens: Token[] = []
state.md.inline.parse(detailsSummary, state.md, state.env, tokens)
for (const t of tokens) {
token = state.push(t.type, t.tag, t.nesting)
Expand All @@ -100,7 +107,7 @@ function parseDetails(state: StateBlock, startLine: number, endLine: number, sil

token = state.push('details_summary', 'summary', -1)

state.md.block.tokenize(state, startLine + 2, nextLine);
state.md.block.tokenize(state, startLine + 2, nextLine)

token = state.push('details_close', 'details', -1)
token.block = true
Expand All @@ -117,6 +124,6 @@ function parseDetails(state: StateBlock, startLine: number, endLine: number, sil
*/
export default function details(md: MarkdownIt) {
md.block.ruler.before('fence', 'details', parseDetails, {
alt: [ 'paragraph', 'reference', 'blockquote', 'list' ],
alt: ['paragraph', 'reference', 'blockquote', 'list'],
})
}
38 changes: 16 additions & 22 deletions src/tests/builders.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { expect } from '@jest/globals';
import { Mark, Node } from '@tiptap/pm/model'
// eslint-disable-next-line n/no-extraneous-import
import { expect } from '@jest/globals'
import { Node } from '@tiptap/pm/model'
import { builders } from 'prosemirror-test-builder'
import { createRichEditor } from '../EditorFactory'

import { createRichEditor } from '../EditorFactory.js'

/**
* Get node builders from the default rich editor.
* @return {object}
*/
export function getBuilders() {
const editor = createRichEditor()
return builders(editor.schema, {
Expand All @@ -34,34 +38,24 @@ export const thead = getBuilders().thead

/**
* Create string representation of prosemirror / TipTap Node with attributes
* @param {Node} node
* @returns {string}
* @param {Node} node to serialize
* @return {string}
*/
function createDocumentString(node) {
/**
* Extract attributes of node or mark
* @param {Node|Mark} node
* @returns {string}
*/
const extractAttributes = (node) => {
const attrs = node.attrs || {}
const attrString = Object.keys(attrs)
.map((key) => {
// null is the TipTap default so we ignore it (e.g. a value of `unknown` must be manually set by the application)
if (attrs[key] !== null) {
return key + '=' + (typeof attrs[key] === 'string' ? `"${attrs[key]}"` : attrs[key])
}
return (attrs[key] === null)
? undefined
: key + '=' + (typeof attrs[key] === 'string' ? `"${attrs[key]}"` : attrs[key])
})
.filter(v => !!v)
.join(',')
return attrString ? `<${attrString}>` : ''
}

/**
* Create string representation of a single Node
* @param {Node} node
* @returns {string}
*/
const stringifyNode = (node) => {
const name = node.type.name
if (name === 'text') return '"' + node.text.replace('"', '\\"').replace('\n', '\\n') + '"'
Expand All @@ -83,9 +77,9 @@ function createDocumentString(node) {
* @example
* const editor = createRichEditor()
* expectDocument(editor.state.doc, table(
* tr(
* td('foo')
* )
* tr(
* td('foo')
* )
* ))
*/
export function expectDocument(subject, expected) {
Expand Down
16 changes: 8 additions & 8 deletions src/tests/extensions/Markdown.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import { Markdown } from './../../extensions/index.js'
import { createMarkdownSerializer } from './../../extensions/Markdown.js'
import CodeBlock from '@tiptap/extension-code-block'
import Blockquote from '@tiptap/extension-blockquote'
import { CodeBlock } from '@tiptap/extension-code-block'
import { Blockquote } from '@tiptap/extension-blockquote'
import Image from './../../nodes/Image.js'
import ImageInline from './../../nodes/ImageInline.js'
import TaskList from './../../nodes/TaskList.js'
import TaskItem from './../../nodes/TaskItem.js'
import { Italic, Strong, Underline, Link} from './../../marks/index.js'
import { Italic, Strong, Underline, Link } from './../../marks/index.js'
import TiptapImage from '@tiptap/extension-image'
import { getExtensionField } from '@tiptap/core'
import { __serializeForClipboard as serializeForClipboard } from '@tiptap/pm/view'
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Markdown extension integrated in the editor', () => {

it('copies address from blockquote to markdown', () => {
const editor = createCustomEditor({
content: '<blockquote><p>Hermannsreute 44A</p></blockquote>',
content: '<blockquote><p>Hermannsreute 44A</p></blockquote>',
extensions: [Markdown, Blockquote],
})
const text = copyEditorContent(editor)
Expand All @@ -123,7 +123,7 @@ describe('Markdown extension integrated in the editor', () => {

it('copy version number without escape character', () => {
const editor = createCustomEditor({
content: '<p>Hello</p><p>28.0.4</p>',
content: '<p>Hello</p><p>28.0.4</p>',
extensions: [Markdown],
})
const text = copyEditorContent(editor)
Expand All @@ -132,7 +132,7 @@ describe('Markdown extension integrated in the editor', () => {

it('strips bold, italic, and other marks from paragraph', () => {
const editor = createCustomEditor({
content: '<p><strong>Hello</strong></p><p><span style="text-decoration: underline;">lonely </span><em>world</em></p>',
content: '<p><strong>Hello</strong></p><p><span style="text-decoration: underline;">lonely </span><em>world</em></p>',
extensions: [Markdown, Italic, Strong, Underline],
})
const text = copyEditorContent(editor)
Expand All @@ -141,7 +141,7 @@ describe('Markdown extension integrated in the editor', () => {

it('strips href and link formatting from email address', () => {
const editor = createCustomEditor({
content: '<p>Hello</p><p><a href="mailto:[email protected]">[email protected]</a></p>',
content: '<p>Hello</p><p><a href="mailto:[email protected]">[email protected]</a></p>',
extensions: [Markdown, Link],
})
const text = copyEditorContent(editor)
Expand All @@ -150,7 +150,7 @@ describe('Markdown extension integrated in the editor', () => {

})

function copyEditorContent(editor) {
const copyEditorContent = (editor) => {
editor.commands.selectAll()
const slice = editor.state.selection.content()
const { text } = serializeForClipboard(editor.view, slice)
Expand Down
32 changes: 19 additions & 13 deletions src/tests/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { createMarkdownSerializer } from '../extensions/Markdown'
import { createMarkdownSerializer } from '../extensions/Markdown.js'
import { Editor } from '@tiptap/core'

import Document from '@tiptap/extension-document'
import Paragraph from '../nodes/Paragraph'
import Text from '@tiptap/extension-text'
import { Document } from '@tiptap/extension-document'
import { Text } from '@tiptap/extension-text'
import Paragraph from '../nodes/Paragraph.js'

import { createRichEditor } from '../EditorFactory'
import markdownit from '../markdownit'
import { createRichEditor } from '../EditorFactory.js'
import markdownit from '../markdownit/index.js'

/**
*
* @param {object} options for the editor
* @param {string} options.content Content for the editor.
* @param {Array} options.extensions Tip tap extensions
*/
export function createCustomEditor({ content, extensions }) {
return new Editor({
content,
Expand All @@ -21,15 +27,15 @@ export function createCustomEditor({ content, extensions }) {
Paragraph,
Text,
...extensions,
]
],
})
}

/**
* Ease markdown through TipTap editor and return serialized markdown
*
* @param {string} markdown
* @returns {string}
* @param {string} markdown to process
* @return {string}
*/
export function markdownThroughEditor(markdown) {
const tiptap = createRichEditor()
Expand All @@ -41,8 +47,8 @@ export function markdownThroughEditor(markdown) {
/**
* Ease HTML as input through the Editor and return the serialized markdown
*
* @param {string} html
* @returns {string}
* @param {string} html to process
* @return {string}
*/
export function markdownThroughEditorHtml(html) {
const tiptap = createRichEditor()
Expand All @@ -54,8 +60,8 @@ export function markdownThroughEditorHtml(html) {
/**
* Paste HTML into the Editor and return the serialized markdown
*
* @param {string} html
* @returns {string}
* @param {string} html to paste
* @return {string}
*/
export function markdownFromPaste(html) {
const tiptap = createRichEditor()
Expand Down
4 changes: 2 additions & 2 deletions src/tests/helpers/base64.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { encodeArrayBuffer, decodeArrayBuffer } from '../../helpers/base64'
import { encodeArrayBuffer, decodeArrayBuffer } from '../../helpers/base64.ts'

describe('encoding ArrayBuffer content', () => {
test('empty array buffer is empty base64 string', () => {
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('decoding base64 to array buffer', () => {
const encoded = 'AAAA'
const buffer = decodeArrayBuffer(encoded)
expect(buffer.byteLength).toBe(3)
const view = new Uint8Array(buffer);
const view = new Uint8Array(buffer)
expect(view).toEqual(new Uint8Array([0, 0, 0]))
})
})
22 changes: 11 additions & 11 deletions src/tests/helpers/links.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { domHref, parseHref } from '../../helpers/links'
import { domHref, parseHref } from '../../helpers/links.js'
import { loadState } from '@nextcloud/initial-state'

global.OCA = {
Expand All @@ -13,7 +13,7 @@ global.OCA = {
}

global.OC = {
config: {modRewriteWorking: true},
config: { modRewriteWorking: true },
}

global._oc_webroot = ''
Expand All @@ -30,7 +30,7 @@ describe('Preparing href attributes for the DOM', () => {
})

test('leave undefined hrefs alone', () => {
expect(domHref({attrs: {}})).toBe(undefined)
expect(domHref({ attrs: {} })).toBe(undefined)
})

test('full url', () => {
Expand Down Expand Up @@ -88,8 +88,8 @@ describe('Extracting short urls from the DOM', () => {

describe('Inserting hrefs into the dom and extracting them again', () => {

function insertAndExtract(attrs) {
const node = {attrs}
const insertAndExtract = (attrs) => {
const node = { attrs }
const dom = {
getAttribute() {
return domHref(node)
Expand All @@ -99,35 +99,35 @@ describe('Inserting hrefs into the dom and extracting them again', () => {
}

test('leave empty hrefs alone', () => {
expect(insertAndExtract({href: ''})).toBe('')
expect(insertAndExtract({ href: '' })).toBe('')
})

test('leave undefined hrefs alone', () => {
expect(insertAndExtract({})).toBe(undefined)
})

test('old relative link format (from file picker) is rewritten', () => {
expect(insertAndExtract({href: 'otherfile?fileId=123'}))
expect(insertAndExtract({ href: 'otherfile?fileId=123' }))
.toBe('http://localhost/f/123')
})

test('old relative link format with ../ (from file picker) is rewritten', () => {
expect(insertAndExtract({href: '../otherfile?fileId=123'}))
expect(insertAndExtract({ href: '../otherfile?fileId=123' }))
.toBe('http://localhost/f/123')
})

test('old absolute link format (from file picker) is rewritten', () => {
expect(insertAndExtract({href: '/otherfile?fileId=123'}))
expect(insertAndExtract({ href: '/otherfile?fileId=123' }))
.toBe('http://localhost/f/123')
})

test('default full URL link format is unchanged', () => {
expect(insertAndExtract({href: 'http://localhost/f/123'}))
expect(insertAndExtract({ href: 'http://localhost/f/123' }))
.toBe('http://localhost/f/123')
})

test('absolute link to collectives page is unchanged', () => {
expect(insertAndExtract({href: '/apps/collectives/page?fileId=123'}))
expect(insertAndExtract({ href: '/apps/collectives/page?fileId=123' }))
.toBe('/apps/collectives/page?fileId=123')
})

Expand Down
Loading

0 comments on commit a103e4f

Please sign in to comment.