From b8f7eb5175b6b9ad1ed3c2b95c93a66e00c13479 Mon Sep 17 00:00:00 2001 From: Xiaoji Chen Date: Wed, 7 Feb 2024 11:05:11 -0800 Subject: [PATCH] Remove smoosh script (#448) --- modules/dev-tools/package.json | 3 +- modules/dev-tools/scripts/ts-smoosh.js | 13 - modules/dev-tools/src/ts-smoosh/README.md | 118 ------- modules/dev-tools/src/ts-smoosh/log.js | 40 --- modules/dev-tools/src/ts-smoosh/smoosh.js | 313 ------------------ modules/dev-tools/test/index.js | 1 - .../fixtures/01-functions/a-output.ts | 16 - .../ts-smoosh/fixtures/01-functions/a.d.ts | 3 - .../test/ts-smoosh/fixtures/01-functions/a.js | 12 - .../test/ts-smoosh/fixtures/01-functions/a.ts | 16 - .../test/ts-smoosh/fixtures/02-imports/b.d.ts | 1 - .../ts-smoosh/fixtures/02-imports/c-output.ts | 8 - .../test/ts-smoosh/fixtures/02-imports/c.js | 8 - .../test/ts-smoosh/fixtures/02-imports/c.ts | 8 - .../ts-smoosh/fixtures/03-const/e-output.ts | 2 - .../test/ts-smoosh/fixtures/03-const/e.d.ts | 4 - .../test/ts-smoosh/fixtures/03-const/e.js | 3 - .../test/ts-smoosh/fixtures/03-const/e.ts | 2 - .../ts-smoosh/fixtures/04-alias/a-output.ts | 14 - .../test/ts-smoosh/fixtures/04-alias/a.d.ts | 5 - .../test/ts-smoosh/fixtures/04-alias/a.js | 13 - .../test/ts-smoosh/fixtures/04-alias/a.ts | 14 - .../ts-smoosh/fixtures/04-alias/b-output.ts | 4 - .../test/ts-smoosh/fixtures/04-alias/b.d.ts | 5 - .../test/ts-smoosh/fixtures/04-alias/b.js | 2 - .../test/ts-smoosh/fixtures/04-alias/b.ts | 4 - .../fixtures/05-arrow-funcs/f-output.ts | 10 - .../ts-smoosh/fixtures/05-arrow-funcs/f.d.ts | 7 - .../ts-smoosh/fixtures/05-arrow-funcs/f.js | 4 - .../ts-smoosh/fixtures/05-arrow-funcs/f.ts | 10 - modules/dev-tools/test/ts-smoosh/test.js | 33 -- 31 files changed, 1 insertion(+), 695 deletions(-) delete mode 100755 modules/dev-tools/scripts/ts-smoosh.js delete mode 100644 modules/dev-tools/src/ts-smoosh/README.md delete mode 100644 modules/dev-tools/src/ts-smoosh/log.js delete mode 100644 modules/dev-tools/src/ts-smoosh/smoosh.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a-output.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.d.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/02-imports/b.d.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c-output.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/03-const/e-output.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.d.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a-output.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.d.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b-output.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.d.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f-output.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.d.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.js delete mode 100644 modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.ts delete mode 100644 modules/dev-tools/test/ts-smoosh/test.js diff --git a/modules/dev-tools/package.json b/modules/dev-tools/package.json index 50073340..f50fb1ba 100644 --- a/modules/dev-tools/package.json +++ b/modules/dev-tools/package.json @@ -35,8 +35,7 @@ "ocular-lint": "./scripts/lint.js", "ocular-metrics": "./scripts/metrics.js", "ocular-publish": "./scripts/publish.js", - "ocular-test": "./scripts/test.js", - "ocular-tsify": "./scripts/ts-smoosh.js" + "ocular-test": "./scripts/test.js" }, "scripts": { "bootstrap": "yarn install-fast && ocular-bootstrap", diff --git a/modules/dev-tools/scripts/ts-smoosh.js b/modules/dev-tools/scripts/ts-smoosh.js deleted file mode 100755 index 1c48f196..00000000 --- a/modules/dev-tools/scripts/ts-smoosh.js +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env node - -import {smoosh} from '../src/ts-smoosh/smoosh.js'; - -// Given an array of `.js` files, smooshes their .d.ts declarations and -// produces a .tsx file. - -const without = (ending) => (fileName) => - fileName.endsWith(ending) ? fileName.substr(0, fileName.length - ending.length) : fileName; - -const files = process.argv.slice(2); - -files.map(without('.js')).map(smoosh); diff --git a/modules/dev-tools/src/ts-smoosh/README.md b/modules/dev-tools/src/ts-smoosh/README.md deleted file mode 100644 index 1a4402b6..00000000 --- a/modules/dev-tools/src/ts-smoosh/README.md +++ /dev/null @@ -1,118 +0,0 @@ -# TS Smoosh - -Combine type decls with related source files. - -## Use - -Given a JavaScript file (or list of files) like so: - -``` -$ node ts-smoosh/bin ./src/some-file.js -``` - -Will produce `.ts` files using nearby `.d.ts` files. - -# Supported syntax - -ts-smoosh parses `.d.ts` file, collects type declarations and runs typescript parser on `.js` file, injects types wherever a `JsDoc` type declaration is available. It supports most of the common `JSDoc` type declarations we use. - -### 1. Type imports - -If importing from matching .d.ts, will inline type declaration: - -from - -```js -/** @typedef {import('./geokey-reducers').GeoKey} GeoKey */ -``` - -to - -```js -export type GeoKey = { - id: string, - label: string, - dataId: string -}; -``` - -If importing from files other than matching .d.ts, will convert to type import. - -from - -```js -/** @typedef {import('./kepler-types').VisState} VisState */ -``` - -to - -```js -import type {VisState} from './kepler-types'; -``` - -### 2. Functions - -Functions with `JSDoc` type declaration using the `@type {typeof import('./').updater}` will be converted - -From - -```js -// js -/** @type {typeof import('./project-reducers').createMapUpdater} */ -const createMapUpdater = (state, action) => state - -// d.ts -export declare function createMapUpdater(state: State, action: Action): State; -``` - -To - -```js -export function createMapUpdater(state: State, action: Action): State { - ... -} -``` - -# Running Tests - -``` -$ node test -``` - -Test cases live in various directories in `./tests`. Tests run by comparing the output against golden master `.tsx` files. To update the masters, run `node ts-smoosh/bin ./test/0X-dir/somefile.js`. - -# Known issues - -Here are some limitations and drawbacks of the scripts - -#### 1. Missing empty line inside functions - -Typescript compiler [doesn't preserve empty lines](https://github.com/microsoft/TypeScript/issues/843). When ts-smoosh injects types into functions, it will replace the existing JSDoc comment with an empty line, but empty lines inside the functions will be missing. So are empty lines between variable declarations that does not have JSDoc types. More improvements can be made such as insert empty before return statements. - -#### 2. Mixed import and export statement. - -When type decl are injected, they might come between import statements. Some manual cleanup is required. - -#### 3. Unsupported JSDoc `@returns` `@params` tags - -Only type declarations with the `@type` tag are supported. Some functions use separate tags such as below, future work can be done to support it. - -```js -/** - * Currently not supported - * @param {KeplerDataset} dataset - * @param {GeoKey} geoKey - * @returns {Array} layers - */ -function defaultLayersForGeoKey(dataset, geoKey) {...} -``` - -# Notes - -Here are articles and docs that were helpful when writing this script. - -- [Using the Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) -- [Blog post about using TS's parser directly](https://medium.com/allenhwkim/how-to-parse-typescript-from-source-643387971f4e) -- [TypeScript compiler APIs revisited](https://blog.scottlogic.com/2017/05/02/typescript-compiler-api-revisited.html) -- [Dealing with Comment Nodes in the TypeScript Compiler API](https://quramy.medium.com/manipulate-comments-with-typescript-api-73d5f1d43d7f) -- [TypeScript API Playground on Glitch](https://typescript-api-playground.glitch.me/#example=Transformation%203) diff --git a/modules/dev-tools/src/ts-smoosh/log.js b/modules/dev-tools/src/ts-smoosh/log.js deleted file mode 100644 index 139c49d0..00000000 --- a/modules/dev-tools/src/ts-smoosh/log.js +++ /dev/null @@ -1,40 +0,0 @@ -// TODO: Make use of probe.gl instead -const Colors = { - Reset: '\x1b[0m', - Bright: '\x1b[1m', - Dim: '\x1b[2m', - Underscore: '\x1b[4m', - Blink: '\x1b[5m', - Reverse: '\x1b[7m', - Hidden: '\x1b[8m', - - FgBlack: '\x1b[30m', - FgRed: '\x1b[31m', - FgGreen: '\x1b[32m', - FgYellow: '\x1b[33m', - FgBlue: '\x1b[34m', - FgMagenta: '\x1b[35m', - FgCyan: '\x1b[36m', - FgWhite: '\x1b[37m', - - BgBlack: '\x1b[40m', - BgRed: '\x1b[41m', - BgGreen: '\x1b[42m', - BgYellow: '\x1b[43m', - BgBlue: '\x1b[44m', - BgMagenta: '\x1b[45m', - BgCyan: '\x1b[46m', - BgWhite: '\x1b[47m' -}; - -function log(color) { - return function logWithColor(msg) { - console.log(`${color}%s${Colors.Reset}`, msg); - }; -} - -export const logSuccess = log(Colors.FgGreen); -export const logOk = log(Colors.FgCyan); -export const logError = log(Colors.FgRed); -export const logProgress = log(Colors.FgBlue); -export const logStep = log(Colors.FgMagenta); diff --git a/modules/dev-tools/src/ts-smoosh/smoosh.js b/modules/dev-tools/src/ts-smoosh/smoosh.js deleted file mode 100644 index 05647d43..00000000 --- a/modules/dev-tools/src/ts-smoosh/smoosh.js +++ /dev/null @@ -1,313 +0,0 @@ -import ts from 'typescript'; -import fs from 'fs'; -import path from 'path'; -import * as log from './log.js'; - -import prettier from 'prettier'; -// @ts-ignore -import {getPrettierConfig} from '../configuration/get-prettier-config.cjs'; - -const suffix = 'ts'; -const prettierConfig = getPrettierConfig(); - -/** - * Writes the result of smooshing to a file - */ -export function smoosh(base, options = {}) { - const smooshedSrc = returnSmooshed(base, options); - - const outputFile = `./${base}.${suffix}`; - - fs.writeFileSync(outputFile, smooshedSrc, 'utf8'); - log.logSuccess(`Smooshed ${outputFile}`); -} - -const declDoesntExist = {typeAliases: [], declarations: [], imports: []}; - -export function returnSmooshed(base, options = {}) { - const dtsFile = `${base}.d.ts`; - // TODO(btford): log a warning here? - const decls = fs.existsSync(dtsFile) ? parseDts(dtsFile) : declDoesntExist; - - const jsFile = `${base}.js`; - const enrichedJsNode = enrichJs(jsFile, decls); - - const outputFile = `${base}.${suffix}`; - - const resultFile = ts.createSourceFile( - outputFile, - '', - ts.ScriptTarget.Latest, - false, - ts.ScriptKind.TSX - ); - - const printer = ts.createPrinter({newLine: ts.NewLineKind.LineFeed}); - - const smooshedSrc = printer.printNode(ts.EmitHint.Unspecified, enrichedJsNode, resultFile); - - const cleanedSrc = replaceExportDeclareType(withoutJSDoc(smooshedSrc)); - // @ts-ignore - return options.prettier - ? prettier.format(cleanedSrc, {...prettierConfig, filepath: jsFile}) - : cleanedSrc; -} - -function parseDts(dtsFile) { - const parsed = ts.createSourceFile( - dtsFile, - fs.readFileSync(dtsFile, 'utf8'), - ts.ScriptTarget.Latest - ); - - // these are going on top - const typeAliases = []; - const declarations = {}; - const imports = []; - - const aggregateDecl = (statement) => { - const kind = ts.SyntaxKind[statement.kind]; - - if (kind === 'TypeAliasDeclaration') { - declarations[getIdentifierName(statement)] = statement.type; - // console.log(statement && statement.name && statement.name.escapedText) - typeAliases.push(statement); - return; - } - if (kind === 'InterfaceDeclaration') { - declarations[getIdentifierName(statement)] = statement; - typeAliases.push(statement); - return; - } - if (kind === 'ImportDeclaration') { - imports.push(statement); - return; - } - - if (kind === 'FirstStatement') { - // eslint-disable-next-line consistent-return - return statement.declarationList.declarations.map(aggregateDecl); - } - - if (!kind.endsWith('Declaration')) { - const message = `Unexpected statement kind "${kind}" in type definition file "${dtsFile}"`; - // eslint-disable-next-line consistent-return - return console.warn(message); - } - declarations[getIdentifierName(statement)] = statement; - }; - - parsed.statements.forEach(aggregateDecl); - - return {typeAliases, declarations, imports}; -} - -function enrichJs(jsFile, dts) { - const parsed = ts.createSourceFile( - jsFile, - fs.readFileSync(jsFile, 'utf8'), - ts.ScriptTarget.Latest - ); - - const findSource = (node) => { - let typeSource = null; - - // First, search for a jsdoc tag with the type, like: - // @type {typeof import('./b').Noop} - if (node.jsDoc) { - const typeTag = (node.jsDoc[0].tags || []).find((tag) => tag.tagName.escapedText === 'type'); - if (typeTag) { - const fileName = typeTag.typeExpression.type.argument.literal.text; - const identifier = typeTag.typeExpression.type.qualifier.escapedText; - const dir = path.dirname(jsFile); - const fullPath = path.resolve(dir, `${fileName}.d.ts`); - const importedDts = parseDts(fullPath); - const importedType = importedDts.declarations[identifier]; - if (!importedType) { - console.warn( - `Could not find ${identifier} in ${fullPath} while trying to smoosh ${jsFile}` - ); - return node; - } - typeSource = importedType; - } - } - - // Second, use the d.ts file with the same name as this file. - if (!typeSource) { - typeSource = dts.declarations[getIdentifierName(node)]; - } - - return typeSource; - }; - - const transformer = (context) => { - let importsToFind = []; - return (rootNode) => { - function visit(node) { - const kind = ts.SyntaxKind[node.kind]; - importsToFind = importsToFind.concat( - (node.jsDoc || []) - .flatMap((d) => (d.tags || []).filter((tag) => tag.tagName.escapedText === 'typedef')) - .flatMap((typeTag) => { - const fileName = typeTag.typeExpression.type.argument.literal.text; - const identifier = typeTag.typeExpression.type.qualifier.escapedText; - - // skip adding imports for js/d.ts pairs. We automatically merge imports - // for that below. - if ( - path.resolve(jsFile) === - path.resolve(`${path.join(path.dirname(jsFile), fileName)}.js'`) - ) { - return []; - } - - return [{fileName, identifier}]; - }) - ); - - // - if (kind.endsWith('Declaration')) { - if (kind === 'FunctionDeclaration') { - const typeSource = findSource(node); - delete node.jsDoc; - if (typeSource) { - return ts.factory.updateFunctionDeclaration( - node, - node.decorators, - node.modifiers, - node.asteriskToken, - node.name, - typeSource.typeParameters, - node.parameters.map((p, i) => - ts.factory.updateParameterDeclaration( - p, - p.decorators, - p.modifiers, - p.dotDotDotToken, - p.name, - p.questionToken, - cloneType(typeSource.parameters[i]), - p.initializer - ) - ), - cloneType(typeSource), - node.body - ); - } - return node; - } else if (kind === 'VariableDeclaration') { - const typeSource = findSource(node); - if (typeSource) { - // Account for the case where the d.ts file is a fn decl, - // but this file is a variable decl - // eslint-disable-next-line max-depth - if ( - ts.SyntaxKind[typeSource.kind] === 'FunctionDeclaration' && - ts.SyntaxKind[node.initializer.kind] === 'ArrowFunction' - ) { - return ts.factory.updateVariableDeclaration( - node, - node.name, - node.exclamationToken, - node.type, - ts.factory.updateArrowFunction( - node.initializer, - node.initializer.modifiers, - typeSource.typeParameters, - node.initializer.parameters.map((p, i) => - ts.factory.updateParameterDeclaration( - p, - p.decorators, - p.modifiers, - p.dotDotDotToken, - p.name, - p.questionToken, - cloneType(typeSource.parameters[i]), - p.initializer - ) - ), - cloneType(typeSource), - node.initializer.equalsGreaterThanToken, - node.initializer.body - ) - ); - } - - return ts.factory.updateVariableDeclaration( - node, - node.name, - node.exclamationToken, - typeSource.type, - node.initializer - ); - } - return node; - } - - return node; - } - - return ts.visitEachChild(node, visit, context); - } - const newRoot = ts.visitNode(rootNode, visit); - - // TODO: should we dedupe/combine these imports? - - const importsForTypes = importsToFind.map(({identifier, fileName}) => - ts.factory.createImportDeclaration( - undefined, - undefined, - ts.factory.createImportClause( - true, - undefined, - ts.factory.createNamedImports([ - ts.factory.createImportSpecifier(undefined, ts.factory.createIdentifier(identifier)) - ]) - ), - ts.factory.createStringLiteral(fileName) - ) - ); - - return ts.factory.updateSourceFile(newRoot, [ - ...dts.imports, - ...importsForTypes, - ...dts.typeAliases, - ...newRoot.statements - ]); - }; - }; - - return ts.transform(parsed, [transformer]).transformed[0]; -} - -function cloneType(node) { - if (!node.type) { - console.log(node); - } - return node.type && node.type.typeName - ? // If the node has a name, we clone it. Referencing the type nodes from the - // d.ts file directly seems to break code comments. - ts.factory.createTypeReferenceNode(node.type.typeName.escapedText) - : // this should be a built-in type (like `string`, `number`, etc.) - node.type; -} - -function getIdentifierName(node) { - return node.name.escapedText; -} - -// Removing JSDoc comments post-hoc with a regex is less-than-ideal, but it does not -// appear that there is a way to update nodes returned from the typescript compiler -// with respect to JSDocs. I see APIs for creating new nodes, but no way to attach em to arbitrary fields. -const RE = /^[ ]*\/?\*?\*?[ ]*\@(type|typedef)(.*)$/gm; -function withoutJSDoc(text) { - return text.replace(RE, ''); -} - -// HACK export declare type is not allowed in ts prettier -function replaceExportDeclareType(text) { - const reT = /^export declare type /gm; - const reI = /^export declare interface /gm; - return text.replace(reT, 'export type ').replace(reI, 'export interface '); -} diff --git a/modules/dev-tools/test/index.js b/modules/dev-tools/test/index.js index 5d9137cc..8b5d352c 100644 --- a/modules/dev-tools/test/index.js +++ b/modules/dev-tools/test/index.js @@ -1,4 +1,3 @@ import './lib/utils.spec'; // import './lib/configuration.spec'; // TODO - issues after upgrade -// import './ts-smoosh/test'; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a-output.ts b/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a-output.ts deleted file mode 100644 index a553770c..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a-output.ts +++ /dev/null @@ -1,16 +0,0 @@ -/** - * A function with a JSDoc type import that matches its name - - */ -export function foo( - bar: string, - baz: number -): { - boo: number; -} { - return {boo: baz}; -} -// A function with no type import -export function typeParams(a: A, b: B): B { - return a + b; -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.d.ts b/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.d.ts deleted file mode 100644 index 5e7d3fd8..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.d.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function foo(bar: string, baz: number): {boo: number}; - -export function typeParams(a: A, b: B): B; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.js b/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.js deleted file mode 100644 index 884d3a1c..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.js +++ /dev/null @@ -1,12 +0,0 @@ -/** - * A function with a JSDoc type import that matches its name - * @type {typeof import('./a').foo} - */ -export function foo(bar, baz) { - return {boo: baz}; -} - -// A function with no type import -export function typeParams(a, b) { - return a + b; -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.ts b/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.ts deleted file mode 100644 index a553770c..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/01-functions/a.ts +++ /dev/null @@ -1,16 +0,0 @@ -/** - * A function with a JSDoc type import that matches its name - - */ -export function foo( - bar: string, - baz: number -): { - boo: number; -} { - return {boo: baz}; -} -// A function with no type import -export function typeParams(a: A, b: B): B { - return a + b; -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/b.d.ts b/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/b.d.ts deleted file mode 100644 index a0ca23b3..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/b.d.ts +++ /dev/null @@ -1 +0,0 @@ -export type MyFn = (a: number, b: number) => number; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c-output.ts b/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c-output.ts deleted file mode 100644 index b963c175..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c-output.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * A function with a JSDoc type import that is different from its name - - */ -function c(a: number, b: number): number { - console.log('haha hi'); - return a + b; -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.js b/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.js deleted file mode 100644 index f6c56256..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.js +++ /dev/null @@ -1,8 +0,0 @@ -/** - * A function with a JSDoc type import that is different from its name - * @type {typeof import('./b').MyFn} - */ -function c(a, b) { - console.log('haha hi'); - return a + b; -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.ts b/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.ts deleted file mode 100644 index b963c175..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/02-imports/c.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * A function with a JSDoc type import that is different from its name - - */ -function c(a: number, b: number): number { - console.log('haha hi'); - return a + b; -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e-output.ts b/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e-output.ts deleted file mode 100644 index 8886106e..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e-output.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const x: number = 3; -export const add: (a: number, b: number) => number = (a, b) => a + b; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.d.ts b/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.d.ts deleted file mode 100644 index f46e5335..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.d.ts +++ /dev/null @@ -1,4 +0,0 @@ -// hi -export const x: number; - -export const add: (a: number, b: number) => number; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.js b/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.js deleted file mode 100644 index d0ab5ccc..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.js +++ /dev/null @@ -1,3 +0,0 @@ -export const x = 3; - -export const add = (a, b) => a + b; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.ts b/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.ts deleted file mode 100644 index 8886106e..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/03-const/e.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const x: number = 3; -export const add: (a: number, b: number) => number = (a, b) => a + b; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a-output.ts b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a-output.ts deleted file mode 100644 index 49df05ea..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a-output.ts +++ /dev/null @@ -1,14 +0,0 @@ -import {Taro} from './b'; // TYPE IMPORTS -import type {Foo} from './a'; -import type {Blue} from './b'; -export type Foo = Taro[]; -import {x} from './z'; -// TYPE IMPORTS - -/** - * set join display property - - */ -export function convert(a: Taro): string { - return String(a); -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.d.ts b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.d.ts deleted file mode 100644 index 22b3c73e..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {Taro} from './b'; - -export type Foo = Taro[]; - -export function convert(t: Taro): string; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.js b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.js deleted file mode 100644 index 24b524da..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.js +++ /dev/null @@ -1,13 +0,0 @@ -import {x} from './z'; - -// TYPE IMPORTS -/** @typedef {import('./a').Foo} Foo */ -/** @typedef {import('./b').Blue} Blue */ - -/** - * set join display property - * @type {typeof import('./a').convert} - */ -export function convert(a) { - return String(a); -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.ts b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.ts deleted file mode 100644 index 49df05ea..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/a.ts +++ /dev/null @@ -1,14 +0,0 @@ -import {Taro} from './b'; // TYPE IMPORTS -import type {Foo} from './a'; -import type {Blue} from './b'; -export type Foo = Taro[]; -import {x} from './z'; -// TYPE IMPORTS - -/** - * set join display property - - */ -export function convert(a: Taro): string { - return String(a); -} diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b-output.ts b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b-output.ts deleted file mode 100644 index b3671fa3..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b-output.ts +++ /dev/null @@ -1,4 +0,0 @@ -export type Taro = string; -export type Blue = string; -export const taro: Taro = 'chubby'; -export const blue: Blue = 'serious'; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.d.ts b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.d.ts deleted file mode 100644 index 3f79bf79..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -export type Taro = string; -export type Blue = string; - -export const taro: Taro; -export const blue: Blue; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.js b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.js deleted file mode 100644 index ec211d10..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.js +++ /dev/null @@ -1,2 +0,0 @@ -export const taro = 'chubby'; -export const blue = 'serious'; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.ts b/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.ts deleted file mode 100644 index b3671fa3..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/04-alias/b.ts +++ /dev/null @@ -1,4 +0,0 @@ -export type Taro = string; -export type Blue = string; -export const taro: Taro = 'chubby'; -export const blue: Blue = 'serious'; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f-output.ts b/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f-output.ts deleted file mode 100644 index 91d70f5b..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f-output.ts +++ /dev/null @@ -1,10 +0,0 @@ -export type State = { - a: string; -}; -export type Action = { - payload: string; -}; - -export const createMapUpdater = (state: State, action: Action): State => { - return state; -}; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.d.ts b/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.d.ts deleted file mode 100644 index b7cb7419..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.d.ts +++ /dev/null @@ -1,7 +0,0 @@ -export type State = { - a: string; -}; -export type Action = { - payload: string; -}; -export declare function createMapUpdater(state: State, action: Action): State; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.js b/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.js deleted file mode 100644 index 3fed7233..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.js +++ /dev/null @@ -1,4 +0,0 @@ -/** @type {typeof import('./f').createMapUpdater} */ -export const createMapUpdater = (state, action) => { - return state; -}; diff --git a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.ts b/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.ts deleted file mode 100644 index 91d70f5b..00000000 --- a/modules/dev-tools/test/ts-smoosh/fixtures/05-arrow-funcs/f.ts +++ /dev/null @@ -1,10 +0,0 @@ -export type State = { - a: string; -}; -export type Action = { - payload: string; -}; - -export const createMapUpdater = (state: State, action: Action): State => { - return state; -}; diff --git a/modules/dev-tools/test/ts-smoosh/test.js b/modules/dev-tools/test/ts-smoosh/test.js deleted file mode 100644 index d9985264..00000000 --- a/modules/dev-tools/test/ts-smoosh/test.js +++ /dev/null @@ -1,33 +0,0 @@ -import fs from 'fs'; -import assert from 'assert'; -import path from 'path'; - -import * as log from '../../src/ts-smoosh/log.js'; -import {returnSmooshed} from '../../src/ts-smoosh/smoosh.js'; - -// eslint-disable-next-line no-undef -const args = process.argv.slice(3); -const dirs = - args.length > 0 - ? args - : fs.readdirSync(`${__dirname}/fixtures`).map((f) => `${__dirname}/fixtures/${f}`); - -dirs.forEach((dir) => { - const files = fs.readdirSync(dir); - - files - .filter((file) => file.endsWith('.js')) - .map((file) => file.substr(0, file.length - 3)) - .forEach((file) => { - const fullFile = path.resolve(dir, file); - log.logProgress(`Testing ${fullFile}.js`); - const smooshed = returnSmooshed(fullFile, {prettier: true}); - // - fs.writeFileSync(`${fullFile}-output.ts`, smooshed, 'utf8'); - - const target = fs.readFileSync(`${fullFile}.ts`, 'utf8'); - assert.equal(smooshed, target); - }); -}); - -log.logSuccess('Done!');