Răsfoiți Sursa

format test file

Yuki Takei 3 ani în urmă
părinte
comite
4e58b2967c

+ 0 - 1
packages/remark-growi-plugin/.eslintignore

@@ -1,2 +1 @@
 /dist/**
-/test/**

+ 1 - 1
packages/remark-growi-plugin/package.json

@@ -21,7 +21,7 @@
     "tsc": "tsc -p tsconfig.build.json",
     "tsc:w": "yarn tsc -w",
     "test": "cross-env NODE_ENV=test npm run test-coverage",
-    "test-api": "node --conditions development test/index.js",
+    "test-api": "node --conditions development test/**.test.js",
     "test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov npm run test-api",
     "lint": "eslint \"**/*.{cjs, js,jsx,ts,tsx}\"",
     "lint:fix": "eslint \"**/*.{cjs, js,jsx,ts,tsx}\" --fix"

+ 1 - 1
packages/remark-growi-plugin/src/index.js

@@ -1,3 +1,3 @@
-import { remarkGrowiPlugin } from './remark-growi-plugin';
+import { remarkGrowiPlugin } from './remark-growi-plugin.js';
 
 export default remarkGrowiPlugin;

+ 26 - 15
packages/remark-growi-plugin/src/remark-growi-plugin.js

@@ -1,24 +1,35 @@
-import { Plugin } from 'unified';
+/**
+ * @typedef {import('mdast').Root} Root
+ *
+ * @typedef {import('mdast-util-directive')} DoNotTouchAsThisImportIncludesDirectivesInTree
+ */
 
 import { directiveFromMarkdown, directiveToMarkdown } from './mdast-util-growi-plugin/index.js';
 import { directive } from './micromark-extension-growi-plugin/dev/index.js';
 
-export const remarkGrowiPlugin: Plugin = function() {
+/**
+    * Plugin to support GROWI plugin (`$lsx(/path, depth=2)`).
+    *
+    * @type {import('unified').Plugin<void[], Root>}
+    */
+export function remarkGrowiPlugin() {
   const data = this.data();
 
-  function add(field: string, value) {
-    if (data[field] != null) {
-      const array = data[field];
-      if (Array.isArray(array)) {
-        array.push(value);
-      }
-    }
-    else {
-      data[field] = [value];
-    }
-  }
-
   add('micromarkExtensions', directive());
   add('fromMarkdownExtensions', directiveFromMarkdown);
   add('toMarkdownExtensions', directiveToMarkdown);
-};
+
+  /**
+      * @param {string} field
+      * @param {unknown} value
+      */
+  function add(field, value) {
+    const list = /** @type {unknown[]} */ (
+      // Other extensions
+      /* c8 ignore next 2 */
+      data[field] ? data[field] : (data[field] = [])
+    );
+
+    list.push(value);
+  }
+}

+ 43 - 39
packages/remark-growi-plugin/test/remark-growi-plugin.test.js

@@ -2,66 +2,70 @@
  * @typedef {import('mdast').Root} Root
  */
 
-import fs from 'node:fs'
-import path from 'node:path'
-import test from 'tape'
-import {readSync} from 'to-vfile'
-import {unified} from 'unified'
-import {remark} from 'remark'
-import {isHidden} from 'is-hidden'
-import directive from '../src/index.js'
+import fs from 'node:fs';
+import path from 'node:path';
+
+import { isHidden } from 'is-hidden';
+import { remark } from 'remark';
+import test from 'tape';
+import { readSync } from 'to-vfile';
+import { unified } from 'unified';
+
+import { remarkGrowiPlugin } from '../src/remark-growi-plugin.js';
 
 test('directive()', (t) => {
   t.doesNotThrow(() => {
-    remark().use(directive).freeze()
-  }, 'should not throw if not passed options')
+    remark().use(remarkGrowiPlugin).freeze();
+  }, 'should not throw if not passed options');
 
   t.doesNotThrow(() => {
-    unified().use(directive).freeze()
-  }, 'should not throw if without parser or compiler')
+    unified().use(remarkGrowiPlugin).freeze();
+  }, 'should not throw if without parser or compiler');
 
-  t.end()
-})
+  t.end();
+});
 
 test('fixtures', (t) => {
-  const base = path.join('test', 'fixtures')
-  const entries = fs.readdirSync(base).filter((d) => !isHidden(d))
+  const base = path.join('test', 'fixtures');
+  const entries = fs.readdirSync(base).filter(d => !isHidden(d));
 
-  t.plan(entries.length)
+  t.plan(entries.length);
 
-  let index = -1
+  let index = -1;
   while (++index < entries.length) {
-    const fixture = entries[index]
+    const fixture = entries[index];
     t.test(fixture, (st) => {
-      const file = readSync(path.join(base, fixture, 'input.md'))
-      const input = String(file)
-      const outputPath = path.join(base, fixture, 'output.md')
-      const treePath = path.join(base, fixture, 'tree.json')
-      const proc = remark().use(directive).freeze()
-      const actual = proc.parse(file)
+      const file = readSync(path.join(base, fixture, 'input.md'));
+      const input = String(file);
+      const outputPath = path.join(base, fixture, 'output.md');
+      const treePath = path.join(base, fixture, 'tree.json');
+      const proc = remark().use(remarkGrowiPlugin).freeze();
+      const actual = proc.parse(file);
       /** @type {string} */
-      let output
+      let output;
       /** @type {Root} */
-      let expected
+      let expected;
 
       try {
-        expected = JSON.parse(String(fs.readFileSync(treePath)))
-      } catch {
+        expected = JSON.parse(String(fs.readFileSync(treePath)));
+      }
+      catch {
         // New fixture.
-        fs.writeFileSync(treePath, JSON.stringify(actual, null, 2) + '\n')
-        expected = actual
+        fs.writeFileSync(treePath, `${JSON.stringify(actual, null, 2)}\n`);
+        expected = actual;
       }
 
       try {
-        output = fs.readFileSync(outputPath, 'utf8')
-      } catch {
-        output = input
+        output = fs.readFileSync(outputPath, 'utf8');
+      }
+      catch {
+        output = input;
       }
 
-      st.deepEqual(actual, expected, 'tree')
-      st.equal(String(proc.processSync(file)), output, 'process')
+      st.deepEqual(actual, expected, 'tree');
+      st.equal(String(proc.processSync(file)), output, 'process');
 
-      st.end()
-    })
+      st.end();
+    });
   }
-})
+});