Skip to content

Commit

Permalink
Replace 3rd-party type definitions (stylelint#4857)
Browse files Browse the repository at this point in the history
* Replace 3rd-party type definitions

This change replaces 3rd-party type definitions under the `types/` directory
with the following `@types/*` npm packages.

- [@types/balanced-match](https://www.npmjs.com/package/@types/balanced-match)
- [@types/imurmurhash](https://www.npmjs.com/package/@types/imurmurhash)
- [@types/postcss-safe-parser](https://www.npmjs.com/package/@types/postcss-safe-parser)
- [@types/style-search](https://www.npmjs.com/package/@types/style-search)
- [@types/svg-tags](https://www.npmjs.com/package/@types/svg-tags)
- [@types/write-file-atomic](https://www.npmjs.com/package/@types/write-file-atomic)

Note that this change does **not** replace all type definitions under `types/`.
We should address the remaining types via other pull requests.

Related to stylelint#4399

* Replace with `@types/file-entry-cache`
  • Loading branch information
ybiquitous committed Jul 12, 2020
1 parent b1187b6 commit 909063d
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 74 deletions.
8 changes: 7 additions & 1 deletion lib/rules/function-calc-no-unspaced-operator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ function rule(actual) {
return;
}

const parensMatch = balancedMatch('(', ')', valueParser.stringify(node));
const nodeText = valueParser.stringify(node);
const parensMatch = balancedMatch('(', ')', nodeText);

if (!parensMatch) {
throw new Error(`No parens match: "${nodeText}"`);
}

const rawExpression = parensMatch.body;
const expressionIndex =
decl.source.start.column +
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/FileCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const path = require('path');
const DEFAULT_CACHE_LOCATION = './.stylelintcache';
const DEFAULT_HASH = '';

/** @typedef {import('file-entry-cache').FileDescriptor["meta"] & { hashOfConfig?: string }} CacheMetadata */

/**
* @param {string} [cacheLocation]
* @param {string} [hashOfConfig]
Expand All @@ -30,6 +32,7 @@ class FileCache {
// Get file descriptor compares current metadata against cached
// one and stores the result to "changed" prop.w
const descriptor = this._fileCache.getFileDescriptor(absoluteFilepath);
/** @type {CacheMetadata} */
const meta = descriptor.meta || {};
const changed = descriptor.changed || meta.hashOfConfig !== this._hashOfConfig;

Expand Down
9 changes: 7 additions & 2 deletions lib/utils/blurFunctionArguments.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ module.exports = function (source, functionName, blurChar = '`') {
while (lowerCaseSource.includes(nameWithParen, searchStartIndex)) {
const openingParenIndex =
lowerCaseSource.indexOf(nameWithParen, searchStartIndex) + functionNameLength;
const closingParenIndex =
balancedMatch('(', ')', lowerCaseSource.slice(openingParenIndex)).end + openingParenIndex;
const parensMatch = balancedMatch('(', ')', lowerCaseSource.slice(openingParenIndex));

if (!parensMatch) {
throw new Error(`No parens match: "${source}"`);
}

const closingParenIndex = parensMatch.end + openingParenIndex;
const argumentsLength = closingParenIndex - openingParenIndex - 1;

result =
Expand Down
4 changes: 4 additions & 0 deletions lib/utils/functionArgumentsSearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ module.exports = function (source, functionName, callback) {

const parensMatch = balancedMatch('(', ')', source.substr(match.startIndex));

if (!parensMatch) {
throw new Error(`No parens match: "${source}"`);
}

callback(parensMatch.body, match.endIndex + 1);
},
);
Expand Down
2 changes: 1 addition & 1 deletion lib/writeOutputFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const writeFileAtomic = require('write-file-atomic');
/**
* @param {string} content
* @param {string} filePath
* @returns {Promise<Error | undefined>}
* @returns {Promise<void>}
*/
module.exports = (content, filePath) =>
writeFileAtomic(path.normalize(filePath), stripAnsi(content));
51 changes: 51 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,19 @@
"devDependencies": {
"@stylelint/prettier-config": "^2.0.0",
"@stylelint/remark-preset": "^1.0.0",
"@types/balanced-match": "^1.0.1",
"@types/browserslist": "^4.8.0",
"@types/debug": "^4.1.5",
"@types/file-entry-cache": "^5.0.1",
"@types/global-modules": "^2.0.0",
"@types/globjoin": "^0.1.0",
"@types/imurmurhash": "^0.1.1",
"@types/lodash": "^4.14.155",
"@types/micromatch": "^4.0.1",
"@types/postcss-safe-parser": "^4.0.0",
"@types/style-search": "^0.1.1",
"@types/svg-tags": "^1.0.0",
"@types/write-file-atomic": "^3.0.1",
"benchmark": "^2.1.4",
"common-tags": "^1.8.0",
"del": "^5.1.0",
Expand Down
5 changes: 0 additions & 5 deletions types/balanced-match/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions types/file-entry-cache/index.d.ts

This file was deleted.

5 changes: 0 additions & 5 deletions types/imurmurhash/index.d.ts

This file was deleted.

10 changes: 0 additions & 10 deletions types/postcss/extensions.d.ts

This file was deleted.

17 changes: 0 additions & 17 deletions types/style-search/index.d.ts

This file was deleted.

6 changes: 0 additions & 6 deletions types/svg-tags/index.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/svg-tags/postcss-safe-parser.ts

This file was deleted.

21 changes: 0 additions & 21 deletions types/write-file-atomic/index.d.ts

This file was deleted.

0 comments on commit 909063d

Please sign in to comment.