Skip to content

Commit

Permalink
feat: is directive
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Nov 5, 2020
1 parent 50c7387 commit f8c2b81
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 44 deletions.
17 changes: 7 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@
"handlebars": "^4.7.6",
"jest": "^26.2.2",
"mustache": "^4.0.1",
"san": "^3.9.2",
"san-html-cases": "^2.1.0",
"san": "^3.9.5",
"san-html-cases": "^3.10.1",
"san-ssr-target-fake-cmd": "^1.0.0",
"san-ssr-target-fake-esm": "^1.0.0",
"semantic-release": "^17.1.1",
Expand All @@ -99,7 +99,7 @@
"yargs": "^15.4.1"
},
"peerDependencies": {
"san": "^3.9.2"
"san": "^3.9.5"
},
"release": {
"branch": "master",
Expand Down
14 changes: 7 additions & 7 deletions src/models/component-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type TrimWhitespace = 'none' | 'blank' | 'all' | undefined
export interface ComponentInfo {
id: string,
root: ANode,
childComponents: Map<TagName | ANode, ComponentReference>
childComponents: Map<TagName, ComponentReference>
hasMethod (name: string): boolean
initData?(): any,
getComputedNames (): string[]
getFilterNames (): string[]
getChildComponentRenference (aNode: ANode): ComponentReference | undefined
getChildComponentRenference (tagName: string): ComponentReference | undefined
}

/**
Expand All @@ -35,15 +35,15 @@ abstract class ComponentInfoImpl<R extends ComponentReference = ComponentReferen
*/
public readonly id: string,
public readonly root: ANode,
public readonly childComponents: Map<TagName | ANode, R>
public readonly childComponents: Map<TagName, R>
) {}

abstract hasMethod (name: string): boolean
abstract getComputedNames (): string[]
abstract getFilterNames (): string[]

getChildComponentRenference (aNode: ANode): R | undefined {
return this.childComponents.get(aNode) || this.childComponents.get(aNode.tagName)
getChildComponentRenference (tagName: string): R | undefined {
return this.childComponents.get(tagName)
}
}

Expand All @@ -58,7 +58,7 @@ export class DynamicComponentInfo extends ComponentInfoImpl<DynamicComponentRefe
constructor (
id: string,
root: ANode,
childComponents: Map<TagName | ANode, DynamicComponentReference>,
childComponents: Map<TagName, DynamicComponentReference>,
public readonly componentClass: ComponentClass
) {
super(id, root, childComponents)
Expand Down Expand Up @@ -136,7 +136,7 @@ export class TypedComponentInfo extends ComponentInfoImpl implements ComponentIn
constructor (
id: string,
root: ANode,
childComponents: Map<TagName | ANode, ComponentReference>,
childComponents: Map<TagName, ComponentReference>,
public readonly classDeclaration: ClassDeclaration
) {
super(id, root, childComponents)
Expand Down
23 changes: 3 additions & 20 deletions src/parsers/component-class-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { DynamicSanSourceFile } from '../models/san-source-file'
import { DynamicComponentInfo } from '../models/component-info'
import { getMember } from '../utils/lang'
import { isComponentLoader, ComponentClass } from '../models/component'
import { visitANodeRecursively } from '../utils/anode-util'
import { parseAndNormalizeTemplate } from './parse-template'
import { componentID, DynamicComponentReference } from '../models/component-reference'

Expand Down Expand Up @@ -52,18 +51,16 @@ export class ComponentClassParser {
const trimWhitespace = getMember<'none' | 'blank' | 'all'>(componentClass, 'trimWhitespace')
const delimiters = getMember<[string, string]>(componentClass, 'delimiters')
const rootANode = parseAndNormalizeTemplate(template, { trimWhitespace, delimiters })
const childComponents = this.getChildComponentClasses(componentClass, rootANode)
const childComponents = this.getChildComponentClasses(componentClass)

return new DynamicComponentInfo(id, rootANode, childComponents, componentClass)
}

/**
* 从组件 class 得到子组件 class
* - 从 .components 属性获取
* - 从 .getComponentType() 方法获取
*/
getChildComponentClasses (parentComponentClass: ComponentClass, rootANode: ANode): Map<string | ANode, DynamicComponentReference> {
const children: Map<string | ANode, DynamicComponentReference> = new Map()
getChildComponentClasses (parentComponentClass: ComponentClass): Map<string, DynamicComponentReference> {
const children: Map<string, DynamicComponentReference> = new Map()

const components: { [key: string]: ComponentConstructor<{}, {}> } = getMember(parentComponentClass, 'components', {})
for (const [tagName, componentClass] of Object.entries(components)) {
Expand All @@ -74,20 +71,6 @@ export class ComponentClassParser {
componentClass
})
}

const getComponentType = parentComponentClass.prototype.getComponentType
if (typeof getComponentType !== 'function') return children

visitANodeRecursively(rootANode, (aNode: ANode) => {
const childClazz: ComponentClass = getComponentType(aNode)
if (!childClazz) return

children.set(aNode, {
specifier: '.',
id: componentID(childClazz === this.root, () => this.getOrSetID(childClazz)),
componentClass: childClazz
})
})
return children
}

Expand Down
17 changes: 14 additions & 3 deletions src/parsers/san-file-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ export class SanFileParser {
return this.parser.parse()
}

expandToSanComponent (options: ObjectExpression) {
const opts = { ...options }
/**
* 把简写的 san options 替换为 san Component。
* { inited(){} } -> require('san').defineComponent({ inited(){} })
*/
private expandToSanComponent (options: ObjectExpression) {
const defineComponent: CallExpression = {
type: 'CallExpression',
callee: {
Expand All @@ -48,12 +51,17 @@ export class SanFileParser {
computed: false,
optional: false
},
arguments: [opts],
arguments: [{ ...options }],
optional: false
}
Object.assign(options, defineComponent)
}

/**
* 把模板字符串插入到 san 组件定义中
* - 情况一:defineComponent({ inited(){} }) -> defineComponent({ inited(){}, template: '<div>...</div>' })
* - 情况二:class XComponent { constructor() {} } -> class XComponent { constructor() { this.template='<div>...</div>' } }
*/
private insertTemplate (expr: Node) {
if (isCallExpression(expr)) {
assert(expr.arguments[0], 'cannot parse san script')
Expand Down Expand Up @@ -83,6 +91,9 @@ export class SanFileParser {
}
}

/**
* 创建给 this.template 赋值为 this.templateContent 的表达式
*/
private createTemplateAssignmentExpression (): ExpressionStatement {
return {
type: 'ExpressionStatement',
Expand Down
2 changes: 1 addition & 1 deletion src/target-js/compilers/anode-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ANodeCompiler<T extends 'none' | 'typed'> {
if (TypeGuards.isATemplateNode(aNode)) return this.compileTemplate(aNode)
if (TypeGuards.isAFragmentNode(aNode)) return this.compileFragment(aNode)

const ref = this.componentInfo.getChildComponentRenference(aNode)
const ref = this.componentInfo.getChildComponentRenference(aNode.tagName)
if (ref) {
return this.compileComponent(aNode, ref, isRootElement)
}
Expand Down

0 comments on commit f8c2b81

Please sign in to comment.