File tree 6 files changed +70
-16
lines changed
packages/typeless-sample-bot
6 files changed +70
-16
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import babel from '@babel/core';
22
22
import path from 'node:path' ;
23
23
import { typescript as presetTypescript } from './preset-loader.js' ;
24
24
import importToRequire from './transforms/import-to-require.js' ;
25
+ import nullCoalescing from './transforms/null-coalescing.js' ;
25
26
import { addComments } from './transforms/add-comments.js' ;
26
27
27
28
// Converts an async iterable into an array of the same type.
@@ -84,7 +85,7 @@ export async function* filterByContents(
84
85
// the transform process.
85
86
const babelConfig = {
86
87
presets : [ [ presetTypescript , { } ] ] ,
87
- plugins : [ [ importToRequire ] ] ,
88
+ plugins : [ [ importToRequire ] , [ nullCoalescing ] ] ,
88
89
parserOpts : { } as babel . ParserOptions ,
89
90
generatorOpts : {
90
91
// Ensures that Babel keeps newlines so that comments end up
Original file line number Diff line number Diff line change
1
+ // Copyright 2022 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ // Reference notes:
16
+ // https://lihautan.com/step-by-step-guide-for-writing-a-babel-transformation/
17
+ // https://lihautan.com/babel-ast-explorer/
18
+ // https://github.com/esamattis/babel-plugin-ts-optchain/blob/master/packages/babel-plugin-ts-optchain/src/plugin.ts
19
+
20
+ import { NodePath , Visitor } from '@babel/traverse' ;
21
+ import * as Babel from '@babel/types' ;
22
+
23
+ export interface VisitorPlugin {
24
+ visitor : Visitor < unknown > ;
25
+ }
26
+
27
+ export type NodePathArray = NodePath < Babel . Node > [ ] ;
28
+ export type NodePathSingle = NodePath < Babel . Node > ;
Original file line number Diff line number Diff line change 12
12
// See the License for the specific language governing permissions and
13
13
// limitations under the License.
14
14
15
- // Reference notes:
16
- // https://lihautan.com/step-by-step-guide-for-writing-a-babel-transformation/
17
- // https://lihautan.com/babel-ast-explorer/
18
- // https://github.com/esamattis/babel-plugin-ts-optchain/blob/master/packages/babel-plugin-ts-optchain/src/plugin.ts
19
-
20
15
import * as Babel from '@babel/types' ;
21
- import { NodePath , Visitor } from '@babel/traverse' ;
22
-
23
- type NodePathArray = NodePath < Babel . Node > [ ] ;
24
- type NodePathSingle = NodePath < Babel . Node > ;
16
+ import { NodePath } from '@babel/traverse' ;
17
+ import { NodePathArray , NodePathSingle , VisitorPlugin } from './babel' ;
25
18
26
19
function getArray ( path : NodePathSingle , subPathName : string ) : NodePathArray {
27
20
return path . get ( subPathName ) as NodePathArray ;
@@ -102,10 +95,6 @@ function wildcardImport(path: NodePathSingle) {
102
95
path . replaceWith ( replacement ) ;
103
96
}
104
97
105
- interface VisitorPlugin {
106
- visitor : Visitor < unknown > ;
107
- }
108
-
109
98
export default function importToRequire ( ) : VisitorPlugin {
110
99
return {
111
100
visitor : {
Original file line number Diff line number Diff line change
1
+ // Copyright 2022 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ import * as Babel from '@babel/types' ;
16
+ import { NodePathSingle , VisitorPlugin } from './babel' ;
17
+
18
+ // Because null coalescing is still a proposal in many versions of Node,
19
+ // go ahead and convert it to plain ||. It should work in TypeScript.
20
+ export default function nullCoalescing ( ) : VisitorPlugin {
21
+ return {
22
+ visitor : {
23
+ LogicalExpression ( path : NodePathSingle ) {
24
+ const node = path . node as Babel . LogicalExpression ;
25
+ if ( node . operator === '??' ) {
26
+ path . replaceWith (
27
+ Babel . logicalExpression ( '||' , node . left , node . right )
28
+ ) ;
29
+ }
30
+ } ,
31
+ } ,
32
+ } ;
33
+ }
Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ async function deleteSchema(schemaNameOrId: string) {
45
45
}
46
46
// [END pubsub_delete_schema]
47
47
48
+ const someValue : number | undefined = undefined ;
49
+ const coalesced = someValue ?? 5 ;
50
+
48
51
function main ( schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID' ) {
49
52
deleteSchema ( schemaNameOrId ) . catch ( err => {
50
53
console . error ( err . message ) ;
You can’t perform that action at this time.
0 commit comments