Skip to content
This repository was archived by the owner on Mar 8, 2019. It is now read-only.

Commit f0a7f76

Browse files
authored
chore: simplify ts typings (#106)
* simplify vue-template-compiler typings * fix typings * remove tslint comments * remove one last comment * fix tsconfig
1 parent 109cd4e commit f0a7f76

18 files changed

+61
-307
lines changed

package-lock.json

+8-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@
3636
"dependencies": {
3737
"@babel/parser": "^7.2.3",
3838
"@babel/types": "^7.0.0",
39-
"ast-types": "^0.11.7",
39+
"ast-types": "^0.12.2",
4040
"hash-sum": "^1.0.2",
4141
"lru-cache": "^4.1.5",
4242
"pug": "^2.0.3",
43-
"recast": "^0.16.2",
43+
"recast": "^0.17.3",
4444
"ts-map": "^1.0.3",
4545
"typescript": "^3.2.2",
4646
"vue": "^2.6.3",

src/@types/ast-types.d.ts

-14
This file was deleted.

src/@types/recast.d.ts

-12
This file was deleted.

src/@types/vue-template-compiler.d.ts

+2-212
Original file line numberDiff line numberDiff line change
@@ -1,220 +1,10 @@
11
/**
2-
* This declaration is copied from https://github.com/vuejs/vue/pull/7918
3-
* which may included vue-template-compiler v2.6.0.
2+
* augment the original types with the needed comments
43
*/
4+
import 'vue-template-compiler'
55

66
declare module 'vue-template-compiler' {
7-
import Vue, { VNode } from 'vue'
8-
9-
/*
10-
* Template compilation options / results
11-
*/
127
interface CompilerOptions {
13-
modules?: ModuleOptions[]
14-
directives?: Record<string, DirectiveFunction>
15-
preserveWhitespace?: boolean
168
comments?: boolean
179
}
18-
19-
interface CompiledResult {
20-
ast: ASTElement | undefined
21-
render: string
22-
staticRenderFns: string[]
23-
errors: string[]
24-
tips: string[]
25-
}
26-
27-
interface CompiledResultFunctions {
28-
render: () => VNode
29-
staticRenderFns: (() => VNode)[]
30-
}
31-
32-
interface ModuleOptions {
33-
preTransformNode: (el: ASTElement) => ASTElement | undefined
34-
transformNode: (el: ASTElement) => ASTElement | undefined
35-
postTransformNode: (el: ASTElement) => void
36-
genData: (el: ASTElement) => string
37-
transformCode?: (el: ASTElement, code: string) => string
38-
staticKeys?: string[]
39-
}
40-
41-
type DirectiveFunction = (node: ASTElement, directiveMeta: ASTDirective) => void
42-
43-
/*
44-
* AST Types
45-
*/
46-
47-
/**
48-
* - 0: FALSE - whole sub tree un-optimizable
49-
* - 1: FULL - whole sub tree optimizable
50-
* - 2: SELF - self optimizable but has some un-optimizable children
51-
* - 3: CHILDREN - self un-optimizable but have fully optimizable children
52-
* - 4: PARTIAL - self un-optimizable with some un-optimizable children
53-
*/
54-
export type SSROptimizability = 0 | 1 | 2 | 3 | 4
55-
56-
export interface ASTModifiers {
57-
[key: string]: boolean
58-
}
59-
60-
export interface ASTIfCondition {
61-
exp: string | undefined
62-
block: ASTElement
63-
}
64-
65-
export interface ASTElementHandler {
66-
value: string
67-
params?: any[]
68-
modifiers: ASTModifiers | undefined
69-
}
70-
71-
export interface ASTElementHandlers {
72-
[key: string]: ASTElementHandler | ASTElementHandler[]
73-
}
74-
75-
export interface ASTDirective {
76-
name: string
77-
rawName: string
78-
value: string
79-
arg: string | undefined
80-
modifiers: ASTModifiers | undefined
81-
}
82-
83-
export type ASTNode = ASTElement | ASTText | ASTExpression
84-
85-
export interface ASTElement {
86-
type: 1
87-
tag: string
88-
attrsList: { name: string; value: any }[]
89-
attrsMap: Record<string, any>
90-
parent: ASTElement | undefined
91-
children: ASTNode[]
92-
93-
processed?: true
94-
95-
static?: boolean
96-
staticRoot?: boolean
97-
staticInFor?: boolean
98-
staticProcessed?: boolean
99-
hasBindings?: boolean
100-
101-
text?: string
102-
attrs?: { name: string; value: any }[]
103-
props?: { name: string; value: string }[]
104-
plain?: boolean
105-
pre?: true
106-
ns?: string
107-
108-
component?: string
109-
inlineTemplate?: true
110-
transitionMode?: string | null
111-
slotName?: string
112-
slotTarget?: string
113-
slotScope?: string
114-
scopedSlots?: Record<string, ASTElement>
115-
116-
ref?: string
117-
refInFor?: boolean
118-
119-
if?: string
120-
ifProcessed?: boolean
121-
elseif?: string
122-
else?: true
123-
ifConditions?: ASTIfCondition[]
124-
125-
for?: string
126-
forProcessed?: boolean
127-
key?: string
128-
alias?: string
129-
iterator1?: string
130-
iterator2?: string
131-
132-
staticClass?: string
133-
classBinding?: string
134-
staticStyle?: string
135-
styleBinding?: string
136-
events?: ASTElementHandlers
137-
nativeEvents?: ASTElementHandlers
138-
139-
transition?: string | true
140-
transitionOnAppear?: boolean
141-
142-
model?: {
143-
value: string
144-
callback: string
145-
expression: string
146-
}
147-
148-
directives?: ASTDirective[]
149-
150-
forbidden?: true
151-
once?: true
152-
onceProcessed?: boolean
153-
wrapData?: (code: string) => string
154-
wrapListeners?: (code: string) => string
155-
156-
// 2.4 ssr optimization
157-
ssrOptimizability?: SSROptimizability
158-
159-
// weex specific
160-
appendAsTree?: boolean
161-
}
162-
163-
export interface ASTExpression {
164-
type: 2
165-
expression: string
166-
text: string
167-
tokens: (string | Record<string, any>)[]
168-
static?: boolean
169-
// 2.4 ssr optimization
170-
ssrOptimizability?: SSROptimizability
171-
}
172-
173-
export interface ASTText {
174-
type: 3
175-
text: string
176-
static?: boolean
177-
isComment?: boolean
178-
// 2.4 ssr optimization
179-
ssrOptimizability?: SSROptimizability
180-
}
181-
182-
/*
183-
* SFC parser related types
184-
*/
185-
interface SFCParserOptions {
186-
pad?: true | 'line' | 'space'
187-
}
188-
189-
export interface SFCBlock {
190-
type: string
191-
content: string
192-
attrs: Record<string, string>
193-
start?: number
194-
end?: number
195-
lang?: string
196-
src?: string
197-
scoped?: boolean
198-
module?: string | boolean
199-
}
200-
201-
export interface SFCDescriptor {
202-
template: SFCBlock | undefined
203-
script: SFCBlock | undefined
204-
styles: SFCBlock[]
205-
customBlocks: SFCBlock[]
206-
}
207-
208-
/*
209-
* Exposed functions
210-
*/
211-
export function compile(template: string, options?: CompilerOptions): CompiledResult
212-
213-
export function compileToFunctions(template: string): CompiledResultFunctions
214-
215-
export function ssrCompile(template: string, options?: CompilerOptions): CompiledResult
216-
217-
export function ssrCompileToFunctions(template: string): CompiledResultFunctions
218-
219-
export function parseComponent(file: string, options?: SFCParserOptions): SFCDescriptor
22010
}

src/parse-script.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { ParserPlugin } from '@babel/parser'
22
import * as bt from '@babel/types'
33
import { NodePath } from 'ast-types'
4+
import recast from 'recast'
45
import Map from 'ts-map'
56
import buildParser from './babel-parser'
67
import { Documentation } from './Documentation'
8+
import { ParseOptions } from './parse'
79
import cacher from './utils/cacher'
810
import resolveExportedComponent from './utils/resolveExportedComponent'
911

10-
// tslint:disable-next-line:no-var-requires
11-
import recast = require('recast')
12-
import { ParseOptions } from './parse'
13-
1412
const ERROR_MISSING_DEFINITION = 'No suitable component definition found'
1513

1614
export type Handler = (

src/script-handlers/classDisplayNameHandler.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function getArgFromDecorator(
3535
const exp = p.get('expression')
3636
const decoratorIdenifier = bt.isCallExpression(exp.node) ? exp.node.callee : exp.node
3737
return 'Component' === (bt.isIdentifier(decoratorIdenifier) ? decoratorIdenifier.name : null)
38-
})[0]
38+
}, null)[0]
3939
.get('expression')
4040
if (bt.isCallExpression(expForDecorator.node)) {
4141
return expForDecorator.get('arguments', 0) as NodePath<

src/script-handlers/eventHandler.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as bt from '@babel/types'
22
import { NodePath } from 'ast-types'
3+
import recast from 'recast'
34
import {
45
BlockTag,
56
DocBlockTags,
@@ -15,12 +16,9 @@ export interface TypedParamTag extends ParamTag {
1516
type: ParamType
1617
}
1718

18-
// tslint:disable-next-line:no-var-requires
19-
import recast = require('recast')
20-
2119
export default function eventHandler(documentation: Documentation, path: NodePath) {
22-
recast.visit(path, {
23-
visitCallExpression(pathExpression: NodePath<bt.CallExpression>) {
20+
recast.visit(path.node, {
21+
visitCallExpression(pathExpression) {
2422
if (
2523
bt.isMemberExpression(pathExpression.node.callee) &&
2624
bt.isThisExpression(pathExpression.node.callee.object) &&

src/script-handlers/oldEventsHandler.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import * as bt from '@babel/types'
22
import { NodePath } from 'ast-types'
3+
import recast from 'recast'
34
import { BlockTag, Documentation, Tag } from '../Documentation'
45
import { setEventDescriptor } from '../script-handlers/eventHandler'
56
import { parseDocblock } from '../utils/getDocblock'
67
import getDoclets from '../utils/getDoclets'
78

8-
// tslint:disable-next-line:no-var-requires
9-
import recast = require('recast')
10-
119
export default function oldEventsHanlder(
1210
documentation: Documentation,
1311
path: NodePath,

src/script-handlers/propHandler.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import * as bt from '@babel/types'
22
import { NodePath } from 'ast-types'
3+
import recast from 'recast'
34
import { BlockTag, DocBlockTags, Documentation, PropDescriptor } from '../Documentation'
45
import getDocblock from '../utils/getDocblock'
56
import getDoclets from '../utils/getDoclets'
67
import transformTagsIntoObject from '../utils/transformTagsIntoObject'
78

8-
// tslint:disable-next-line:no-var-requires
9-
import recast = require('recast')
10-
119
type ValueLitteral = bt.StringLiteral | bt.BooleanLiteral | bt.NumericLiteral
1210

1311
export default function propHandler(documentation: Documentation, path: NodePath) {

0 commit comments

Comments
 (0)