Skip to content

Commit e58beec

Browse files
committed
refactor(compiler/types): convert compiler options documentation to jsdoc
BREAKING CHANGE: `getTextMode` compiler option signature has changed from ```ts (tag: string, ns: string, parent: ElementNode | undefined) => TextModes ``` to ```ts (node: ElementNode, parent: ElementNode | undefined) => TextModes ```
1 parent 304ab8c commit e58beec

File tree

5 files changed

+123
-49
lines changed

5 files changed

+123
-49
lines changed

packages/compiler-core/__tests__/parse.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2515,7 +2515,7 @@ foo
25152515
}
25162516
return ns
25172517
},
2518-
getTextMode: tag => {
2518+
getTextMode: ({ tag }) => {
25192519
if (tag === 'textarea') {
25202520
return TextModes.RCDATA
25212521
}

packages/compiler-core/src/options.ts

+119-45
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,44 @@ import {
99
import { ParserPlugin } from '@babel/parser'
1010

1111
export interface ParserOptions {
12-
// e.g. platform native elements, e.g. <div> for browsers
12+
/**
13+
* e.g. platform native elements, e.g. <div> for browsers
14+
*/
1315
isNativeTag?: (tag: string) => boolean
14-
// e.g. native elements that can self-close, e.g. <img>, <br>, <hr>
16+
/**
17+
* e.g. native elements that can self-close, e.g. <img>, <br>, <hr>
18+
*/
1519
isVoidTag?: (tag: string) => boolean
16-
// e.g. elements that should preserve whitespace inside, e.g. <pre>
20+
/**
21+
* e.g. elements that should preserve whitespace inside, e.g. <pre>
22+
*/
1723
isPreTag?: (tag: string) => boolean
18-
// platform-specific built-in components e.g. <Transition>
24+
/**
25+
* Platform-specific built-in components e.g. <Transition>
26+
*/
1927
isBuiltInComponent?: (tag: string) => symbol | void
20-
// separate option for end users to extend the native elements list
28+
/**
29+
* Separate option for end users to extend the native elements list
30+
*/
2131
isCustomElement?: (tag: string) => boolean
32+
/**
33+
* Get tag namespace
34+
*/
2235
getNamespace?: (tag: string, parent: ElementNode | undefined) => Namespace
36+
/**
37+
* Get text parsing mode for this element
38+
*/
2339
getTextMode?: (
24-
tag: string,
25-
ns: Namespace,
40+
node: ElementNode,
2641
parent: ElementNode | undefined
2742
) => TextModes
28-
delimiters?: [string, string] // ['{{', '}}']
43+
/**
44+
* @default ['{{', '}}']
45+
*/
46+
delimiters?: [string, string]
47+
/**
48+
* Only needed for DOM compilers
49+
*/
2950
decodeEntities?: (rawText: string, asAttr: boolean) => string
3051
onError?: (error: CompilerError) => void
3152
}
@@ -36,64 +57,117 @@ export type HoistTransform = (
3657
) => JSChildNode
3758

3859
export interface TransformOptions {
60+
/**
61+
* An array of node trasnforms to be applied to every AST node.
62+
*/
3963
nodeTransforms?: NodeTransform[]
64+
/**
65+
* An object of { name: transform } to be applied to every directive attribute
66+
* node found on element nodes.
67+
*/
4068
directiveTransforms?: Record<string, DirectiveTransform | undefined>
41-
// an optional hook to transform a node being hoisted.
42-
// used by compiler-dom to turn hoisted nodes into stringified HTML vnodes.
69+
/**
70+
* An optional hook to transform a node being hoisted.
71+
* used by compiler-dom to turn hoisted nodes into stringified HTML vnodes.
72+
* @default null
73+
*/
4374
transformHoist?: HoistTransform | null
75+
/**
76+
* If the pairing runtime provides additional built-in elements, use this to
77+
* mark them as built-in so the compiler will generate component vnodes
78+
* for them.
79+
*/
4480
isBuiltInComponent?: (tag: string) => symbol | void
45-
// Transform expressions like {{ foo }} to `_ctx.foo`.
46-
// If this option is false, the generated code will be wrapped in a
47-
// `with (this) { ... }` block.
48-
// - This is force-enabled in module mode, since modules are by default strict
49-
// and cannot use `with`
50-
// - Default: mode === 'module'
81+
/**
82+
* Transform expressions like {{ foo }} to `_ctx.foo`.
83+
* If this option is false, the generated code will be wrapped in a
84+
* `with (this) { ... }` block.
85+
* - This is force-enabled in module mode, since modules are by default strict
86+
* and cannot use `with`
87+
* @default mode === 'module'
88+
*/
5189
prefixIdentifiers?: boolean
52-
// Hoist static VNodes and props objects to `_hoisted_x` constants
53-
// - Default: false
90+
/**
91+
* Hoist static VNodes and props objects to `_hoisted_x` constants
92+
* @default false
93+
*/
5494
hoistStatic?: boolean
55-
// Cache v-on handlers to avoid creating new inline functions on each render,
56-
// also avoids the need for dynamically patching the handlers by wrapping it.
57-
// e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
58-
// option it's compiled to:
59-
// `{ onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }`
60-
// - Requires "prefixIdentifiers" to be enabled because it relies on scope
61-
// analysis to determine if a handler is safe to cache.
62-
// - Default: false
95+
/**
96+
* Cache v-on handlers to avoid creating new inline functions on each render,
97+
* also avoids the need for dynamically patching the handlers by wrapping it.
98+
* e.g `@click="foo"` by default is compiled to `{ onClick: foo }`. With this
99+
* option it's compiled to:
100+
* ```js
101+
* { onClick: _cache[0] || (_cache[0] = e => _ctx.foo(e)) }
102+
* ```
103+
* - Requires "prefixIdentifiers" to be enabled because it relies on scope
104+
* analysis to determine if a handler is safe to cache.
105+
* @default false
106+
*/
63107
cacheHandlers?: boolean
64-
// a list of parser plugins to enable for @babel/parser
65-
// https://babeljs.io/docs/en/next/babel-parser#plugins
108+
/**
109+
* A list of parser plugins to enable for `@babel/parser`, which is used to
110+
* parse expressions in bindings and interpolations.
111+
* https://babeljs.io/docs/en/next/babel-parser#plugins
112+
*/
66113
expressionPlugins?: ParserPlugin[]
67-
// SFC scoped styles ID
114+
/**
115+
* SFC scoped styles ID
116+
*/
68117
scopeId?: string | null
118+
/**
119+
* Generate SSR-optimized render functions instead.
120+
* The resulting funciton must be attached to the component via the
121+
* `ssrRender` option instead of `render`.
122+
*/
69123
ssr?: boolean
70124
onError?: (error: CompilerError) => void
71125
}
72126

73127
export interface CodegenOptions {
74-
// - Module mode will generate ES module import statements for helpers
75-
// and export the render function as the default export.
76-
// - Function mode will generate a single `const { helpers... } = Vue`
77-
// statement and return the render function. It is meant to be used with
78-
// `new Function(code)()` to generate a render function at runtime.
79-
// - Default: 'function'
128+
/**
129+
* - `module` mode will generate ES module import statements for helpers
130+
* and export the render function as the default export.
131+
* - `function` mode will generate a single `const { helpers... } = Vue`
132+
* statement and return the render function. It expects `Vue` to be globally
133+
* available (or passed by wrapping the code with an IIFE). It is meant to be
134+
* used with `new Function(code)()` to generate a render function at runtime.
135+
* @default 'function'
136+
*/
80137
mode?: 'module' | 'function'
81-
// Generate source map?
82-
// - Default: false
138+
/**
139+
* Generate source map?
140+
* @default false
141+
*/
83142
sourceMap?: boolean
84-
// Filename for source map generation.
85-
// - Default: `template.vue.html`
143+
/**
144+
* Filename for source map generation.
145+
* @default 'template.vue.html'
146+
*/
86147
filename?: string
87-
// SFC scoped styles ID
148+
/**
149+
* SFC scoped styles ID
150+
*/
88151
scopeId?: string | null
89-
// we need to know about this to generate proper preambles
90-
prefixIdentifiers?: boolean
91-
// option to optimize helper import bindings via variable assignment
92-
// (only used for webpack code-split)
152+
/**
153+
* Option to optimize helper import bindings via variable assignment
154+
* (only used for webpack code-split)
155+
* @default false
156+
*/
93157
optimizeBindings?: boolean
94-
// for specifying where to import helpers
158+
/**
159+
* Customize where to import runtime helpers from.
160+
* @default 'vue'
161+
*/
95162
runtimeModuleName?: string
163+
/**
164+
* Customize the global variable name of `Vue` to get helpers from
165+
* in function mode
166+
* @default 'Vue'
167+
*/
96168
runtimeGlobalName?: string
169+
// we need to know this during codegen to generate proper preambles
170+
prefixIdentifiers?: boolean
97171
// generate ssr-specific code?
98172
ssr?: boolean
99173
}

packages/compiler-core/src/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ function parseElement(
373373

374374
// Children.
375375
ancestors.push(element)
376-
const mode = context.options.getTextMode(element.tag, element.ns, parent)
376+
const mode = context.options.getTextMode(element, parent)
377377
const children = parseChildren(context, mode, ancestors)
378378
ancestors.pop()
379379

packages/compiler-dom/src/parserOptions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const parserOptions: ParserOptions = {
8686
},
8787

8888
// https://html.spec.whatwg.org/multipage/parsing.html#parsing-html-fragments
89-
getTextMode(tag: string, ns: DOMNamespaces): TextModes {
89+
getTextMode({ tag, ns }: ElementNode): TextModes {
9090
if (ns === DOMNamespaces.HTML) {
9191
if (tag === 'textarea' || tag === 'title') {
9292
return TextModes.RCDATA

packages/compiler-sfc/src/parse.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export function parse(
9696
isNativeTag: () => true,
9797
// preserve all whitespaces
9898
isPreTag: () => true,
99-
getTextMode: (tag, _ns, parent) => {
99+
getTextMode: ({ tag }, parent) => {
100100
// all top level elements except <template> are parsed as raw text
101101
// containers
102102
if (!parent && tag !== 'template') {

0 commit comments

Comments
 (0)