|
| 1 | +const fs = require("fs"); |
| 2 | +const componentInfoList = require("../docs/components.js"); |
| 3 | + |
| 4 | +const matchComponentNames = [ |
| 5 | + "button", |
| 6 | + "calendar", |
| 7 | + // "origincalendar", |
| 8 | + "chart", |
| 9 | + "checkbox", |
| 10 | + "container", |
| 11 | + // "async_modal", |
| 12 | + // "modal", |
| 13 | + "input", |
| 14 | + "pagination", |
| 15 | + "processor", |
| 16 | + "radio", |
| 17 | + "select", |
| 18 | + "tab", |
| 19 | + "table", |
| 20 | + "tag", |
| 21 | + "text", |
| 22 | + "textarea", |
| 23 | + // "tips-tpl", |
| 24 | + // "upload-item", |
| 25 | + "upload" |
| 26 | +]; |
| 27 | +const componentProsDesMap = {}; |
| 28 | + |
| 29 | +let snippetCollection = {}; |
| 30 | + |
| 31 | +let matchNum = /^\d+$/; |
| 32 | +let matchLetter = /^'|"([a-zA-Z]+|[\u4e00-\u9fa5]+)'|"$/; |
| 33 | +let matchFunc = /^\(\) => \[\]$/; |
| 34 | +let matchEmptyStr = /^(''|\"\")$/; |
| 35 | +let matchEmptyArr = /^\[\]$/; |
| 36 | +let matchBool = /^(true|false)$/; |
| 37 | + |
| 38 | +function parseDefaultValue(defaultValue) { |
| 39 | + let result = false; |
| 40 | + let matchFuncArr = [ |
| 41 | + value => matchNum.test(value) && "number", |
| 42 | + value => matchLetter.test(value) && "string", |
| 43 | + value => matchFunc.test(value) && "array", |
| 44 | + value => matchEmptyStr.test(value) && "emptyString", |
| 45 | + value => matchEmptyArr.test(value) && "emptyArray", |
| 46 | + value => matchBool.test(value) && "boolean" |
| 47 | + ]; |
| 48 | + while (matchFuncArr.length !== 0 && !result) { |
| 49 | + let func = matchFuncArr.pop(); |
| 50 | + if (func) { |
| 51 | + result = func(defaultValue) || false; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (!result) return defaultValue; |
| 56 | + |
| 57 | + switch (result) { |
| 58 | + case "number": |
| 59 | + defaultValue = Number.parseInt(defaultValue); |
| 60 | + break; |
| 61 | + case "string": |
| 62 | + defaultValue = defaultValue.replace( |
| 63 | + /\'([a-zA-Z]+|[\u4e00-\u9fa5]+)\'/, |
| 64 | + "$1" |
| 65 | + ); |
| 66 | + break; |
| 67 | + case "array": |
| 68 | + defaultValue = []; |
| 69 | + break; |
| 70 | + case "emptyString": |
| 71 | + defaultValue = ""; |
| 72 | + break; |
| 73 | + case "emptyArray": |
| 74 | + defaultValue = []; |
| 75 | + break; |
| 76 | + case "boolean": |
| 77 | + defaultValue = defaultValue === "true"; |
| 78 | + break; |
| 79 | + } |
| 80 | + return defaultValue; |
| 81 | +} |
| 82 | + |
| 83 | +componentInfoList.forEach(currentComponentInfo => { |
| 84 | + let { displayName, props, events, methods, slots } = currentComponentInfo; |
| 85 | + if (!matchComponentNames.includes(displayName.toLowerCase())) return; |
| 86 | + |
| 87 | + let componentName = displayName.toLowerCase(); |
| 88 | + let prefix = `test-v2-${componentName}-full`; |
| 89 | + let desc = `@cls ${prefix}`; |
| 90 | + let snippetConstructor = { |
| 91 | + [desc]: { |
| 92 | + scope: "javascript", |
| 93 | + prefix, |
| 94 | + description: desc, |
| 95 | + body: [] |
| 96 | + } |
| 97 | + }; |
| 98 | + let snippetConstructorBody = { |
| 99 | + component: componentName, |
| 100 | + id: "$1", |
| 101 | + name: "$1", |
| 102 | + label: "", |
| 103 | + value: undefined, |
| 104 | + attributes: {}, |
| 105 | + validity: {}, |
| 106 | + decoration: [] |
| 107 | + }; |
| 108 | + Object.keys(props).forEach(propsKey => { |
| 109 | + let { description, tags, defaultValue, name } = props[propsKey]; |
| 110 | + |
| 111 | + // ## 检测到 tagsProperty 中包含 'ignore',退出 |
| 112 | + let { property: tagsProperty, ignore: tagsIgnore } = tags; |
| 113 | + if ( |
| 114 | + tagsIgnore && |
| 115 | + tagsIgnore.some(curItem => curItem.title === "ignore") |
| 116 | + ) |
| 117 | + return; |
| 118 | + |
| 119 | + if (!componentProsDesMap[componentName]) |
| 120 | + componentProsDesMap[componentName] = {}; |
| 121 | + componentProsDesMap[componentName][name] = description; |
| 122 | + |
| 123 | + let curDefaultValue = ""; |
| 124 | + if (defaultValue) { |
| 125 | + ({ value: curDefaultValue } = defaultValue); |
| 126 | + } |
| 127 | + |
| 128 | + curDefaultValue = parseDefaultValue(curDefaultValue); |
| 129 | + if (tagsProperty) { |
| 130 | + let wrapperNameFindResult = tagsProperty.find( |
| 131 | + curItem => curItem.name |
| 132 | + ); |
| 133 | + if (wrapperNameFindResult) { |
| 134 | + let { name: wrapperName } = wrapperNameFindResult; |
| 135 | + |
| 136 | + if (name === "wraperClass") name = "class"; |
| 137 | + snippetConstructorBody[wrapperName][name] = curDefaultValue; |
| 138 | + } |
| 139 | + } else { |
| 140 | + curDefaultValue = parseDefaultValue(curDefaultValue); |
| 141 | + |
| 142 | + // ### 配置默认填写区 |
| 143 | + if (name === "id" || name === "name") curDefaultValue = "$1"; |
| 144 | + |
| 145 | + snippetConstructorBody[name] = curDefaultValue; |
| 146 | + } |
| 147 | + }); |
| 148 | + |
| 149 | + let snippetBodyArr = JSON.stringify( |
| 150 | + snippetConstructorBody, |
| 151 | + undefined, |
| 152 | + 2 |
| 153 | + ).split("\n"); |
| 154 | + let snippetBodyArrLength = snippetBodyArr.length; |
| 155 | + for (let i = 0; i < snippetBodyArrLength; i++) { |
| 156 | + let str = snippetBodyArr[i]; |
| 157 | + let propsNames = Object.keys(componentProsDesMap[componentName]); |
| 158 | + |
| 159 | + let selectProsName = propsNames.find(curName => { |
| 160 | + let testReg = new RegExp(`${curName}(?![a-zA-Z])`); |
| 161 | + return testReg.test(str); |
| 162 | + }); |
| 163 | + if (selectProsName) { |
| 164 | + let desc = componentProsDesMap[componentName][selectProsName]; |
| 165 | + desc = desc.replace(/\n/g, "; "); |
| 166 | + snippetBodyArr[i] = `${str} // ${desc}`; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + snippetConstructor[desc].body = snippetBodyArr; |
| 171 | + Object.assign(snippetCollection, snippetConstructor); |
| 172 | +}); |
| 173 | + |
| 174 | +fs.writeFileSync( |
| 175 | + "./.vscode/snippets.code-snippets", |
| 176 | + JSON.stringify(snippetCollection, undefined, 4) |
| 177 | +); |
0 commit comments