Skip to content

Commit 0e97a7b

Browse files
committed
Whiteblock-preferred mode
commit 5e45d5e Author: William C. Johnson <[email protected]> Date: Sat Sep 30 18:13:56 2017 -0400 Lightscript OBA fixup commit 1aded08 Author: William C. Johnson <[email protected]> Date: Sat Sep 30 17:39:12 2017 -0400 Whiteblock-preferred, step 1
1 parent 0a87973 commit 0e97a7b

File tree

89 files changed

+9051
-503
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

89 files changed

+9051
-503
lines changed

src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ export function getAvailablePlugins() {
3737
return result;
3838
}
3939

40+
export function getPluginMetadata() {
41+
return pluginMetadata;
42+
}
43+
4044
export { tokTypes };

src/plugins/lightscript.js

+90-17
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,36 @@ pp.parseObjectWhiteBlock = function(node, blockIndentLevel) {
163163
return this.finishNode(node, "BlockStatement");
164164
};
165165

166+
pp.tryParseObjectWhiteBlock = function(node, blockIndentLevel) {
167+
const state = this.state.clone();
168+
try {
169+
return [this.parseObjectWhiteBlock(node, blockIndentLevel)];
170+
} catch (err) {
171+
this.state = state;
172+
return [null, err];
173+
}
174+
};
175+
176+
pp.rethrowObjParseError = function(objParseResult, blockParseError) {
177+
const objParseError = objParseResult ? objParseResult[1] : null;
178+
if (objParseError) {
179+
if (objParseError.message === "WRONG_SPECULATIVE_BRANCH") {
180+
throw blockParseError;
181+
} else {
182+
throw objParseError;
183+
}
184+
} else {
185+
throw blockParseError;
186+
}
187+
};
188+
189+
pp.parseNonemptyWhiteBlock = function(node, indentLevel) {
190+
this.parseBlockBody(node, false, false, indentLevel);
191+
if (!node.body.length) {
192+
this.unexpected(node.start, "Expected an Indent or Statement");
193+
}
194+
};
195+
166196
pp.parseInlineWhiteBlock = function(node) {
167197
if (this.state.type.startsExpr) return this.parseMaybeAssign();
168198
// oneline statement case
@@ -179,9 +209,17 @@ pp.parseMultilineWhiteBlock = function(node, indentLevel) {
179209
return this.parseObjectWhiteBlock(node, indentLevel);
180210
}
181211

182-
this.parseBlockBody(node, false, false, indentLevel);
183-
if (!node.body.length) {
184-
this.unexpected(node.start, "Expected an Indent or Statement");
212+
if (this.match(tt.braceL) && this.hasPlugin("whiteblockPreferred")) {
213+
const objParseResult = this.tryParseObjectWhiteBlock(node, indentLevel);
214+
if (objParseResult[0]) return objParseResult[0];
215+
216+
try {
217+
this.parseNonemptyWhiteBlock(node, indentLevel);
218+
} catch (err) {
219+
this.rethrowObjParseError(objParseResult, err);
220+
}
221+
} else {
222+
this.parseNonemptyWhiteBlock(node, indentLevel);
185223
}
186224

187225
this.addExtra(node, "curly", false);
@@ -198,9 +236,25 @@ pp.parseWhiteBlock = function (isExpression?) {
198236
return this.parseInlineWhiteBlock(node);
199237
}
200238

201-
if (this.match(tt.braceL) && this.hasPlugin("whiteblockOnly")) {
202-
// Parse as object
203-
return this.parseObjectWhiteBlock(node, indentLevel);
239+
if (this.match(tt.braceL)) {
240+
if (this.hasPlugin("whiteblockOnly")) {
241+
return this.parseObjectWhiteBlock(node, indentLevel);
242+
}
243+
244+
let objParseResult = null;
245+
if (this.hasPlugin("whiteblockPreferred")) {
246+
objParseResult = this.tryParseObjectWhiteBlock(node, indentLevel);
247+
if (objParseResult[0]) return objParseResult[0];
248+
249+
try {
250+
this.state.nestedBlockLevel++;
251+
const stmt = this.parseStatement(false);
252+
this.state.nestedBlockLevel--;
253+
return stmt;
254+
} catch (err) {
255+
this.rethrowObjParseError(objParseResult, err);
256+
}
257+
}
204258
}
205259

206260
this.state.nestedBlockLevel++;
@@ -270,6 +324,35 @@ pp.parseArrowType = function (node) {
270324

271325
// largely c/p from parseFunctionBody
272326

327+
pp.parseBraceArrowFunctionBody = function(node, indentLevel) {
328+
// In whiteblock-only mode, `{` must be introducing an object
329+
if (this.hasPlugin("whiteblockOnly")) {
330+
return this.parseObjectWhiteBlock(node, indentLevel);
331+
}
332+
333+
// In whiteblock-preferred mode, try to parse an object, otherwise fall back on
334+
// a braceblock.
335+
if (this.hasPlugin("whiteblockPreferred")) {
336+
const objParseResult = this.tryParseObjectWhiteBlock(node, indentLevel);
337+
if (objParseResult[0]) return objParseResult[0];
338+
this.next();
339+
340+
try {
341+
this.parseBlockBody(node, true, false, tt.braceR);
342+
} catch (err) {
343+
this.rethrowObjParseError(objParseResult, err);
344+
}
345+
346+
this.addExtra(node, "curly", true);
347+
return this.finishNode(node, "BlockStatement");
348+
}
349+
350+
this.next();
351+
this.parseBlockBody(node, true, false, tt.braceR);
352+
this.addExtra(node, "curly", true);
353+
return this.finishNode(node, "BlockStatement");
354+
};
355+
273356
pp.parseArrowFunctionBody = function (node) {
274357
// set and reset state surrounding block
275358
const oldInAsync = this.state.inAsync,
@@ -286,17 +369,7 @@ pp.parseArrowFunctionBody = function (node) {
286369
this.expect(tt.arrow);
287370
if (!this.isLineBreak()) {
288371
if (this.match(tt.braceL)) {
289-
if (this.hasPlugin("whiteblockOnly")) {
290-
// In whiteblock mode, arrows that start with `{` must be object exprs
291-
node.body = this.parseObjectWhiteBlock(nodeAtArrow, indentLevel);
292-
} else {
293-
// restart node at brace start instead of arrow start
294-
node.body = this.startNode();
295-
this.next();
296-
this.parseBlockBody(node.body, true, false, tt.braceR);
297-
this.addExtra(node.body, "curly", true);
298-
node.body = this.finishNode(node.body, "BlockStatement");
299-
}
372+
node.body = this.parseBraceArrowFunctionBody(this.startNode(), indentLevel);
300373
} else {
301374
node.body = this.parseInlineWhiteBlock(nodeAtArrow);
302375
}

src/registerPlugins.js

+12
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ import { matchCoreSyntax, match } from "./plugins/match";
1414
function noncePlugin() {}
1515

1616
export default function registerPlugins(plugins, metadata) {
17+
metadata._reverseDeps = {};
18+
1719
function registerPlugin(name, plugin, meta) {
1820
if (!plugin) plugin = noncePlugin;
1921
plugins[name] = plugin;
2022
metadata[name] = meta;
23+
if (meta && meta.dependencies) {
24+
meta.dependencies.forEach( (dep) => {
25+
if (!metadata._reverseDeps[dep]) metadata._reverseDeps[dep] = [];
26+
metadata._reverseDeps[dep].push(name);
27+
});
28+
}
2129
}
2230

2331
registerPlugin("doExpressions");
@@ -86,4 +94,8 @@ export default function registerPlugins(plugins, metadata) {
8694
registerPlugin("whiteblockOnly", noncePlugin, {
8795
dependencies: ["lightscript"]
8896
});
97+
98+
registerPlugin("whiteblockPreferred", noncePlugin, {
99+
dependencies: ["lightscript"]
100+
});
89101
}

test/fixtures/comments/options.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
},
88
"noLsc": {
99
"allPlugins": true,
10-
"excludePlugins": ["lightscript", "match", "splatComprehension", "whiteblockOnly"]
10+
"excludePlugins": ["lightscript", "match", "splatComprehension", "whiteblockOnly", "whiteblockPreferred"]
1111
}
1212
}
1313
}

test/fixtures/core/options.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"noLsc": {
1010
"allPlugins": true,
11-
"excludePlugins": ["lightscript", "match", "splatComprehension", "whiteblockOnly"]
11+
"excludePlugins": ["lightscript"]
1212
}
1313
}
1414
}

test/fixtures/core/uncategorised/146/options.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
},
99
"noTildeCalls": {
1010
"allPlugins": true,
11-
"excludePlugins": [
12-
"lightscript", "match", "splatComprehension",
13-
"whiteblockOnly", "tildeCallExpression"
14-
],
11+
"excludePlugins": [ "tildeCallExpression" ],
1512
"expected": "expected.json"
1613
}
1714
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"alternatives": {
3+
"all": {
4+
"throws": "Only normal whitespace (ascii-32) is allowed. (1:0)"
5+
},
36
"noLsc": {
4-
"excludePlugins": ["significantWhitespace", "bangCall", "enforceSubscriptIndentation", "lightscript", "match", "splatComprehension", "whiteblockOnly"]
7+
"excludePlugins": ["significantWhitespace"]
58
}
69
}
710
}

test/fixtures/core/uncategorised/19/options.lightscript.json

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"alternatives": {
3+
"all": {
4+
"throws": "Only '\\n' and '\\r\\n' are legal newlines in significant-whitespace mode. (1:0)"
5+
},
36
"noLsc": {
4-
"excludePlugins": ["significantWhitespace", "bangCall", "enforceSubscriptIndentation", "splatComprehension", "whiteblockOnly"]
7+
"excludePlugins": ["significantWhitespace"]
58
}
69
}
710
}

test/fixtures/core/uncategorised/321/options.lightscript.json

-3
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
{
22
"alternatives": {
3+
"all": {
4+
"throws": "Indentation required. (2:0)"
5+
},
36
"noLsc": {
4-
"excludePlugins": ["bangCall", "enforceSubscriptIndentation", "significantWhitespace", "splatComprehension", "whiteblockOnly"]
7+
"excludePlugins": ["enforceSubscriptIndentation"]
58
}
69
}
710
}

test/fixtures/core/uncategorised/542/options.lightscript.json

-3
This file was deleted.

0 commit comments

Comments
 (0)