[postcss-less] add definitions (#46235)

* [postcss-less] add definitions

https://github.com/shellscape/postcss-less

* Remove needless `FunctionAtRule.params`

* Bump postcss to the latest

* Add `nodeToString()` type and more test cases
This commit is contained in:
Masafumi Koba 2020-07-24 03:45:59 +09:00 committed by GitHub
parent b2ac55feed
commit 60909bad58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 156 additions and 0 deletions

55
types/postcss-less/index.d.ts vendored Normal file
View File

@ -0,0 +1,55 @@
// Type definitions for postcss-less 3.1
// Project: https://github.com/shellscape/postcss-less
// Definitions by: Masafumi Koba <https://github.com/ybiquitous>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import * as postcss from 'postcss';
export = postcssLess;
declare const postcssLess: postcss.Syntax & {
parse: postcss.Parser;
stringify: postcss.Stringifier;
nodeToString: (node: postcss.Node) => string;
};
declare namespace postcssLess {
// @see https://github.com/shellscape/postcss-less/blob/v3.1.4/lib/nodes/import.js
interface ImportAtRule extends postcss.AtRule {
import: true;
filename: string;
options?: string;
}
// @see https://github.com/shellscape/postcss-less/blob/v3.1.4/lib/nodes/variable.js
interface VariableAtRule extends postcss.AtRule {
variable: true;
}
// @see https://github.com/shellscape/postcss-less/blob/v3.1.4/lib/LessParser.js#L147-L151
interface MixinAtRule extends postcss.AtRule {
mixin: true;
important?: true;
}
// @see https://github.com/shellscape/postcss-less/blob/v3.1.4/lib/LessParser.js#L57
interface FunctionAtRule extends postcss.AtRule {
function: true;
}
type AtRule = ImportAtRule | VariableAtRule | MixinAtRule | FunctionAtRule;
// @see https://github.com/shellscape/postcss-less/blob/v3.1.4/lib/LessParser.js#L187
interface ExtendRule extends postcss.Rule {
extend: true;
}
type Rule = ExtendRule;
// @see https://github.com/shellscape/postcss-less/blob/v3.1.4/lib/LessParser.js#L73
interface InlineComment extends postcss.Comment {
inline: true;
}
type Comment = InlineComment;
}

View File

@ -0,0 +1,6 @@
{
"private": true,
"dependencies": {
"postcss": "^7.0.32"
}
}

View File

@ -0,0 +1,71 @@
import postcss = require('postcss');
import less = require('postcss-less');
const lessCode = 'a { @link-color: blue; }';
postcss().process(lessCode, { syntax: less });
less.parse(lessCode).walkAtRules(atRule => {
const lessAtRule = atRule as less.AtRule;
if ('import' in lessAtRule) {
assert(lessAtRule.import === true);
assert(typeof lessAtRule.filename === 'string');
const { options } = lessAtRule;
if (options) {
assert(typeof options === 'string');
} else {
assert(options === undefined);
}
}
if ('variable' in lessAtRule) {
assert(lessAtRule.variable === true);
}
if ('mixin' in lessAtRule) {
assert(lessAtRule.mixin === true);
const { important } = lessAtRule;
if (important) {
assert(important === true);
} else {
assert(important === undefined);
}
}
if ('function' in lessAtRule) {
assert(lessAtRule.function === true);
}
});
less.parse(lessCode).walkRules(rule => {
const lessRule = rule as less.Rule;
assert(lessRule.extend === true);
});
less.parse(lessCode).walkComments(comment => {
const lessComment = comment as less.Comment;
assert(lessComment.inline === true);
});
less.stringify(less.parse(lessCode), (part, node, type) => {
assert(typeof part === 'string');
assert(type === 'start');
assert(type === 'end');
if (node) {
assert(typeof node.root === 'function');
} else {
assert(node === undefined);
}
});
assert(typeof less.nodeToString(less.parse(lessCode)) === 'string');
function assert(condition: any) /* : asserts condition */ {
if (!condition) {
throw new Error('Assertion failed');
}
}

View File

@ -0,0 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"postcss-less-tests.ts"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }