-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jinliang.wjl
committed
Sep 3, 2018
0 parents
commit af7d361
Showing
10 changed files
with
507 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
# Apply for all files | ||
[*] | ||
|
||
charset = utf-8 | ||
|
||
indent_style = space | ||
indent_size = 2 | ||
|
||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
|
||
# package.json | ||
[package.json] | ||
indent_size = 2 | ||
indent_style = space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"env": { | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": "eslint:recommended", | ||
"rules": { | ||
"indent": [ | ||
"error", | ||
2 | ||
], | ||
"linebreak-style": [ | ||
"error", | ||
"unix" | ||
], | ||
"quotes": [ | ||
"error", | ||
"single" | ||
], | ||
"semi": [ | ||
"error", | ||
"always" | ||
], | ||
"no-console": 0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules/ | ||
.idea/ | ||
*.log | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT LICENSE | ||
|
||
Copyright (c) 2018-present Alibaba Group Holding Limited, https://www.alibabagroup.com/ | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# @alifd/dts-generator | ||
一个依据[描述 react 组件 api 信息文件](https://github.com/alibaba-fusion/api-extractor)生成 [*.d.ts](https://blogs.msdn.microsoft.com/typescript/2016/12/14/writing-dts-files-for-types/) 文件的工具。 | ||
|
||
[](https://www.npmjs.org/package/@alifd/dts-generator) | ||
|
||
## Install | ||
|
||
``` | ||
npm install @alifd/dts-generator | ||
``` | ||
|
||
## Usage | ||
|
||
``` js | ||
const dtsGen = require('@alifd/dts-generator'); | ||
dtsGen(apiSchema).then(result => { | ||
console.log(result.message); | ||
}); | ||
``` | ||
|
||
or | ||
|
||
``` js | ||
const dtsGen = require('@alifd/dts-generator'); | ||
const result = dtsGen(result, true); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const prettier = require('prettier'); | ||
|
||
const Interface = require('./types/interface'); | ||
const Component = require('./types/component'); | ||
|
||
function run(schema, syncExec) { | ||
let subComponents = schema.subComponents; | ||
schema.isEntry = true; | ||
let entryComponent = _run(schema); | ||
|
||
if (Array.isArray(subComponents)) { | ||
// submodules is nullable | ||
subComponents = subComponents.map(component => _run(component)); | ||
} else { | ||
subComponents = []; | ||
} | ||
|
||
const formatted = prettier.format( | ||
_createHead() + subComponents.join('\n') + entryComponent, | ||
{ | ||
parser: 'typescript' | ||
} | ||
); | ||
|
||
if (syncExec) { | ||
return { | ||
message: formatted | ||
}; | ||
} | ||
|
||
return Promise.resolve({ | ||
message: formatted | ||
}); | ||
} | ||
|
||
function _run(component) { | ||
const name = component.name; | ||
const subComponents = component.subComponents; | ||
const isEntry = component.isEntry; | ||
|
||
const reactProps = new Interface({ | ||
name, | ||
props: component.props || {}, | ||
propsExtends: component.propsExtends | ||
}); | ||
const reactComponent = new Component({ | ||
name, | ||
subComponents, | ||
methods: component.methods, | ||
isEntry | ||
}); | ||
return reactProps.toString() + reactComponent.toString(); | ||
} | ||
|
||
function _createHead() { | ||
return ` | ||
/// <reference types="react" />\n | ||
import * as React from 'react';\n | ||
`; | ||
} | ||
|
||
module.exports = run; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const ARRAY = /ARRAY/i; | ||
const ENUM = /ENUM/i; | ||
const UNION = /UNION/i; | ||
const OBJECT = /OBJECT/i; | ||
const FUNC = /FUNC(TION)?/i; | ||
const STRING = /STRING/i; | ||
const BOOL = /BOOL(EAN)?/i; | ||
const NUMBER = /NUMBER/i; | ||
const ELEMENT = /ELEMENT/i; | ||
const NODE = /NODE/i; | ||
|
||
class Base { | ||
constructor(opts) { | ||
Object.assign(this, opts); | ||
} | ||
/** | ||
* 右值的映射 | ||
* @param {*} key | ||
* @param {*} value | ||
*/ | ||
mapping(key, value) { | ||
const typeName = value.type ? value.type.name : value; | ||
const typeValue = value.type ? value.type.value : ''; | ||
|
||
if (key === 'style') { | ||
return 'React.CSSProperties'; | ||
} | ||
|
||
if (ARRAY.test(typeName)) { | ||
return 'Array<any>'; | ||
} | ||
|
||
if (ELEMENT.test(typeName)) { | ||
return 'React.ReactElement<any>'; | ||
} | ||
|
||
if (NODE.test(typeName)) { | ||
return 'React.ReactNode'; | ||
} | ||
|
||
if (ENUM.test(typeName)) { | ||
if (typeValue) { | ||
return typeValue.map(item => item.value).join(' | '); | ||
} | ||
} | ||
|
||
if (UNION.test(typeName)) { | ||
if (typeValue) { | ||
return typeValue.map(item => this.mapping(null, item.name || item)).join(' | '); | ||
} | ||
} | ||
if (OBJECT.test(typeName)) { | ||
return new Obj({ | ||
properties: value.properties || [] | ||
}).toString(); | ||
} | ||
|
||
if (FUNC.test(typeName)) { | ||
if (!value.type) { | ||
return '(() => void)'; | ||
} else { | ||
return new Func({ | ||
params: value.params, | ||
returns: value.returns | ||
}); | ||
} | ||
} | ||
|
||
if (STRING.test(typeName)) { | ||
return 'string'; | ||
} | ||
|
||
if (BOOL.test(typeName)) { | ||
return 'boolean'; | ||
} | ||
|
||
if (NUMBER.test(typeName)) { | ||
return 'number'; | ||
} | ||
|
||
return 'any'; | ||
|
||
} | ||
/** | ||
* 一个类转换为 TS 的方法,由子类自己实现 | ||
*/ | ||
toString() { | ||
throw new TypeError('This method should Subclasses implement'); | ||
} | ||
} | ||
|
||
class Func extends Base { | ||
toString() { | ||
return ` | ||
((${(this.params || []).map(param => { | ||
if (param.name.indexOf('.') > -1) { | ||
return ''; | ||
} | ||
return `${param.name}: ${this.mapping(param.name, param)}`; | ||
}).filter(_ => _).join(',')}) => ${this.returns ? this.mapping(this.returns.name, this.returns) : 'void'}) | ||
`; | ||
} | ||
} | ||
|
||
class Obj extends Base { | ||
toString() { | ||
return `{ | ||
${this.properties.map(property => { | ||
return `${property.name}: ${this.mapping(property.name, property)}`; | ||
})} | ||
} | ||
`; | ||
} | ||
} | ||
|
||
module.exports = Base; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const Base = require('./base'); | ||
|
||
module.exports = class Component extends Base { | ||
toString() { | ||
const exportType = this.isEntry ? 'export default' : 'export'; | ||
this.subComponents = this.subComponents || []; | ||
this.methods = this.methods || []; | ||
|
||
return ` | ||
${exportType} class ${this.name} extends React.Component<${this.name}Props, any> { | ||
${this.subComponents.map(item => { | ||
return `static ${item.name}: typeof ${item.name};`; | ||
}).join('\n')} | ||
${this.methods.filter(method => { | ||
return method.modifiers && method.modifiers.indexOf('static') > -1; | ||
}).map(method => { | ||
return `static ${method.name}(${method.params.map(param => { | ||
return `${param.name}: ${this.mapping(param.name, param)}`; | ||
}).join(', ')}): ${method.returns ? this.mapping(method.returns.name, method.returns) : 'void'};`; | ||
}).join('\n')} | ||
} | ||
`; | ||
} | ||
}; |
Oops, something went wrong.