@datawrapper/svelte-component-info 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Samuele de Tomasi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Svelte - Get Component Info
2
+
3
+ _A function to extract information about the props, actions, slots and css variables from a Svelte file. Designed to simplify the creation of documentation_
4
+
5
+ NPM link: [@el3um4s/svelte-get-component-info](https://www.npmjs.com/package/@el3um4s/svelte-get-component-info)
6
+
7
+ I recommend using it with [el3um4s/svelte-component-info](https://github.com/el3um4s/svelte-component-info) (npm: [@el3um4s/svelte-component-info](https://www.npmjs.com/package/@el3um4s/svelte-component-info))
8
+
9
+ ### Install and use the package
10
+
11
+ To use the package in a project:
12
+
13
+ ```bash
14
+ npm i @el3um4s/svelte-get-component-info
15
+ ```
16
+
17
+ and then in a file:
18
+
19
+ ```ts
20
+ import type { SvelteInformations } from "@el3um4s/svelte-get-component-info";
21
+ import { getInfo, readFileSvelte } from "@el3um4s/svelte-get-component-info";
22
+
23
+ const info: SvelteInformations = getInfo(readFileSvelte("./src/lib/hello.svelte").content.content);
24
+ console.log(info.props); // [{ name: "message", type: "string", defaultValue: "Hello World" }]
25
+ console.log(info.actions); /// [ { name: "notify" }]
26
+ ```
27
+
28
+ `info` looks like this:
29
+
30
+ ```json
31
+ {
32
+ "props": [
33
+ { "name": "color", "type":"string", "defaultValue":"red" },
34
+ { "name": "steps", "type":"number", "defaultValue":"8" }
35
+ { "name": "title", "type":"string" },
36
+ { "name": "description"}
37
+ ],
38
+ "actions": [
39
+ { "name": "click" },
40
+ { "name": "hover" },
41
+ { "name": "customAction" }
42
+ ],
43
+ "slots": [
44
+ { "anonymous": true },
45
+ { "name": "header", "anonymous": false },
46
+ { "name": "footer", "anonymous": false }
47
+ ],
48
+ "css": [
49
+ { "name": "--color-primary" },
50
+ { "name": "--color-secondary" }
51
+ ]
52
+ }
53
+ ```
54
+
55
+ ### To Do List:
56
+
57
+ - [x] get props:
58
+ - [x] name
59
+ - [x] type
60
+ - [x] defaultValue
61
+ - [x] actions
62
+ - [x] slots
63
+ - [x] anonyms
64
+ - [x] named
65
+ - [x] css variables
@@ -0,0 +1,3 @@
1
+ import { SvelteInformations } from "./interfaces";
2
+ declare function getInfo(source: string): SvelteInformations;
3
+ export { getInfo };
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getInfo = void 0;
4
+ const parseFileSvelte_1 = require("./parseFileSvelte");
5
+ const parseProps_1 = require("./parseProps");
6
+ const parseActions_1 = require("./parseActions");
7
+ const parseSlots_1 = require("./parseSlots");
8
+ const parseCSS_1 = require("./parseCSS");
9
+ function getInfo(source) {
10
+ const propsAsInFile = (0, parseFileSvelte_1.getProps_asInFile)(source);
11
+ const props = [];
12
+ propsAsInFile.forEach(p => props.push((0, parseProps_1.getPropInfo)(p)));
13
+ const actionsAsInFile = (0, parseFileSvelte_1.getActions_asInFile)(source);
14
+ const actionsWithDuplicates = [];
15
+ actionsAsInFile.forEach(a => actionsWithDuplicates.push((0, parseActions_1.getActionInfo)(a)));
16
+ const act = new Set();
17
+ const actions = actionsWithDuplicates.filter(el => {
18
+ const duplicate = act.has(el.name);
19
+ act.add(el.name);
20
+ return !duplicate;
21
+ });
22
+ const slotsInFile = (0, parseFileSvelte_1.getSlots_asInFile)(source);
23
+ const slotsWithDuplicates = [];
24
+ slotsInFile.forEach(s => slotsWithDuplicates.push((0, parseSlots_1.getSlotInfo)(s)));
25
+ const slot = new Set();
26
+ const slots = slotsWithDuplicates.filter(el => {
27
+ const duplicate = slot.has(el.name);
28
+ slot.add(el.name);
29
+ return !duplicate;
30
+ });
31
+ const cssInFile = (0, parseFileSvelte_1.getCSS_asInFile)(source);
32
+ const cssWithDuplicates = [];
33
+ cssInFile.forEach(s => cssWithDuplicates.push((0, parseCSS_1.getCSSInfo)(s)));
34
+ const c = new Set();
35
+ const css = cssWithDuplicates.filter(el => {
36
+ const duplicate = c.has(el.name);
37
+ c.add(el.name);
38
+ return !duplicate;
39
+ });
40
+ return { props, actions, slots, css };
41
+ }
42
+ exports.getInfo = getInfo;
@@ -0,0 +1,29 @@
1
+ export interface Content {
2
+ error: Reading;
3
+ content: Reading;
4
+ }
5
+ export interface Reading {
6
+ status: boolean;
7
+ content: string;
8
+ }
9
+ export interface Prop {
10
+ name: string;
11
+ type?: string;
12
+ defaultValue?: string;
13
+ }
14
+ export interface Action {
15
+ name: string;
16
+ }
17
+ export interface Slot {
18
+ anonymous: boolean;
19
+ name?: string;
20
+ }
21
+ export interface CSS {
22
+ name: string;
23
+ }
24
+ export interface SvelteInformations {
25
+ props: Array<Prop>;
26
+ actions: Array<Action>;
27
+ slots: Array<Slot>;
28
+ css: Array<CSS>;
29
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ import type { Action } from "./interfaces";
2
+ declare function getActionInfo(s: string): Action;
3
+ declare function getActionName(s: string): string;
4
+ export { getActionInfo, getActionName };
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getActionName = exports.getActionInfo = void 0;
4
+ function getActionInfo(s) {
5
+ const name = getActionName(s);
6
+ return { name };
7
+ }
8
+ exports.getActionInfo = getActionInfo;
9
+ function getActionName(s) {
10
+ const action = s.trim().substring(1).trim();
11
+ const name = action.substring(0, action.length - 1);
12
+ return name;
13
+ }
14
+ exports.getActionName = getActionName;
@@ -0,0 +1,4 @@
1
+ import type { CSS } from "./interfaces";
2
+ declare function getCSSInfo(s: string): CSS;
3
+ declare function getCSSName(s: string): string;
4
+ export { getCSSInfo, getCSSName };
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCSSName = exports.getCSSInfo = void 0;
4
+ function getCSSInfo(s) {
5
+ const name = getCSSName(s);
6
+ return { name };
7
+ }
8
+ exports.getCSSInfo = getCSSInfo;
9
+ function getCSSName(s) {
10
+ return s.trim();
11
+ }
12
+ exports.getCSSName = getCSSName;
@@ -0,0 +1,9 @@
1
+ declare function hasProps(component: string): boolean;
2
+ declare function getProps_asInFile(component: string): Array<string>;
3
+ declare function hasActions(component: string): boolean;
4
+ declare function getActions_asInFile(component: string): Array<string>;
5
+ declare function hasSlots(component: string): boolean;
6
+ declare function getSlots_asInFile(component: string): Array<string>;
7
+ declare function hasCSS(component: string): boolean;
8
+ declare function getCSS_asInFile(component: string): Array<string>;
9
+ export { hasProps, getProps_asInFile, hasActions, getActions_asInFile, hasSlots, getSlots_asInFile, hasCSS, getCSS_asInFile };
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getCSS_asInFile = exports.hasCSS = exports.getSlots_asInFile = exports.hasSlots = exports.getActions_asInFile = exports.hasActions = exports.getProps_asInFile = exports.hasProps = void 0;
4
+ const regex = {
5
+ propsGeneric: /export let [\s\S]*?;/gi,
6
+ actionsGeneric: /(?<=dispatch[.(])(.*?)(?=[,)])/gi,
7
+ slotsGeneric: /<slot[\s\S]*?>/gi,
8
+ cssGeneric: /(?=--)(.*?)(?=[,=)>:;\n\r\t\0])/gi
9
+ };
10
+ function hasProps(component) {
11
+ const content = component.match(regex.propsGeneric);
12
+ return content != null;
13
+ }
14
+ exports.hasProps = hasProps;
15
+ function getProps_asInFile(component) {
16
+ const content = component.match(regex.propsGeneric);
17
+ return content != null ? content : [];
18
+ }
19
+ exports.getProps_asInFile = getProps_asInFile;
20
+ function hasActions(component) {
21
+ const content = component.match(regex.actionsGeneric);
22
+ return content != null;
23
+ }
24
+ exports.hasActions = hasActions;
25
+ function getActions_asInFile(component) {
26
+ const content = component.match(regex.actionsGeneric);
27
+ return content != null ? content : [];
28
+ }
29
+ exports.getActions_asInFile = getActions_asInFile;
30
+ function hasSlots(component) {
31
+ const content = component.match(regex.slotsGeneric);
32
+ return content != null;
33
+ }
34
+ exports.hasSlots = hasSlots;
35
+ function getSlots_asInFile(component) {
36
+ const content = component.match(regex.slotsGeneric);
37
+ return content != null ? content : [];
38
+ }
39
+ exports.getSlots_asInFile = getSlots_asInFile;
40
+ function hasCSS(component) {
41
+ const content = component.match(regex.cssGeneric);
42
+ return content != null;
43
+ }
44
+ exports.hasCSS = hasCSS;
45
+ function getCSS_asInFile(component) {
46
+ const content = component.match(regex.cssGeneric);
47
+ return content != null ? content : [];
48
+ }
49
+ exports.getCSS_asInFile = getCSS_asInFile;
@@ -0,0 +1,6 @@
1
+ import type { Prop } from "./interfaces";
2
+ declare function getPropInfo(s: string): Prop;
3
+ declare function getPropName(s: string): string;
4
+ declare function getPropType(s: string): string | undefined;
5
+ declare function getPropDefaultValue(s: string): string | undefined;
6
+ export { getPropInfo, getPropName, getPropType, getPropDefaultValue };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getPropDefaultValue = exports.getPropType = exports.getPropName = exports.getPropInfo = void 0;
4
+ const utils_1 = require("./utils");
5
+ const regex = {
6
+ name: /(?<=let )(.*?)(?=\s|;|=|:)/,
7
+ type: /(?<=let [:]|[:])(.*?)(?=;|=)/
8
+ };
9
+ function getPropInfo(s) {
10
+ const name = getPropName(s);
11
+ const type = getPropType(s);
12
+ const defaultValue = getPropDefaultValue(s);
13
+ return { name, type, defaultValue };
14
+ }
15
+ exports.getPropInfo = getPropInfo;
16
+ function getPropName(s) {
17
+ const nameRegex = s.match(regex.name);
18
+ const nameWithDelimiters = nameRegex ? nameRegex[0] : "";
19
+ const name = nameWithDelimiters.replace("let", "").replace(/[:|;|=]/gi, "").trim();
20
+ return name;
21
+ }
22
+ exports.getPropName = getPropName;
23
+ function getPropType(s) {
24
+ const typeRegex = s.match(regex.type);
25
+ const positionEquals = s.indexOf("=");
26
+ const positionSemicolon = s.indexOf(";");
27
+ const firstDelimiters = positionEquals > -1 ? positionEquals : positionSemicolon;
28
+ const positionRegex = (typeRegex === null || typeRegex === void 0 ? void 0 : typeRegex.index) ? typeRegex.index : -1;
29
+ const type = positionRegex < firstDelimiters && typeRegex ? typeRegex[0].trim() : undefined;
30
+ return type;
31
+ }
32
+ exports.getPropType = getPropType;
33
+ function getPropDefaultValue(s) {
34
+ const positionEquals = s.indexOf("=");
35
+ const positionSemicolon = s.lastIndexOf(";");
36
+ const defaultValue = positionEquals < 0 ? undefined : s.substring(positionEquals + 1, positionSemicolon).trim();
37
+ const result = defaultValue && (0, utils_1.isStringType)(defaultValue) ? (0, utils_1.getStringWithoutQuote)(defaultValue) : defaultValue;
38
+ return result;
39
+ }
40
+ exports.getPropDefaultValue = getPropDefaultValue;
@@ -0,0 +1,5 @@
1
+ import type { Slot } from "./interfaces";
2
+ declare function getSlotInfo(s: string): Slot;
3
+ declare function getSlotName(s: string): string | undefined;
4
+ declare function isSlotAnonymous(s: string): boolean;
5
+ export { getSlotName, isSlotAnonymous, getSlotInfo };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getSlotInfo = exports.isSlotAnonymous = exports.getSlotName = void 0;
4
+ const regex = {
5
+ slotName: /(?<=name\s*=)(.*)("|'|`)/gi
6
+ };
7
+ function getSlotInfo(s) {
8
+ const anonymous = isSlotAnonymous(s);
9
+ const name = getSlotName(s);
10
+ return { name, anonymous };
11
+ }
12
+ exports.getSlotInfo = getSlotInfo;
13
+ function getSlotName(s) {
14
+ var _a;
15
+ const slot = (_a = s.match(regex.slotName)) === null || _a === void 0 ? void 0 : _a[0].trim();
16
+ const name = slot == null ? undefined : slot.substring(1, slot.length - 1);
17
+ return name;
18
+ }
19
+ exports.getSlotName = getSlotName;
20
+ function isSlotAnonymous(s) {
21
+ const slot = s.match(regex.slotName);
22
+ return slot == null;
23
+ }
24
+ exports.isSlotAnonymous = isSlotAnonymous;
@@ -0,0 +1,4 @@
1
+ import { Content } from "./interfaces";
2
+ declare function readFileSvelte(nameFile: string): Content;
3
+ declare function checkFileExist(nameFile: string): boolean;
4
+ export { readFileSvelte, checkFileExist };
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.checkFileExist = exports.readFileSvelte = void 0;
4
+ const fs_1 = require("fs");
5
+ const to_try_1 = require("@el3um4s/to-try");
6
+ function readFileSvelte(nameFile) {
7
+ const content = {
8
+ error: {
9
+ status: true,
10
+ content: "file not read"
11
+ },
12
+ content: {
13
+ status: false,
14
+ content: ""
15
+ }
16
+ };
17
+ if (checkFileExist(nameFile)) {
18
+ const [result, error] = (0, to_try_1.toTry)(() => (0, fs_1.readFileSync)(nameFile));
19
+ if (!error && result) {
20
+ const contentString = result.toString();
21
+ content.error = {
22
+ status: false,
23
+ content: ""
24
+ };
25
+ content.content = {
26
+ status: true,
27
+ content: contentString
28
+ };
29
+ }
30
+ }
31
+ else {
32
+ content.error.content = `File "${nameFile}" not exist`;
33
+ }
34
+ return content;
35
+ }
36
+ exports.readFileSvelte = readFileSvelte;
37
+ function checkFileExist(nameFile) {
38
+ return (0, fs_1.existsSync)(nameFile);
39
+ }
40
+ exports.checkFileExist = checkFileExist;
@@ -0,0 +1,3 @@
1
+ declare function isStringType(s: string): boolean;
2
+ declare function getStringWithoutQuote(s: string): string;
3
+ export { isStringType, getStringWithoutQuote };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getStringWithoutQuote = exports.isStringType = void 0;
4
+ function isStringType(s) {
5
+ const startString = s.trim();
6
+ const firstChar = startString.charAt(0);
7
+ const lastChar = startString.charAt(startString.length - 1);
8
+ const charIsQuote = firstChar === `"` || firstChar === `'` || firstChar === "`";
9
+ const result = charIsQuote && firstChar === lastChar;
10
+ return result;
11
+ }
12
+ exports.isStringType = isStringType;
13
+ function getStringWithoutQuote(s) {
14
+ const startString = s.trim();
15
+ const result = startString.substring(1, startString.length - 1);
16
+ return result;
17
+ }
18
+ exports.getStringWithoutQuote = getStringWithoutQuote;
package/lib/index.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { getInfo } from "./functions/getInfo";
2
+ import { readFileSvelte } from "./functions/readFileSvelte";
3
+ import type { SvelteInformations } from "./functions/interfaces";
4
+ export { getInfo, readFileSvelte, SvelteInformations };
package/lib/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readFileSvelte = exports.getInfo = void 0;
4
+ const getInfo_1 = require("./functions/getInfo");
5
+ Object.defineProperty(exports, "getInfo", { enumerable: true, get: function () { return getInfo_1.getInfo; } });
6
+ const readFileSvelte_1 = require("./functions/readFileSvelte");
7
+ Object.defineProperty(exports, "readFileSvelte", { enumerable: true, get: function () { return readFileSvelte_1.readFileSvelte; } });
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@datawrapper/svelte-component-info",
3
+ "version": "0.1.0",
4
+ "description": "A library to read the component infos from Svelte source code",
5
+ "main": "lib/index.js",
6
+ "types": "lib/index.d.ts",
7
+ "scripts": {
8
+ "build": "tsc",
9
+ "lint": "eslint . --ext .ts",
10
+ "test": "jest --config jestconfig.json",
11
+ "test:no-coverage": "jest --config jestconfig.json --coverage=false",
12
+ "prepare": "npm run build",
13
+ "prepublishOnly": "npm test && npm run lint",
14
+ "preversion": "npm run lint",
15
+ "version": "git add -A src",
16
+ "postversion": "git push && git push --tags",
17
+ "check-updates": "npx npm-check-updates",
18
+ "check-updates:minor": "npx npm-check-updates --target minor",
19
+ "check-updates:patch": "npx npm-check-updates --target patch",
20
+ "version:patch": "npm version patch"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "git+https://github.com/datawrapper/svelte-component-info.git"
25
+ },
26
+ "files": [
27
+ "lib/**/*"
28
+ ],
29
+ "keywords": [
30
+ "typescript",
31
+ "npm",
32
+ "ts",
33
+ "svelte",
34
+ "documentation",
35
+ "autogenerator",
36
+ "sveltejs",
37
+ "sveltekit"
38
+ ],
39
+ "author": "Datawrapper GmbH (Original author: Samuele C. De Tomasi)",
40
+ "license": "MIT",
41
+ "bugs": {
42
+ "url": "https://github.com/el3um4s/svelte-get-component-info/issues"
43
+ },
44
+ "homepage": "https://github.com/el3um4s/svelte-get-component-info#readme",
45
+ "devDependencies": {
46
+ "@types/jest": "^27.0.3",
47
+ "@types/node": "^17.0.5",
48
+ "@typescript-eslint/eslint-plugin": "^5.8.1",
49
+ "@typescript-eslint/parser": "^5.8.1",
50
+ "eslint": "^8.5.0",
51
+ "eslint-config-prettier": "^8.3.0",
52
+ "eslint-config-standard": "^16.0.3",
53
+ "eslint-plugin-import": "^2.25.3",
54
+ "eslint-plugin-node": "^11.1.0",
55
+ "eslint-plugin-promise": "^6.0.0",
56
+ "jest": "^27.4.5",
57
+ "ts-jest": "^27.1.2",
58
+ "typescript": "^4.5.4"
59
+ },
60
+ "dependencies": {
61
+ "@el3um4s/to-try": "^0.0.5"
62
+ }
63
+ }