-
-
Notifications
You must be signed in to change notification settings - Fork 119
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
1 parent
3c7b5ba
commit 421b07b
Showing
11 changed files
with
117 additions
and
42 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,15 @@ | ||
<template> | ||
<div></div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
data () { | ||
return { | ||
props: { | ||
a: String | ||
} | ||
} | ||
} | ||
} | ||
</script> |
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,11 @@ | ||
<template> | ||
<div></div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
props: { | ||
a: String | ||
} | ||
} | ||
</script> |
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,36 @@ | ||
import traverse from '@babel/traverse' | ||
import { getAST } from './utils' | ||
import { isVueComponent, isPropsOption } from '../helpers' | ||
|
||
describe('helpers', () => { | ||
const ast1 = getAST('validProps.vue') | ||
const ast2 = getAST('invalidProps.vue') | ||
|
||
test('The default export should be a Vue component', () => { | ||
traverse(ast1, { | ||
ExportDefaultDeclaration(path: any) { | ||
expect(isVueComponent(path.node)).toBe(true) | ||
} | ||
}) | ||
}) | ||
|
||
test('The props property is a Vue component option', () => { | ||
traverse(ast1, { | ||
ObjectProperty(path: any) { | ||
if (path.node.key.name === 'props') { | ||
expect(isPropsOption(path)).toBe(true) | ||
} | ||
} | ||
}) | ||
}) | ||
|
||
test('The props property is not a Vue component option', () => { | ||
traverse(ast2, { | ||
ObjectProperty(path: any) { | ||
if (path.node.key.name === 'props') { | ||
expect(isPropsOption(path)).toBe(false) | ||
} | ||
} | ||
}) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,12 +1,7 @@ | ||
import sfcToEstree from '../sfcToEstree' | ||
import * as path from 'path' | ||
import * as fs from 'fs' | ||
import * as bt from '@babel/types' | ||
|
||
const p = path.resolve(__dirname, '../../__fixtures__/common.vue') | ||
const source = fs.readFileSync(p, 'utf-8') | ||
import { getAST } from './utils' | ||
|
||
test('The type of AST should be File', () => { | ||
const ast = sfcToEstree(source) | ||
const ast = getAST('common.vue') | ||
expect(bt.isFile(ast)).toBe(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,11 @@ | ||
import * as path from 'path' | ||
import * as fs from 'fs' | ||
import sfcToEstree from '../sfcToEstree' | ||
|
||
export function getAST(fileName: string): object { | ||
const p = path.resolve(__dirname, `../../__fixtures__/${fileName}`) | ||
const source = fs.readFileSync(p, 'utf-8') | ||
return sfcToEstree(source) | ||
} | ||
|
||
test('placeholder', () => {}) |
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,36 @@ | ||
import generate from '@babel/generator' | ||
import * as bt from '@babel/types' | ||
|
||
/** | ||
* If a node satisfies the following conditions, then we will use this node as a Vue component. | ||
* 1. It is a default export | ||
* 2. others... | ||
*/ | ||
export function isVueComponent(node: object): boolean { | ||
return bt.isExportDefaultDeclaration(node) | ||
} | ||
|
||
export function getValueFromGenerate(node: any) { | ||
let code: string = 'return' | ||
const { code: genCode } = generate(node) | ||
code += genCode | ||
const fn = new Function(code) | ||
try { | ||
return fn() | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} | ||
|
||
export function isPropsOption(path: any): boolean { | ||
if ( | ||
bt.isObjectProperty(path.node) && | ||
path.parentPath && | ||
path.parentPath.parentPath && | ||
isVueComponent(path.parentPath.parentPath.node) | ||
) { | ||
const keyPath = path.get('key') | ||
return keyPath.node.name === 'props' | ||
} | ||
return false | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
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