-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: goldstein: add support of safe assignment operator (arthurfi…
- Loading branch information
1 parent
058c99c
commit b2b030b
Showing
11 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
packages/keyword-missing-initializer/fixture/no-safe-assignment.gs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const [error, response] ?= await fetch("https://arthur.place"); | ||
const [error1, response1] ?= fn("https://arthur.place"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
import {createTest} from '../test/index.js'; | ||
import keywordBrokenString from './index.js'; | ||
import operatorSafeAssignment from '../operator-safe-assignment/index.js'; | ||
|
||
const test = createTest(import.meta.url, keywordBrokenString); | ||
const testSafeAssignment = createTest(import.meta.url, ...[keywordBrokenString, operatorSafeAssignment]); | ||
|
||
test('goldstein: missing-initializer', (t) => { | ||
t.compile('missing-initializer'); | ||
t.end(); | ||
}); | ||
|
||
testSafeAssignment('goldstein: missing-initializer: safeAssignment', (t) => { | ||
t.compile('missing-initializer'); | ||
t.end(); | ||
}); | ||
|
||
test('goldstein: missing-initializer: no safeAssignment', (t) => { | ||
t.raise('no-safe-assignment', `Enable 'operator-safe-assignment' to have ability to use '?=' (1:26)`); | ||
t.end(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
const a ?= 5; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const [error, response] ?= await fetch("https://arthur.place"); | ||
const [error1, response1] ?= fn("https://arthur.place"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const [error, response] = await tryToCatch(fetch, 'https://arthur.place'); | ||
const [error1, response1] = tryCatch(fn, 'https://arthur.place'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import {types} from 'putout'; | ||
import {setGoldsteinTry} from '../types/try.js'; | ||
import {MissingInitializer} from '../keyword-missing-initializer/index.js'; | ||
import {tokTypes as tt} from '../operator/index.js'; | ||
|
||
const {assign} = Object; | ||
|
||
const { | ||
isCallExpression, | ||
isAwaitExpression, | ||
} = types; | ||
|
||
export default function operatorSafeAssignment(Parser) { | ||
return class extends Parser { | ||
parseVar(node, isFor, kind, allowMissingInitializer) { | ||
node.declarations = []; | ||
node.kind = kind; | ||
for (;;) { | ||
const decl = this.startNode(); | ||
this.parseVarId(decl, kind); | ||
|
||
if (this.eat(tt.question) && this.eat(tt.eq)) | ||
decl.init = this.parseSafeAssignment(); /* c8 ignore start */ | ||
else if (this.eat(tt.eq)) | ||
decl.init = this.parseMaybeAssign(isFor); | ||
else if (!allowMissingInitializer && kind === 'const' && !(this.type === tt._in || this.options.ecmaVersion >= 6 && this.isContextual('of'))) | ||
decl.init = MissingInitializer.missInitializer.call(this, isFor); | ||
else if (!allowMissingInitializer && decl.id.type !== 'Identifier' && !(isFor && (this.type === tt._in || this.isContextual('of')))) | ||
this.raise(this.lastTokEnd, 'Complex binding patterns require an initialization value'); | ||
else | ||
decl.init = null; | ||
|
||
/* c8 ignore end */ | ||
node.declarations.push(this.finishNode(decl, 'VariableDeclarator')); | ||
|
||
if (!this.eat(tt.comma)) | ||
break; | ||
} | ||
|
||
return node; | ||
} | ||
|
||
parseSafeAssignment() { | ||
const node = super.startNode(); | ||
const expression = this.parseExpression(); | ||
|
||
if (isCallExpression(expression)) | ||
assign(node, { | ||
type: 'CallExpression', | ||
callee: { | ||
type: 'Identifier', | ||
name: 'tryCatch', | ||
}, | ||
arguments: [ | ||
expression.callee, | ||
...expression.arguments, | ||
], | ||
}); | ||
else if (isAwaitExpression(expression)) | ||
assign(node, { | ||
type: 'AwaitExpression', | ||
argument: { | ||
type: 'CallExpression', | ||
callee: { | ||
type: 'Identifier', | ||
name: 'tryToCatch', | ||
}, | ||
arguments: [ | ||
expression.argument.callee, | ||
...expression.argument.arguments, | ||
], | ||
}, | ||
}); | ||
else | ||
this.raise(this.start, `After '?=' only 'await' and 'function call' can come`); | ||
|
||
setGoldsteinTry(node); | ||
return super.finishNode(node, node.type); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import {createTest} from '../test/index.js'; | ||
import operatorSafeAssignment from './index.js'; | ||
import keywordMissingInitializer from '../keyword-missing-initializer/index.js'; | ||
|
||
const test = createTest(import.meta.url, ...[ | ||
operatorSafeAssignment, | ||
keywordMissingInitializer, | ||
]); | ||
|
||
test('goldstein: operator: safe-assignment', (t) => { | ||
t.compile('safe-assignment'); | ||
t.end(); | ||
}); | ||
|
||
test('goldstein: operator: safe-assignment: not-supported', (t) => { | ||
t.raise('not-supported', `After '?=' only 'await' and 'function call' can come (1:12)`); | ||
t.end(); | ||
}); |