Skip to content

Commit

Permalink
🤖 Merge PR DefinitelyTyped#60326 [postcss-mixins] Add types for postc…
Browse files Browse the repository at this point in the history
…ss-mixins by @MysteryBlokHed

* Add types for postcss-mixins

* Update return type for MixinFn

* Fix MixinObj type
  • Loading branch information
MysteryBlokHed authored May 13, 2022
1 parent 4583137 commit 063c09f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
47 changes: 47 additions & 0 deletions types/postcss-mixins/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Type definitions for postcss-mixins 9.0
// Project: https://github.com/postcss/postcss-mixins
// Definitions by: Adam Thompson-Sharpe <https://github.com/MysteryBlokHed>
// 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<string, Mixin> | 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<string, any>
// tslint:disable-next-line:ban-types
type MixinObj = Record<string, Exclude<Object, Function>>;
}

declare var postcssMixins: PluginCreator<postcssMixins.Options>;

export = postcssMixins;
6 changes: 6 additions & 0 deletions types/postcss-mixins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"postcss": "^8.2.14"
}
}
49 changes: 49 additions & 0 deletions types/postcss-mixins/postcss-mixins-tests.ts
Original file line number Diff line number Diff line change
@@ -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 });
16 changes: 16 additions & 0 deletions types/postcss-mixins/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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"]
}
1 change: 1 addition & 0 deletions types/postcss-mixins/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "@definitelytyped/dtslint/dt.json" }

0 comments on commit 063c09f

Please sign in to comment.