Skip to content

Commit db012f5

Browse files
authored
feat: add comments about generation and 'use strict' to transformed samples (#3696)
This adds support for adding a comment to generated samples specifying that they're generated, as well as adding the 'use strict' line.
1 parent bab338f commit db012f5

6 files changed

Lines changed: 93 additions & 9 deletions

File tree

packages/typeless-sample-bot/__snapshots__/index.js

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

packages/typeless-sample-bot/src/samples.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import {readFile, writeFile} from 'fs/promises';
2121
import babel from '@babel/core';
2222
import path from 'node:path';
2323
import {typescript as presetTypescript} from './preset-loader.js';
24-
import importToRequire from './import-to-require.js';
24+
import importToRequire from './transforms/import-to-require.js';
25+
import {addComments} from './transforms/add-comments.js';
2526

2627
// Converts an async iterable into an array of the same type.
2728
export async function toArray<T>(iterable: AsyncIterable<T>): Promise<T[]> {
@@ -100,9 +101,11 @@ export async function* transformSamples(
100101
for await (const s of samples) {
101102
const config = Object.assign({}, babelConfig, {filename: s.filename});
102103
const transformed = await babel.transformAsync(s.contents, config);
104+
let contents = transformed?.code || '';
105+
contents = addComments(contents);
103106
yield {
104107
filename: s.filename.replace(/\.ts$/g, '.js'),
105-
contents: transformed?.code || '',
108+
contents,
106109
};
107110
}
108111
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
const headerText = `
16+
// This is a generated sample, using the typeless sample bot. Please
17+
// look for the source TypeScript sample (.ts) for modifications.
18+
'use strict';
19+
20+
`.trim();
21+
22+
/**
23+
* Adds the header comments about the generation of the sample and the
24+
* 'use strict' line for JS. Rather than Babel, this just uses simple
25+
* text searching. The reason for that is that it's much simpler to do
26+
* this particular transform that way. Rather than traversing an AST,
27+
* this simply looks for the first blank line and inserts the block there.
28+
* If it can't find one, it puts it at the top.
29+
*/
30+
export function addComments(text: string): string {
31+
text = text.replace('\r', '');
32+
const lines = text.split('\n');
33+
const firstBlank = lines.findIndex(l => l.length === 0);
34+
if (firstBlank < 0) {
35+
// Apparently the whole file has no blank lines?
36+
return headerText + text + '\n\n';
37+
} else {
38+
lines.splice(firstBlank, 0, '\n' + headerText);
39+
}
40+
41+
return lines.join('\n');
42+
}

packages/typeless-sample-bot/src/import-to-require.ts renamed to packages/typeless-sample-bot/src/transforms/import-to-require.ts

File renamed without changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file
2+
// has no
3+
// blank lines

packages/typeless-sample-bot/test/index.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,34 @@ describe('sample transformation', () => {
6161

6262
snapshot(sOut[0].contents);
6363
});
64+
65+
it('correctly adds use strict when the sample has a licence block', async () => {
66+
const fixture = await loadFixture('listSchemas.ts');
67+
68+
const sIn = samples.fromArray([
69+
{
70+
filename: 'listSchemas.ts',
71+
contents: fixture,
72+
},
73+
]);
74+
75+
const sOut = await samples.toArray(samples.transformSamples(sIn));
76+
assert.strictEqual(sOut[0].contents.includes('use strict'), true);
77+
});
78+
79+
it('correctly adds use strict when the sample has no blank lines', async () => {
80+
const fixture = await loadFixture('noBlankLines.ts');
81+
82+
const sIn = samples.fromArray([
83+
{
84+
filename: 'listSchemas.ts',
85+
contents: fixture,
86+
},
87+
]);
88+
89+
const sOut = await samples.toArray(samples.transformSamples(sIn));
90+
assert.strictEqual(sOut[0].contents.includes('use strict'), true);
91+
});
6492
});
6593

6694
describe('command line option', () => {

0 commit comments

Comments
 (0)