Skip to content
This repository was archived by the owner on Jun 24, 2025. It is now read-only.

Commit 004751b

Browse files
committed
- mode and startRule aware parsing
- Provide rule text fully in input box by default
1 parent 7bf7d56 commit 004751b

File tree

6 files changed

+114
-22
lines changed

6 files changed

+114
-22
lines changed

dist/jsdoctypeparser-demo.js

Lines changed: 45 additions & 9 deletions
Large diffs are not rendered by default.

index.html

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,38 @@ <h3>What is jsdoctypeparser?</h3>
4848
<hr />
4949
<h3>Let's try!</h3>
5050
<div class="type-expr-form">
51-
<form id="form" class="form-inline">
51+
<form id="form" class="form">
5252
<div class="form-group">
5353
<span class="type-exp">/** @type {</span>
5454
<textarea id="input" class="form-control" placeholder="?TypeExpression="></textarea>
5555
<span class="type-exp">} */</span>
5656
</div>
57+
<div class="form-group">
58+
<h3>Options</h3>
59+
</div>
60+
<div class="form-group">
61+
<label>
62+
Rule: &nbsp;
63+
<select id="startRule">
64+
<option value="TopTypeExpr">Top-level rule</option>
65+
<option value="BroadNamepathExpr">Namepath (broad)</option>
66+
<option value="NamepathExpr">Namepath (narrow)</option>
67+
<option value="ExternalNameExpr">External namepath</option>
68+
<option value="ModuleNameExpr">Module namepath (not in TypeScript)</option>
69+
</select>
70+
</label>
71+
</div>
72+
<div class="form-group">
73+
<label>
74+
Mode: &nbsp;
75+
<select id="mode">
76+
<option value="jsdoc">JSDoc</option>
77+
<option value="closure">Closure (Google Closure Compiler)</option>
78+
<option value="typescript">TypeScript</option>
79+
<option value="permissive">Permissive</option>
80+
</select>
81+
</label>
82+
</div>
5783
</form>
5884
</div>
5985
<div class="alert-container">

index.js

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
'use strict';
44
const jsdoctypeparser = require('jsdoctypeparser');
55

6+
const modeMap = new Map([
7+
['permissive', jsdoctypeparser.JSDocTypeSyntaxError],
8+
['jsdoc', jsdoctypeparser.JSDocSyntaxError],
9+
['closure', jsdoctypeparser.ClosureSyntaxError],
10+
['typescript', jsdoctypeparser.TypeScriptSyntaxError],
11+
]);
612
const INITIAL_TYPE_EXPR = '?TypeExpression=';
713

814
class ParseSuccessfulAlert {
@@ -107,16 +113,19 @@
107113
super();
108114
}
109115

110-
parse (typeExpr) {
116+
parse (typeExpr, {mode, startRule}) {
111117
try {
112-
const ast = jsdoctypeparser.parse(typeExpr);
118+
const ast = jsdoctypeparser.parse(typeExpr, {mode, startRule});
113119

114120
this.ast = ast;
115121
this.hasSyntaxError = false;
116122
this.errorMessage = '';
117123
}
118124
catch (err) {
119-
if (!(err instanceof jsdoctypeparser.JSDocTypeSyntaxError)) throw err;
125+
console.log(err);
126+
127+
const syntaxError = modeMap.get(mode);
128+
if (!(err instanceof syntaxError)) throw err;
120129

121130
this.ast = null;
122131
this.hasSyntaxError = true;
@@ -135,18 +144,36 @@
135144
createParseResultView(typeExprModel);
136145

137146
const $input = $('#input');
138-
$input.on('change', function() {
147+
const $mode = $('#mode');
148+
const $startRule = $('#startRule');
149+
150+
function getOptions () {
151+
const mode = $mode.val();
152+
const startRule = $startRule.val();
153+
return {
154+
mode,
155+
startRule,
156+
};
157+
}
158+
159+
function parseInput () {
139160
const typeExprStr = $input.val();
140-
typeExprModel.parse(typeExprStr);
141-
});
161+
typeExprModel.parse(typeExprStr, getOptions());
162+
}
163+
164+
$input.on('change', parseInput);
165+
$mode.on('change', parseInput);
166+
$startRule.on('change', parseInput);
142167

143168
const $form = $('#form');
144169
$form.on('submit', function(e) {
145170
// Prevent page reloading when form submitted.
146171
e.preventDefault();
147172
});
148173

149-
typeExprModel.parse(INITIAL_TYPE_EXPR, true);
174+
$input.val(INITIAL_TYPE_EXPR);
175+
176+
parseInput();
150177
}
151178

152179
bootstrap();

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"eslint": "^7.4.0",
3939
"gulp": "^4.0.2",
4040
"gulp-watch": "^5.0.1",
41-
"jsdoctypeparser": "8.0.0",
41+
"jsdoctypeparser": "9.0.0",
4242
"vinyl-buffer": "^1.0.1",
4343
"vinyl-source-stream": "^2.0.0"
4444
}

styles/jsdoctypeparser-demo.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
margin-bottom: 10px;
3434
}
3535

36+
.form-group {
37+
padding-bottom: 10px;
38+
}
39+
3640
#ie, #err, #success {
3741
display: none;
3842
}
@@ -41,4 +45,3 @@
4145
color: #b94a48;
4246
border: none;
4347
background-color: transparent;
44-

0 commit comments

Comments
 (0)