From e60070cdcb3000bf07f8d7c60696f0933bc10f6b Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 16 Jul 2021 00:30:03 +0800 Subject: [PATCH] type: Fix type error caused by upgrade unified v10 #13 --- package.json | 15 +++++++----- src/index.ts | 21 +++++++++-------- src/utils.ts | 10 ++++---- src/visit.ts | 9 ++++--- {__tests__ => test}/index.test.ts | 37 +++++++++++++++-------------- tsconfig.json | 39 ++++++++++++++++++++++--------- 6 files changed, 77 insertions(+), 54 deletions(-) rename {__tests__ => test}/index.test.ts (95%) diff --git a/package.json b/package.json index 25084847..d8955ef5 100644 --- a/package.json +++ b/package.json @@ -33,24 +33,27 @@ "unified" ], "jest": { + "testMatch": [ + "/test/*.{ts,tsx}" + ], "collectCoverageFrom": [ - "lib/*.js", - "!lib/*.d.ts" + "/src/*.{tsx,ts}" + ], + "transformIgnorePatterns": [ + "node_modules/(?!(unified|bail|is-plain-obj|trough|vfile|vfile-message|unist-util-stringify-position)/)" ] }, "dependencies": { - "ts-mdast": "1.0.0", "unified": "10.0.0" }, "devDependencies": { - "@types/jest": "26.0.23", - "jest": "27.0.3", + "@types/jest": "26.0.24", "rehype": "11.0.0", "rehype-raw": "5.1.0", "rehype-stringify": "8.0.0", "remark-gfm": "1.0.0", "remark-parse": "9.0.0", "remark-rehype": "8.1.0", - "tsbb": "2.2.0" + "tsbb": "2.2.1" } } diff --git a/src/index.ts b/src/index.ts index 8dbe0a70..b23c49c8 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,9 +1,9 @@ -import { Root, Parent } from 'ts-mdast' -import {Plugin} from 'unified' +import { Plugin, Transformer } from 'unified' +import { Parent, NodeData, Node } from 'unist'; import visit from './visit' -import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils' +import { propertiesHandle, nextChild, prevChild, getCommentObject } from './utils'; -export type MdastTransformer = (tree: Root) => void +export type MdastTransformer = (tree: NodeData) => void export type RehypeAttrsOptions = { /** @@ -51,12 +51,13 @@ const defaultOptions: RehypeAttrsOptions = { properties: 'data' } -const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer =>{ +const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): Transformer => { const opts = { ...defaultOptions, ...options } - return transformer - function transformer(tree: Root) { - visit(tree, 'element', (node: Root, index: number, parent: Parent) => { - const codeNode = node && node.children && node.children[0] as any + return transformer; + function transformer(tree: Node>): void { + // ????? any + visit(tree as any, 'element', (node: NodeData, index: number, parent: NodeData) => { + const codeNode = node && node.children && Array.isArray(node.children) && node.children[0] if (node.tagName === 'pre' && codeNode && codeNode.tagName === 'code' && Array.isArray(parent.children) && parent.children.length > 1) { const child = prevChild(parent.children, index) if (child) { @@ -67,7 +68,7 @@ const rehypeAttrs: Plugin<[RehypeAttrsOptions?]> = (options): MdastTransformer = } } } - if (/^(em|strong|b|a|i|p|pre|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol)$/.test(node.tagName as string)) { + if (/^(em|strong|b|a|i|p|pre|blockquote|h(1|2|3|4|5|6)|code|table|img|del|ul|ol)$/.test(node.tagName as string) && Array.isArray(parent.children)) { const child = nextChild(parent.children, index) if (child) { const attr = getCommentObject(child) diff --git a/src/utils.ts b/src/utils.ts index 66fedfcd..0f52caab 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,4 @@ -import { Content } from 'ts-mdast' +import { Parent, NodeData } from 'unist'; import { RehypeAttrsOptions } from './' export const getURLParameters = (url: string): Record => @@ -11,11 +11,11 @@ export const getURLParameters = (url: string): Record { +export const prevChild = (data: NodeData[] = [], index: number): CommentData | undefined => { let i = index; while (i > -1) { i--; @@ -28,7 +28,7 @@ export const prevChild = (data: Content[] = [], index: number): CommentData | un return; } -export const nextChild = (data: Content[] = [], index: number, tagName?: string): CommentData | undefined => { +export const nextChild = (data: NodeData[] = [], index: number, tagName?: string): CommentData | undefined => { let i = index; while (i < data.length) { i++; @@ -71,7 +71,7 @@ export const getCommentObject = ({ value = '' }: CommentData): Record, attrs: Record, type: RehypeAttrsOptions['properties']) => { +export const propertiesHandle = (defaultAttrs?: Record | null, attrs?: Record | null, type?: RehypeAttrsOptions['properties']) => { if (type === 'string') { return { ...defaultAttrs, 'data-config': JSON.stringify({ ...attrs, rehyp: true })} } else if (type === 'attr') { diff --git a/src/visit.ts b/src/visit.ts index 33d22fc2..5804d7f2 100644 --- a/src/visit.ts +++ b/src/visit.ts @@ -1,8 +1,7 @@ -import { Root, Parent, Content } from 'ts-mdast' +import { Parent, NodeData } from 'unist'; -export type VisitCallback = (node: Root | Content | Parent, index: number, parent: Parent | Content) => void - -export default function visit(tree: Root, element: string, callback: VisitCallback) { +export type VisitCallback = (node: NodeData, index: number, parent: NodeData) => void; +export default function visit(tree?: NodeData, element?: string, callback?: VisitCallback) { if (!element || !tree || !callback || typeof callback !== 'function') { return } @@ -11,7 +10,7 @@ export default function visit(tree: Root, element: string, callback: VisitCallba } } -function handle(tree: Content[], element: string, parent: Parent | Content, callback: VisitCallback) { +function handle(tree: NodeData[], element: string, parent: NodeData, callback: VisitCallback) { tree.forEach((item, index) => { if (item.type === element) { callback(item, index, parent) diff --git a/__tests__/index.test.ts b/test/index.test.ts similarity index 95% rename from __tests__/index.test.ts rename to test/index.test.ts index 23a8eaaa..ec495ced 100644 --- a/__tests__/index.test.ts +++ b/test/index.test.ts @@ -1,21 +1,20 @@ -/// - -const unified = require("unified"); -const rehype = require('rehype') -const stringify = require('rehype-stringify') -const rehypeRaw = require('rehype-raw') -const remark2rehype = require('remark-rehype') -const remarkParse = require('remark-parse') -const gfm = require('remark-gfm') -const rehypeAttrs = require('../lib') -const utils = require('../lib/utils') -const visit = require('../lib/visit') +import { unified, Plugin } from 'unified' +import { Parent, NodeData } from 'unist'; +import rehype from 'rehype'; +import gfm from 'remark-gfm'; +import rehypeRaw from 'rehype-raw'; +import remark2rehype from 'remark-rehype'; +import remarkParse from 'remark-parse'; +import stringify from 'rehype-stringify'; +import rehypeAttrs from '../src'; +import * as utils from '../src/utils'; +import visit from '../src/visit'; const mrkStr = "\n```js\nconsole.log('')\n```" describe('rehype-attr function test case', () => { it('visit', async () => { - const node = { + const node: NodeData = { "type": "root", "children": [ { @@ -31,9 +30,9 @@ describe('rehype-attr function test case', () => { ], "data": { "quirksMode": false } } - visit(node, 'element', (node, index, parent) => { - expect(/(del|p)/.test(node.tagName)).toBeTruthy() - expect(typeof node).toEqual('object') + visit(node, 'element', (childNode, index, parent) => { + expect(/(del|p)/.test((childNode as any).tagName)).toBeTruthy() + expect(typeof childNode).toEqual('object') expect(typeof index).toEqual('number') expect(typeof parent).toEqual('object') }) @@ -387,9 +386,13 @@ describe('rehype-attr test case', () => { } ].forEach((data, idx) => { it(data.title, async () => { + const pluginWithoutOptions: Plugin = (options) => { + // expectType(options) + } + const htmlStr = rehype() .data('settings', { fragment: true }) - .use(rehypeAttrs, { properties: 'attr' }) + .use(rehypeAttrs as any, { properties: 'attr' }) .processSync(data.markdown) .toString() expect(htmlStr).toEqual(data.expected); diff --git a/tsconfig.json b/tsconfig.json index cedacfe3..e94ecce6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,22 +1,39 @@ { "compilerOptions": { + "jsx": "react", "target": "esnext", - "module": "commonjs", + "lib": ["dom", "dom.iterable", "esnext"], + "allowJs": true, + "skipLibCheck": true, + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "strict": true, + "forceConsistentCasingInFileNames": true, + "module": "esnext", "moduleResolution": "node", - "sourceMap": true, + "resolveJsonModule": true, + "isolatedModules": true, "declaration": true, + "baseUrl": ".", "noFallthroughCasesInSwitch": true, - "noImplicitThis": true, - "strictBindCallApply": true, - "strictNullChecks": true, - "strictPropertyInitialization": true, - "stripInternal": true, + "noEmit": true, + "types": ["jest", "node"], + // "types": ["jest", "node"], + - "esModuleInterop": false, - "noImplicitAny": true, "outDir": "lib", - "baseUrl": "." + + // // "module": "commonjs", + // "noImplicitThis": true, + // "strictBindCallApply": true, + // "strictNullChecks": true, + // "strictPropertyInitialization": true, + // "stripInternal": true, + // "noImplicitAny": true, }, "include": [ - "src/**/*", ] + "src/types/*", + "src", + "test" + ] }