Skip to content

Commit

Permalink
Project import generated by Copybara
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 726918278
  • Loading branch information
jimper authored and copybara-github committed Feb 14, 2025
1 parent cd297f5 commit 6f1b1d8
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
4 changes: 2 additions & 2 deletions site/js/preview-sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@

import {html, render} from 'lit-html';
import {unsafeHTML} from 'lit/directives/unsafe-html.js';
import ts from 'typescript';

import {SampleConfig} from '../../src/model/sample-config.js';
import {ScriptTarget} from '../../src/model/typescript.js';
import {createTemplate} from '../../src/template/template-factory.js';
import * as base64url from '../../src/util/base64url.js';
import {setLocale} from '../../src/util/localization-utils.js';
Expand All @@ -34,7 +34,7 @@ const config: SampleConfig = configParam
if (config) {
const template = createTemplate(config);
// Force the target output to ES2020, so it can be rendered directly.
template.jsTarget = ts.ScriptTarget.ES2020;
template.jsTarget = ScriptTarget.ES2020;

// Inject link tags.
template.stylesheets.forEach(s => {
Expand Down
8 changes: 4 additions & 4 deletions src/components/configurator/output-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import '../ui-controls/configurator-select';
import {localized} from '@lit/localize';
import {html, LitElement} from 'lit';
import {customElement, property, query} from 'lit/decorators.js';
import ts from 'typescript';

import {SampleTemplateConfig} from '../../model/sample-config.js';
import {configNames, templateConfigNames} from '../../model/settings.js';
import {ScriptTarget} from '../../model/typescript.js';
import {
ConfiguratorOption,
ConfiguratorSelect,
Expand All @@ -48,7 +48,7 @@ export class OutputSettings extends LitElement {
const target = this.targetSelect.value;
this.config.target =
target && target.length > 0
? ts.ScriptTarget[target as keyof typeof ts.ScriptTarget]
? ScriptTarget[target as keyof typeof ScriptTarget]
: undefined;

// Fire an event to let the configurator know a value has changed.
Expand All @@ -66,8 +66,8 @@ export class OutputSettings extends LitElement {
},
];

Object.entries(ts.ScriptTarget)
.filter(([k]) => isNaN(Number(k)) && !['ES3', 'JSON'].includes(k))
Object.entries(ScriptTarget)
.filter(([k]) => isNaN(Number(k)))
.forEach(([k, v]) => {
options.push({
label: `JavaScript (${k})`,
Expand Down
15 changes: 10 additions & 5 deletions src/model/sample-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

import ts from 'typescript';

import {TemplateType} from '../template/template-factory.js';
import {ScriptTarget} from './typescript.js';

/**
* Configuration options for a single custom GPT sample.
Expand Down Expand Up @@ -69,6 +67,13 @@ export interface SampleTargetingKV {
* Custom sample template options.
*/
export interface SampleTemplateConfig {
type?: TemplateType;
target?: ts.ScriptTarget;
type?: SampleTemplateType;
target?: ScriptTarget;
}

/**
* Custom sample template types.
*/
export enum SampleTemplateType {
BASIC,
}
36 changes: 36 additions & 0 deletions src/model/typescript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Copyright 2025 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.
*/

/**
* JavaScript targets supported by the Sample Builder.
*
* This enum is derived from `ts.ScriptTarget`.
*/
export enum ScriptTarget {
ES5 = 1,
ES2015 = 2,
ES2016 = 3,
ES2017 = 4,
ES2018 = 5,
ES2019 = 6,
ES2020 = 7,
ES2021 = 8,
ES2022 = 9,
ES2023 = 10,
ES2024 = 11,
ESNext = 99,
Latest = 99,
}
13 changes: 3 additions & 10 deletions src/template/template-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,20 @@
* limitations under the License.
*/

import {SampleConfig} from '../model/sample-config.js';
import {SampleConfig, SampleTemplateType} from '../model/sample-config.js';

import {BasicSample} from './basic-sample.js';
import {Template} from './template.js';

/**
* Supported custom sample templates.
*/
export enum TemplateType {
BASIC,
}

/**
* Returns an appropriate {@link Template} instance for the specified
* {@link SampleConfig}.
*/
export function createTemplate(config: SampleConfig): Template {
const templateType = config.template?.type || TemplateType.BASIC;
const templateType = config.template?.type || SampleTemplateType.BASIC;

switch (templateType) {
case TemplateType.BASIC:
case SampleTemplateType.BASIC:
return new BasicSample(config, config.template?.target);
default:
throw new Error(`Unsupported template type: ${templateType}`);
Expand Down
4 changes: 2 additions & 2 deletions src/template/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

import {css} from 'lit';
import {ProjectManifest} from 'playground-elements/shared/worker-api.js';
import ts from 'typescript';

import * as samplegen from '../codegen/gpt-sample.js';
import {SampleConfig} from '../model/sample-config.js';
import {ScriptTarget} from '../model/typescript.js';
import {formatHtml} from '../util/format-code.js';
import {tsToJs} from '../util/transpile-code.js';

Expand Down Expand Up @@ -48,7 +48,7 @@ export abstract class Template {
*/
constructor(
public sampleConfig: SampleConfig,
public jsTarget?: ts.ScriptTarget,
public jsTarget?: ScriptTarget,
) {}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/util/transpile-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import ts from 'typescript';

import {ScriptTarget} from '../model/typescript.js';

const PLACEHOLDER_COMMENT = '/* NEW_LINE_PLACEHOLDER */';

const TSC_OPTIONS: ts.CompilerOptions = {
Expand All @@ -37,7 +39,7 @@ function toggleBlankLines(source: string) {
* @param target The output JavaScript target.
* @returns
*/
export function tsToJs(source: string, target = ts.ScriptTarget.ES2020) {
export function tsToJs(source: string, target = ScriptTarget.ES2020) {
const js = ts.transpileModule(toggleBlankLines(source), {
compilerOptions: {...TSC_OPTIONS, target},
}).outputText;
Expand Down
7 changes: 3 additions & 4 deletions test/util-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@

import 'jasmine';

import ts from 'typescript';

import {SampleSlotConfig} from '../src/model/sample-config.js';
import {ScriptTarget} from '../src/model/typescript.js';
import * as base64url from '../src/util/base64url.js';
import {formatHtml, formatTypeScript} from '../src/util/format-code.js';
import {getLocale, setLocale} from '../src/util/localization-utils.js';
Expand Down Expand Up @@ -188,8 +187,8 @@ describe('Utility', () => {
const testData = transpileTestCases[testCase];

const typescript = testData.typescript.trim();
const es5 = tsToJs(typescript, ts.ScriptTarget.ES5);
const es2020 = tsToJs(typescript, ts.ScriptTarget.ES2020);
const es5 = tsToJs(typescript, ScriptTarget.ES5);
const es2020 = tsToJs(typescript, ScriptTarget.ES2020);

expect(unformat(es5)).toEqual(unformat(testData.es5));
expect(unformat(es2020)).toEqual(unformat(testData.es2020));
Expand Down
3 changes: 2 additions & 1 deletion test/web/output-settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/

import {Page} from '@playwright/test';
import {ScriptTarget} from 'typescript';

import {ScriptTarget} from '../../src/model/typescript.js';

import {expect, test} from './fixtures/configurator.js';

Expand Down

0 comments on commit 6f1b1d8

Please sign in to comment.