-
Notifications
You must be signed in to change notification settings - Fork 22
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
Showing
7 changed files
with
223 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,107 @@ | ||
import { ExpressionStatement, MethodDefinition, ObjectExpression, CallExpression, Node } from 'estree' | ||
import { JavaScriptSanParser } from './javascript-san-parser' | ||
import assert from 'assert' | ||
import { isClass, getConstructor, addStringPropertyForObject, assertObjectExpression, isCallExpression, isObjectExpression, findDefaultExport } from '../utils/js-ast-util' | ||
import { generate } from 'astring' | ||
|
||
export class SanFileParser { | ||
fileContent: string | ||
|
||
private readonly parser: JavaScriptSanParser | ||
|
||
constructor ( | ||
public readonly scriptContent: string, | ||
public readonly templateContent: string, | ||
private readonly filePath: string | ||
) { | ||
this.parser = new JavaScriptSanParser(filePath, scriptContent, 'module') | ||
this.fileContent = 'not parsed yet' | ||
} | ||
|
||
parse () { | ||
const expr = findDefaultExport(this.parser.root) | ||
assert(expr, 'default export not found') | ||
|
||
// export default { inited() {} } | ||
if (isObjectExpression(expr)) this.expandToSanComponent(expr) | ||
|
||
// defineComponent({}) -> defineComponent({ template: `${templateContent}` }) | ||
this.insertTemplate(expr) | ||
|
||
this.fileContent = generate(this.parser.root) | ||
return this.parser.parse() | ||
} | ||
|
||
expandToSanComponent (options: ObjectExpression) { | ||
const opts = { ...options } | ||
const defineComponent: CallExpression = { | ||
type: 'CallExpression', | ||
callee: { | ||
type: 'MemberExpression', | ||
object: { | ||
type: 'CallExpression', | ||
callee: { type: 'Identifier', name: 'require' }, | ||
arguments: [{ type: 'Literal', value: 'san', raw: "'san'" }], | ||
optional: false | ||
}, | ||
property: { type: 'Identifier', name: 'defineComponent' }, | ||
computed: false, | ||
optional: false | ||
}, | ||
arguments: [opts], | ||
optional: false | ||
} | ||
Object.assign(options, defineComponent) | ||
} | ||
|
||
private insertTemplate (expr: Node) { | ||
if (isCallExpression(expr)) { | ||
assert(expr.arguments[0], 'cannot parse san script') | ||
assertObjectExpression(expr.arguments[0]) | ||
addStringPropertyForObject(expr.arguments[0], 'template', this.templateContent) | ||
} else if (isClass(expr)) { | ||
const fn = getConstructor(expr) || this.createEmptyConstructor() | ||
fn.value.body.body.push(this.createTemplateAssignmentExpression()) | ||
} | ||
} | ||
|
||
private createEmptyConstructor (): MethodDefinition { | ||
return { | ||
type: 'MethodDefinition', | ||
kind: 'constructor', | ||
static: false, | ||
computed: false, | ||
key: { type: 'Identifier', name: 'constructor' }, | ||
value: { | ||
type: 'FunctionExpression', | ||
id: null, | ||
generator: false, | ||
async: false, | ||
params: [], | ||
body: { type: 'BlockStatement', body: [] } | ||
} | ||
} | ||
} | ||
|
||
private createTemplateAssignmentExpression (): ExpressionStatement { | ||
return { | ||
type: 'ExpressionStatement', | ||
expression: { | ||
type: 'AssignmentExpression', | ||
operator: '=', | ||
left: { | ||
type: 'MemberExpression', | ||
object: { type: 'ThisExpression' }, | ||
property: { type: 'Identifier', name: 'template' }, | ||
computed: false, | ||
optional: false | ||
}, | ||
right: { | ||
type: 'Literal', | ||
value: this.templateContent, | ||
raw: JSON.stringify(this.templateContent) | ||
} | ||
} | ||
} | ||
} | ||
} |
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
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
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,43 @@ | ||
import { SanFileParser } from '../../../src/parsers/san-file-parser' | ||
|
||
describe('SanFileParser', () => { | ||
describe('#wireChildComponents()', () => { | ||
it('should parse a single defineComponent', () => { | ||
const script = ` | ||
import { defineComponent } from 'san' | ||
export default defineComponent({ | ||
inited() {} | ||
})` | ||
const template = '<div>Foo</div>' | ||
const parser = new SanFileParser(script, template, '/tmp/foo.san') | ||
const sourceFile = parser.parse() | ||
|
||
expect(sourceFile.getFilePath()).toEqual('/tmp/foo.san') | ||
expect(sourceFile.getFileContent()).toEqual(script) | ||
expect(sourceFile.componentInfos).toHaveLength(1) | ||
expect(sourceFile.componentInfos[0]).toEqual(sourceFile.entryComponentInfo) | ||
expect(sourceFile.componentInfos[0].hasMethod('inited')).toBeTruthy() | ||
expect(sourceFile.componentInfos[0].root.children[0]).toMatchObject({ | ||
textExpr: { type: 1, value: 'Foo' } | ||
}) | ||
}) | ||
it('should parse a shorthand component', () => { | ||
const script = ` | ||
export default { | ||
inited() {} | ||
}` | ||
const template = '<div>Foo</div>' | ||
const parser = new SanFileParser(script, template, '/tmp/foo.san') | ||
const sourceFile = parser.parse() | ||
|
||
expect(sourceFile.getFilePath()).toEqual('/tmp/foo.san') | ||
expect(sourceFile.getFileContent()).toEqual(script) | ||
expect(sourceFile.componentInfos).toHaveLength(1) | ||
expect(sourceFile.componentInfos[0]).toEqual(sourceFile.entryComponentInfo) | ||
expect(sourceFile.componentInfos[0].hasMethod('inited')).toBeTruthy() | ||
expect(sourceFile.componentInfos[0].root.children[0]).toMatchObject({ | ||
textExpr: { type: 1, value: 'Foo' } | ||
}) | ||
}) | ||
}) | ||
}) |