From 5018a8e1508a57d97bb78be0f0da215aac849e9f Mon Sep 17 00:00:00 2001 From: Billy Kwok Date: Thu, 17 Oct 2019 02:44:50 +0800 Subject: [PATCH] add types for babel-plugin-macros (#39151) * add types for babel-plugin-macros * rename babel-plugin-macros-test.ts to babel-plugin-macros-tests.ts * add paths in tsconfig.json * remove patch version from version string * remove unnecessary generics * fix import module name and minimum TypeScript version * update minimum TypeScript version to 2.9 --- .../babel-plugin-macros-tests.ts | 14 +++++++ types/babel-plugin-macros/index.d.ts | 36 ++++++++++++++++++ types/babel-plugin-macros/tsconfig.json | 37 +++++++++++++++++++ types/babel-plugin-macros/tslint.json | 1 + 4 files changed, 88 insertions(+) create mode 100644 types/babel-plugin-macros/babel-plugin-macros-tests.ts create mode 100644 types/babel-plugin-macros/index.d.ts create mode 100644 types/babel-plugin-macros/tsconfig.json create mode 100644 types/babel-plugin-macros/tslint.json diff --git a/types/babel-plugin-macros/babel-plugin-macros-tests.ts b/types/babel-plugin-macros/babel-plugin-macros-tests.ts new file mode 100644 index 0000000000..aa7a3858b9 --- /dev/null +++ b/types/babel-plugin-macros/babel-plugin-macros-tests.ts @@ -0,0 +1,14 @@ +import { createMacro, MacroError } from 'babel-plugin-macros'; + +const macro = createMacro( + ({ references, state, babel }) => { + references.forEach(() => {}); + references.default.forEach(() => {}); + state.abc = 123; + babel.parse("console.log('Hello world!')"); + throw new MacroError('testing'); + }, + { configName: 'test' }, +); + +macro() === 'Hello world!'; diff --git a/types/babel-plugin-macros/index.d.ts b/types/babel-plugin-macros/index.d.ts new file mode 100644 index 0000000000..ef48280994 --- /dev/null +++ b/types/babel-plugin-macros/index.d.ts @@ -0,0 +1,36 @@ +// Type definitions for babel-plugin-macros 2.6 +// Project: https://github.com/kentcdodds/babel-plugin-macros +// Definitions by: Billy Kwok +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.9 +import * as Babel from '@babel/core'; + +type References = Babel.NodePath[]; + +interface Options { + configName?: string; +} + +interface MacroParams { + references: { default: References } & References; + state: any; + babel: typeof Babel; +} + +type MacroHandler = (params: MacroParams) => void; + +declare function babel_plugin_macros( + babel: typeof Babel, + ref: { + require: (path: string) => any; + resolvePath: (src: string, baseDir: string) => any; + }, +): Babel.PluginObj; + +declare namespace babel_plugin_macros { + class MacroError extends Error {} + + function createMacro(handler: MacroHandler, options?: Options): any; +} + +export = babel_plugin_macros; diff --git a/types/babel-plugin-macros/tsconfig.json b/types/babel-plugin-macros/tsconfig.json new file mode 100644 index 0000000000..10c6998332 --- /dev/null +++ b/types/babel-plugin-macros/tsconfig.json @@ -0,0 +1,37 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true, + "paths": { + "@babel/core": [ + "babel__core" + ], + "@babel/generator": [ + "babel__generator" + ], + "@babel/template": [ + "babel__template" + ], + "@babel/traverse": [ + "babel__traverse" + ] + } + }, + "files": [ + "index.d.ts", + "babel-plugin-macros-tests.ts" + ] +} diff --git a/types/babel-plugin-macros/tslint.json b/types/babel-plugin-macros/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/babel-plugin-macros/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }