From 063c09f5361fb081e09221d955c565f4c60fd0f1 Mon Sep 17 00:00:00 2001 From: Adam Thompson-Sharpe Date: Fri, 13 May 2022 17:09:07 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#60326=20[postcss-m?= =?UTF-8?q?ixins]=20Add=20types=20for=20postcss-mixins=20by=20@MysteryBlok?= =?UTF-8?q?Hed?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add types for postcss-mixins * Update return type for MixinFn * Fix MixinObj type --- types/postcss-mixins/index.d.ts | 47 +++++++++++++++++++ types/postcss-mixins/package.json | 6 +++ types/postcss-mixins/postcss-mixins-tests.ts | 49 ++++++++++++++++++++ types/postcss-mixins/tsconfig.json | 16 +++++++ types/postcss-mixins/tslint.json | 1 + 5 files changed, 119 insertions(+) create mode 100644 types/postcss-mixins/index.d.ts create mode 100644 types/postcss-mixins/package.json create mode 100644 types/postcss-mixins/postcss-mixins-tests.ts create mode 100644 types/postcss-mixins/tsconfig.json create mode 100644 types/postcss-mixins/tslint.json diff --git a/types/postcss-mixins/index.d.ts b/types/postcss-mixins/index.d.ts new file mode 100644 index 00000000000000..8b7121be943864 --- /dev/null +++ b/types/postcss-mixins/index.d.ts @@ -0,0 +1,47 @@ +// Type definitions for postcss-mixins 9.0 +// Project: https://github.com/postcss/postcss-mixins +// Definitions by: Adam Thompson-Sharpe +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { Container, PluginCreator } from 'postcss'; + +declare namespace postcssMixins { + interface Options { + /** + * Define mixins in code instead of with CSS. + * + * For functions: the first parameter is the mixin rule, + * and all others are parameters passed to the mixin. + */ + mixins?: Record | undefined; + /** + * Autoload all mixins from one or more dirs. + * Mixin name will be taken from file name. + */ + mixinsDir?: string | string[] | undefined; + /** + * Similar to mixinsDir, except you can provide fast-glob syntax + * to target or not to target specific files. + */ + mixinsFiles?: string | string[] | undefined; + /** + * Remove unknown mixins instead of throwing an error. + * Off by default. + */ + silent?: boolean | undefined; + } + + /** + * A mixin, either a function or an object + */ + type Mixin = MixinFn | MixinObj; + type MixinFn = (mixin: Container, ...args: string[]) => MixinObj | void; + // The Exclude here is meant to make sure that you can't assign invalid functions to MixinObj, + // which is possible with Record + // tslint:disable-next-line:ban-types + type MixinObj = Record>; +} + +declare var postcssMixins: PluginCreator; + +export = postcssMixins; diff --git a/types/postcss-mixins/package.json b/types/postcss-mixins/package.json new file mode 100644 index 00000000000000..f310f083276d15 --- /dev/null +++ b/types/postcss-mixins/package.json @@ -0,0 +1,6 @@ +{ + "private": true, + "dependencies": { + "postcss": "^8.2.14" + } +} diff --git a/types/postcss-mixins/postcss-mixins-tests.ts b/types/postcss-mixins/postcss-mixins-tests.ts new file mode 100644 index 00000000000000..42b9ffa817ae66 --- /dev/null +++ b/types/postcss-mixins/postcss-mixins-tests.ts @@ -0,0 +1,49 @@ +import postcss from 'postcss'; +import postcssMixins = require('postcss-mixins'); + +// Using with postcss with and without config +postcss([postcssMixins]); +postcss([postcssMixins()]); +postcss([postcssMixins({})]); + +// Defining mixins +postcssMixins({ + mixins: { + // Modfifying the mixin directly + mixinFn1(mixin, arg1, arg2) { + arg1; // $ExpectType string + arg2; // $ExpectType string + + const rule = postcss.rule(); + rule.append(mixin.nodes); + mixin.replaceWith(rule); + }, + + // Returning new nodes + mixinFn2(_mixin, color) { + return { + top: 10, + '&:hover': { color }, + }; + }, + + // $ExpectError + invalidFn() { + return 1234; + }, + + // Mixin object + mixinObj: { + top: 10, + '&:hover': { color: 'red' }, + }, + }, +}); + +postcssMixins({ mixinsDir: './mixins' }); +postcssMixins({ mixinsDir: ['./mixins'] }); + +postcssMixins({ mixinsFiles: './mixins/!(*.spec.js)' }); +postcssMixins({ mixinsFiles: ['./mixins/!(*.spec.js)'] }); + +postcssMixins({ silent: true }); diff --git a/types/postcss-mixins/tsconfig.json b/types/postcss-mixins/tsconfig.json new file mode 100644 index 00000000000000..12ddc7b3e1637f --- /dev/null +++ b/types/postcss-mixins/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es6", "ES2018.Promise"], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": ["../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": ["index.d.ts", "postcss-mixins-tests.ts"] +} diff --git a/types/postcss-mixins/tslint.json b/types/postcss-mixins/tslint.json new file mode 100644 index 00000000000000..794cb4bf3e0782 --- /dev/null +++ b/types/postcss-mixins/tslint.json @@ -0,0 +1 @@ +{ "extends": "@definitelytyped/dtslint/dt.json" }