Skip to content

Commit 949a34a

Browse files
committed
(vue): flow to typescript
1 parent eaff22a commit 949a34a

30 files changed

+1892
-40
lines changed

vue/template-compiler-playground/babel.config.js

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
},
10+
],
11+
'@babel/preset-typescript',
12+
],
13+
};

vue/template-compiler-playground/vue3-compiler/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// ../../../node_modules/.bin/webpack-dev-server ./index.ts --config ./webpack.config.js
2-
31
/**
2+
* ../../../node_modules/.bin/jest -c ./jest.config.js
3+
* ../../../node_modules/.bin/webpack-dev-server ./index.ts --config ./webpack.config.js
4+
*
45
* @see https://github.com/vuejs/vue/blob/master/src/platforms/web/compiler/index.js
56
*/
67

vue/template-compiler-playground/vue3-compiler/jest.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ module.exports = {
114114
// A list of paths to directories that Jest should use to search for files in
115115
roots: [
116116
// "<rootDir>"
117-
"test"
117+
"./test"
118118
],
119119

120120
// Allows you to use a custom runner instead of Jest's default test runner

vue/template-compiler-playground/vue3-compiler/test/parser.spec.js

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {baseOptions} from "../platform-web-compiler/options";
2+
import {parse} from "../vue3-template-compiler/parser";
3+
4+
describe('parser', () => {
5+
it('simple element', () => {
6+
const ast = parse('<h1>hello world</h1>', baseOptions);
7+
expect(ast.tag).toBe('h1');
8+
expect(ast.plain).toBe(true);
9+
expect(ast.children[0].text).toBe('hello world');
10+
});
11+
12+
it('interpolation in element', () => {
13+
const ast = parse('<h1>{{msg}}</h1>', baseOptions);
14+
expect(ast.tag).toBe('h1');
15+
expect(ast.plain).toBe(true);
16+
expect(ast.children[0].expression).toBe('_s(msg)');
17+
});
18+
19+
it('child elements', () => {
20+
const ast = parse('<ul><li>hello world</li></ul>', baseOptions);
21+
expect(ast.tag).toBe('ul');
22+
expect(ast.plain).toBe(true);
23+
expect(ast.children[0].tag).toBe('li');
24+
expect(ast.children[0].plain).toBe(true);
25+
expect(ast.children[0].children[0].text).toBe('hello world');
26+
expect(ast.children[0].parent).toBe(ast);
27+
});
28+
29+
it('unary element', () => {
30+
const ast = parse('<hr>', baseOptions);
31+
expect(ast.tag).toBe('hr');
32+
expect(ast.plain).toBe(true);
33+
expect(ast.children.length).toBe(0);
34+
});
35+
36+
it('camelCase element', () => {
37+
const ast = parse('<MyComponent><p>hello world</p></MyComponent>', baseOptions)
38+
expect(ast.tag).toBe('MyComponent')
39+
expect(ast.plain).toBe(true)
40+
expect(ast.children[0].tag).toBe('p')
41+
expect(ast.children[0].plain).toBe(true)
42+
expect(ast.children[0].children[0].text).toBe('hello world')
43+
expect(ast.children[0].parent).toBe(ast)
44+
})
45+
});

vue/template-compiler-playground/vue3-compiler/vue3-template-compiler/parser/html-parser.ts

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type StartTagMatch = {
3939
export const parseHTML = (html: string, options: any) => {
4040
let last: string, lastTag: string;
4141
let index = 0;
42+
const expectHTML = options.expectHTML;
4243

4344
/**
4445
*
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,48 @@
1-
// Parse
2-
31
import {ASTElement, CompilerOptions} from '../types';
2+
import {parseHTML} from "./html-parser";
3+
4+
5+
function makeAttributesMap(attributes: Array<any>) {
6+
const map: any = {};
7+
8+
for (let i = 0, l = attributes.length; i < l; i++) {
9+
map[attributes[i].name] = attributes[i].value;
10+
}
11+
12+
return map;
13+
}
14+
15+
function createASTElement(tag: string, attributes: Array<any>, parent: ASTElement) {
16+
return {
17+
type: 1,
18+
tag,
19+
attributesList: attributes,
20+
attributesMap: makeAttributesMap(attributes),
21+
rawAttributesMap: {},
22+
parent,
23+
children: []
24+
}
25+
}
26+
27+
428

529
/**
630
* Convert HTML string to AST.
731
*/
8-
export const parse = (template: string, options: CompilerOptions): ASTElement => {
9-
32+
export const parse = (template: string, options: any): ASTElement => {
33+
let root: any;
34+
let currentParent: any;
35+
36+
parseHTML(template, {
37+
expectHTML: options.expectHTML,
38+
start(tag: any, attributes: Array<any>) {
39+
let element = createASTElement(tag, attributes, currentParent);
40+
41+
if (!root) {
42+
root = element;
43+
}
44+
}
45+
});
46+
47+
return root;
1048
};

vue/template-compiler-playground/vue3-compiler/vue3-template-compiler/types.ts

+68-9
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
1-
import {
2-
ASTDirective,
3-
ASTElementHandlers,
4-
ASTIfCondition,
5-
ASTNode, DirectiveFunction,
6-
ModuleOptions,
7-
SSROptimizability
8-
} from "vue-template-compiler";
1+
export type ASTAttr = {
2+
name: string;
3+
value: any;
4+
dynamic?: boolean;
5+
start?: number;
6+
end?: number
7+
};
8+
9+
interface ModuleOptions {
10+
preTransformNode: (el: ASTElement) => ASTElement | undefined;
11+
transformNode: (el: ASTElement) => ASTElement | undefined;
12+
postTransformNode: (el: ASTElement) => void;
13+
genData: (el: ASTElement) => string;
14+
transformCode?: (el: ASTElement, code: string) => string;
15+
staticKeys?: string[];
16+
}
17+
18+
export interface ASTIfCondition {
19+
exp: string | undefined;
20+
block: ASTElement;
21+
}
22+
23+
export interface ASTElementHandler {
24+
value: string;
25+
params?: any[];
26+
modifiers: ASTModifiers | undefined;
27+
}
28+
29+
export interface ASTElementHandlers {
30+
[key: string]: ASTElementHandler | ASTElementHandler[];
31+
}
32+
33+
export interface ASTModifiers {
34+
[key: string]: boolean;
35+
}
36+
37+
export interface ASTDirective {
38+
name: string;
39+
rawName: string;
40+
value: string;
41+
arg: string | undefined;
42+
modifiers: ASTModifiers | undefined;
43+
}
44+
type DirectiveFunction = (node: ASTElement, directiveMeta: ASTDirective) => void;
45+
46+
export interface ASTExpression {
47+
type: 2;
48+
expression: string;
49+
text: string;
50+
tokens: (string | Record<string, any>)[];
51+
static?: boolean;
52+
// 2.4 ssr optimization
53+
// ssrOptimizability?: SSROptimizability;
54+
}
55+
56+
export interface ASTText {
57+
type: 3;
58+
text: string;
59+
static?: boolean;
60+
isComment?: boolean;
61+
// 2.4 ssr optimization
62+
// ssrOptimizability?: SSROptimizability;
63+
}
64+
965

1066
export interface ASTElement {
1167
type: 1;
@@ -79,7 +135,7 @@ export interface ASTElement {
79135
wrapListeners?: (code: string) => string;
80136

81137
// 2.4 ssr optimization
82-
ssrOptimizability?: SSROptimizability;
138+
// ssrOptimizability?: SSROptimizability;
83139

84140
// weex specific
85141
appendAsTree?: boolean;
@@ -102,3 +158,6 @@ export interface CompiledResult<ErrorType> {
102158
}
103159

104160

161+
162+
163+
export type ASTNode = ASTElement | ASTText | ASTExpression;

vue/vue/src/compiler/babel.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
},
10+
],
11+
'@babel/preset-typescript',
12+
],
13+
};

0 commit comments

Comments
 (0)