diff --git a/packages/typeless-sample-bot/__snapshots__/index.js b/packages/typeless-sample-bot/__snapshots__/index.js index 9343dab51cd..f9ad75930d9 100644 --- a/packages/typeless-sample-bot/__snapshots__/index.js +++ b/packages/typeless-sample-bot/__snapshots__/index.js @@ -115,7 +115,7 @@ main(); ` exports['command line option "targets" works with multiple sample files 1'] = [ - "// Copyright 2021 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// This is a generated sample, using the typeless sample bot. Please\n// look for the source TypeScript sample (.ts) for modifications.\n'use strict';\n\n/**\n * This application demonstrates how to perform basic operations on\n * schemas with the Google Cloud Pub/Sub API.\n *\n * For more information, see the README.md under /pubsub and the documentation\n * at https://cloud.google.com/pubsub/docs.\n */\n\n// sample-metadata:\n// title: Delete a previously created schema\n// description: Deletes a schema which was previously created in the project.\n// usage: node deleteSchema.js \n\n// [START pubsub_delete_schema]\n/**\n * TODO(developer): Uncomment this variable before running the sample.\n */\n// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';\n\n// Imports the Google Cloud client library\nconst { PubSub } = require(\"@google-cloud/pubsub\");\n\n// Creates a client; cache this for further use\nconst pubSubClient = new PubSub();\n\nasync function deleteSchema(schemaNameOrId) {\n const schema = pubSubClient.schema(schemaNameOrId);\n const name = await schema.getName();\n await schema.delete();\n console.log(`Schema ${name} deleted.`);\n}\n// [END pubsub_delete_schema]\n\nfunction main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') {\n deleteSchema(schemaNameOrId).catch((err) => {\n console.error(err.message);\n process.exitCode = 1;\n });\n}\n\nmain(...process.argv.slice(2));", + "// Copyright 2021 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// This is a generated sample, using the typeless sample bot. Please\n// look for the source TypeScript sample (.ts) for modifications.\n'use strict';\n\n/**\n * This application demonstrates how to perform basic operations on\n * schemas with the Google Cloud Pub/Sub API.\n *\n * For more information, see the README.md under /pubsub and the documentation\n * at https://cloud.google.com/pubsub/docs.\n */\n\n// sample-metadata:\n// title: Delete a previously created schema\n// description: Deletes a schema which was previously created in the project.\n// usage: node deleteSchema.js \n\n// [START pubsub_delete_schema]\n/**\n * TODO(developer): Uncomment this variable before running the sample.\n */\n// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';\n\n// Imports the Google Cloud client library\nconst { PubSub } = require(\"@google-cloud/pubsub\");\n\n// Creates a client; cache this for further use\nconst pubSubClient = new PubSub();\n\nasync function deleteSchema(schemaNameOrId) {\n const schema = pubSubClient.schema(schemaNameOrId);\n const name = await schema.getName();\n await schema.delete();\n console.log(`Schema ${name} deleted.`);\n}\n// [END pubsub_delete_schema]\n\nconst someValue = undefined;\nconst coalesced = someValue || 5;\n\nfunction main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') {\n deleteSchema(schemaNameOrId).catch((err) => {\n console.error(err.message);\n process.exitCode = 1;\n });\n}\n\nmain(...process.argv.slice(2));", "// Copyright 2021 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// This is a generated sample, using the typeless sample bot. Please\n// look for the source TypeScript sample (.ts) for modifications.\n'use strict';\n\n/**\n * This application demonstrates how to perform basic operations on\n * schemas with the Google Cloud Pub/Sub API.\n *\n * For more information, see the README.md under /pubsub and the documentation\n * at https://cloud.google.com/pubsub/docs.\n */\n\n// sample-metadata:\n// title: Get a previously created schema\n// description: Gets information about a schema which was previously created in the project.\n// usage: node getSchema.js \n\n// [START pubsub_get_schema]\n/**\n * TODO(developer): Uncomment this variable before running the sample.\n */\n// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';\n\n// Imports the Google Cloud client library\nconst { PubSub } = require(\"@google-cloud/pubsub\");\n\n// Creates a client; cache this for further use\nconst pubSubClient = new PubSub();\n\nasync function getSchema(schemaNameOrId) {\n const schema = pubSubClient.schema(schemaNameOrId);\n const info = await schema.get();\n const fullName = await schema.getName();\n console.log(`Schema ${fullName} info: ${JSON.stringify(info, null, 4)}.`);\n}\n// [END pubsub_get_schema]\n\nfunction main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') {\n getSchema(schemaNameOrId).catch((err) => {\n console.error(err.message);\n process.exitCode = 1;\n });\n}\n\nmain(...process.argv.slice(2));" ] @@ -132,6 +132,6 @@ exports['sample transformation does not change JS 1'] = [ ] exports['command line option "outputpath" causes the output to move 1'] = [ - "// Copyright 2021 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// This is a generated sample, using the typeless sample bot. Please\n// look for the source TypeScript sample (.ts) for modifications.\n'use strict';\n\n/**\n * This application demonstrates how to perform basic operations on\n * schemas with the Google Cloud Pub/Sub API.\n *\n * For more information, see the README.md under /pubsub and the documentation\n * at https://cloud.google.com/pubsub/docs.\n */\n\n// sample-metadata:\n// title: Delete a previously created schema\n// description: Deletes a schema which was previously created in the project.\n// usage: node deleteSchema.js \n\n// [START pubsub_delete_schema]\n/**\n * TODO(developer): Uncomment this variable before running the sample.\n */\n// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';\n\n// Imports the Google Cloud client library\nconst { PubSub } = require(\"@google-cloud/pubsub\");\n\n// Creates a client; cache this for further use\nconst pubSubClient = new PubSub();\n\nasync function deleteSchema(schemaNameOrId) {\n const schema = pubSubClient.schema(schemaNameOrId);\n const name = await schema.getName();\n await schema.delete();\n console.log(`Schema ${name} deleted.`);\n}\n// [END pubsub_delete_schema]\n\nfunction main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') {\n deleteSchema(schemaNameOrId).catch((err) => {\n console.error(err.message);\n process.exitCode = 1;\n });\n}\n\nmain(...process.argv.slice(2));", + "// Copyright 2021 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// This is a generated sample, using the typeless sample bot. Please\n// look for the source TypeScript sample (.ts) for modifications.\n'use strict';\n\n/**\n * This application demonstrates how to perform basic operations on\n * schemas with the Google Cloud Pub/Sub API.\n *\n * For more information, see the README.md under /pubsub and the documentation\n * at https://cloud.google.com/pubsub/docs.\n */\n\n// sample-metadata:\n// title: Delete a previously created schema\n// description: Deletes a schema which was previously created in the project.\n// usage: node deleteSchema.js \n\n// [START pubsub_delete_schema]\n/**\n * TODO(developer): Uncomment this variable before running the sample.\n */\n// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';\n\n// Imports the Google Cloud client library\nconst { PubSub } = require(\"@google-cloud/pubsub\");\n\n// Creates a client; cache this for further use\nconst pubSubClient = new PubSub();\n\nasync function deleteSchema(schemaNameOrId) {\n const schema = pubSubClient.schema(schemaNameOrId);\n const name = await schema.getName();\n await schema.delete();\n console.log(`Schema ${name} deleted.`);\n}\n// [END pubsub_delete_schema]\n\nconst someValue = undefined;\nconst coalesced = someValue || 5;\n\nfunction main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') {\n deleteSchema(schemaNameOrId).catch((err) => {\n console.error(err.message);\n process.exitCode = 1;\n });\n}\n\nmain(...process.argv.slice(2));", "// Copyright 2021 Google LLC\n//\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\n// This is a generated sample, using the typeless sample bot. Please\n// look for the source TypeScript sample (.ts) for modifications.\n'use strict';\n\n/**\n * This application demonstrates how to perform basic operations on\n * schemas with the Google Cloud Pub/Sub API.\n *\n * For more information, see the README.md under /pubsub and the documentation\n * at https://cloud.google.com/pubsub/docs.\n */\n\n// sample-metadata:\n// title: Get a previously created schema\n// description: Gets information about a schema which was previously created in the project.\n// usage: node getSchema.js \n\n// [START pubsub_get_schema]\n/**\n * TODO(developer): Uncomment this variable before running the sample.\n */\n// const schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID';\n\n// Imports the Google Cloud client library\nconst { PubSub } = require(\"@google-cloud/pubsub\");\n\n// Creates a client; cache this for further use\nconst pubSubClient = new PubSub();\n\nasync function getSchema(schemaNameOrId) {\n const schema = pubSubClient.schema(schemaNameOrId);\n const info = await schema.get();\n const fullName = await schema.getName();\n console.log(`Schema ${fullName} info: ${JSON.stringify(info, null, 4)}.`);\n}\n// [END pubsub_get_schema]\n\nfunction main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') {\n getSchema(schemaNameOrId).catch((err) => {\n console.error(err.message);\n process.exitCode = 1;\n });\n}\n\nmain(...process.argv.slice(2));" ] diff --git a/packages/typeless-sample-bot/src/samples.ts b/packages/typeless-sample-bot/src/samples.ts index 06c3fc95c52..b8e731a15a7 100644 --- a/packages/typeless-sample-bot/src/samples.ts +++ b/packages/typeless-sample-bot/src/samples.ts @@ -22,6 +22,7 @@ import babel from '@babel/core'; import path from 'node:path'; import {typescript as presetTypescript} from './preset-loader.js'; import importToRequire from './transforms/import-to-require.js'; +import nullCoalescing from './transforms/null-coalescing.js'; import {addComments} from './transforms/add-comments.js'; // Converts an async iterable into an array of the same type. @@ -84,7 +85,7 @@ export async function* filterByContents( // the transform process. const babelConfig = { presets: [[presetTypescript, {}]], - plugins: [[importToRequire]], + plugins: [[importToRequire], [nullCoalescing]], parserOpts: {} as babel.ParserOptions, generatorOpts: { // Ensures that Babel keeps newlines so that comments end up diff --git a/packages/typeless-sample-bot/src/transforms/babel.ts b/packages/typeless-sample-bot/src/transforms/babel.ts new file mode 100644 index 00000000000..fd7db1438f5 --- /dev/null +++ b/packages/typeless-sample-bot/src/transforms/babel.ts @@ -0,0 +1,28 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Reference notes: +// https://lihautan.com/step-by-step-guide-for-writing-a-babel-transformation/ +// https://lihautan.com/babel-ast-explorer/ +// https://github.com/esamattis/babel-plugin-ts-optchain/blob/master/packages/babel-plugin-ts-optchain/src/plugin.ts + +import {NodePath, Visitor} from '@babel/traverse'; +import * as Babel from '@babel/types'; + +export interface VisitorPlugin { + visitor: Visitor; +} + +export type NodePathArray = NodePath[]; +export type NodePathSingle = NodePath; diff --git a/packages/typeless-sample-bot/src/transforms/import-to-require.ts b/packages/typeless-sample-bot/src/transforms/import-to-require.ts index 6d628955fb7..a6fd8434658 100644 --- a/packages/typeless-sample-bot/src/transforms/import-to-require.ts +++ b/packages/typeless-sample-bot/src/transforms/import-to-require.ts @@ -12,16 +12,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Reference notes: -// https://lihautan.com/step-by-step-guide-for-writing-a-babel-transformation/ -// https://lihautan.com/babel-ast-explorer/ -// https://github.com/esamattis/babel-plugin-ts-optchain/blob/master/packages/babel-plugin-ts-optchain/src/plugin.ts - import * as Babel from '@babel/types'; import {NodePath, Visitor} from '@babel/traverse'; - -type NodePathArray = NodePath[]; -type NodePathSingle = NodePath; +import {NodePathArray, NodePathSingle, VisitorPlugin} from './babel'; function getArray(path: NodePathSingle, subPathName: string): NodePathArray { return path.get(subPathName) as NodePathArray; @@ -102,10 +95,6 @@ function wildcardImport(path: NodePathSingle) { path.replaceWith(replacement); } -interface VisitorPlugin { - visitor: Visitor; -} - export default function importToRequire(): VisitorPlugin { return { visitor: { diff --git a/packages/typeless-sample-bot/src/transforms/null-coalescing.ts b/packages/typeless-sample-bot/src/transforms/null-coalescing.ts new file mode 100644 index 00000000000..8146f8877b9 --- /dev/null +++ b/packages/typeless-sample-bot/src/transforms/null-coalescing.ts @@ -0,0 +1,32 @@ +// Copyright 2022 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import * as Babel from '@babel/types'; +import {NodePathSingle, VisitorPlugin} from './babel'; + + +// Because null coalescing is still a proposal in many versions of Node, +// go ahead and convert it to plain ||. It should work in TypeScript. +export default function nullCoalescing(): VisitorPlugin { + return { + visitor: { + LogicalExpression(path: NodePathSingle) { + const node = path.node as Babel.LogicalExpression; + if (node.operator === '??') { + path.replaceWith(Babel.logicalExpression('||', node.left, node.right)); + } + }, + }, + }; +} diff --git a/packages/typeless-sample-bot/test/fixtures/deleteSchema.ts b/packages/typeless-sample-bot/test/fixtures/deleteSchema.ts index 8599d61578a..98c83258005 100644 --- a/packages/typeless-sample-bot/test/fixtures/deleteSchema.ts +++ b/packages/typeless-sample-bot/test/fixtures/deleteSchema.ts @@ -45,6 +45,9 @@ async function deleteSchema(schemaNameOrId: string) { } // [END pubsub_delete_schema] +const someValue: number | undefined = undefined; +const coalesced = someValue ?? 5; + function main(schemaNameOrId = 'YOUR_SCHEMA_NAME_OR_ID') { deleteSchema(schemaNameOrId).catch(err => { console.error(err.message);