Skip to content

Commit

Permalink
Merge pull request #145 from XhmikosR/dev
Browse files Browse the repository at this point in the history
Assorted updates
  • Loading branch information
joscha authored Feb 22, 2022
2 parents 6b95108 + be9d27b commit 56ea4c9
Show file tree
Hide file tree
Showing 6 changed files with 576 additions and 774 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# editorconfig.org

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Enforce Unix newlines
* text=auto eol=lf
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Supports `@import` and [`@value ... from`](https://github.com/css-modules/postcs

`npm install --save detective-postcss`

It's the CSS (PostCSS dialect) counterpart to [detective](https://github.com/substack/node-detective), [detective-amd](https://github.com/mrjoelkemp/node-detective-amd), [detective-es6](https://github.com/mrjoelkemp/node-detective-es6), [detective-sass](https://github.com/mrjoelkemp/node-detective-sass), [detective-scss](https://github.com/mrjoelkemp/node-detective-scss).
It's the CSS (PostCSS dialect) counterpart to [detective](https://github.com/browserify/detective), [detective-amd](https://github.com/dependents/node-detective-amd), [detective-es6](https://github.com/dependents/node-detective-es6), [detective-sass](https://github.com/dependents/node-detective-sass), [detective-scss](https://github.com/dependents/node-detective-scss).

- The AST is generated using [postcss](https://github.com/postcss/postcss) and [postcss-values-parser](https://github.com/shellscape/postcss-values-parser).

Expand Down
14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
"node": "^10 || ^12 || >=14"
},
"dependencies": {
"debug": "^4.3.1",
"is-url": "^1.2.4",
"postcss": "^8.2.13",
"postcss": "^8.4.6",
"postcss-values-parser": "^5.0.0"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/is-url": "^1.2.28",
"@types/jest": "^26.0.23",
"@types/is-url": "^1.2.30",
"@types/jest": "^26.0.24",
"husky": "^6.0.0",
"jest": "^26.6.3",
"lint-staged": "^10.5.4",
"prettier": "^2.2.1",
"prettier": "^2.5.1",
"rimraf": "^3.0.2",
"ts-jest": "^26.5.5",
"typescript": "^4.2.4"
"ts-jest": "^26.5.6",
"typescript": "^4.5.5"
},
"scripts": {
"prepare": "rimraf ./dist",
Expand Down
30 changes: 19 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as d from 'debug';
import { debuglog } from 'util';
import { parse, AtRule } from 'postcss';
import {
parse as postCssParseValue,
Expand All @@ -12,56 +12,63 @@ import {
} from 'postcss-values-parser';
import isUrl = require('is-url');

const debug = d('detective-postcss');
const debug = debuglog('detective-postcss');

function detective(src, options: detective.Options = { url: false }) {
let references = [];
let root;
try {
root = parse(src);
} catch (e) {
} catch {
throw new detective.MalformedCssError();
}

root.walkAtRules((rule) => {
let file = null;
if (isImportRule(rule)) {
const firstNode = parseValue(rule.params).first;
file = getValueOrUrl(firstNode);
if (file) {
debug(`found %s of %s`, '@import', file);
debug('found %s of %s', '@import', file);
}
}

if (isValueRule(rule)) {
const lastNode = parseValue(rule.params).last;
const prevNode = lastNode.prev();

if (prevNode && isFrom(prevNode)) {
file = getValueOrUrl(lastNode);
if (file) {
debug(`found %s of %s`, '@value with import', file);
debug('found %s of %s', '@value with import', file);
}
}

if (options.url && isUrlNode(lastNode)) {
file = getValueOrUrl(lastNode);
if (file) {
debug(`found %s of %s`, 'url() with import', file);
debug('found %s of %s', 'url() with import', file);
}
}
}
file && references.push(file);

if (file) references.push(file);
});

if (options.url) {
root.walkDecls((decl) => {
const { nodes } = parseValue(decl.value);
const files = nodes.filter(isUrlNode).map(getValueOrUrl);
if (files) {
files.forEach((file) =>
debug(`found %s of %s`, 'url() with import', file),
);
for (const file of files) {
debug('found %s of %s', 'url() with import', file);
}

references = references.concat(files);
}
});
}

return references;
}

Expand All @@ -78,6 +85,7 @@ function getValueOrUrl(node: ChildNode) {
} else {
ret = getValue(node);
}

// is-url sometimes gets data: URLs wrong
return !isUrl(ret) && !ret.startsWith('data:') && ret;
}
Expand Down Expand Up @@ -119,7 +127,7 @@ function isImportRule(rule: AtRule) {
}

function isFrom(node: ChildNode): node is Word {
return node.type == 'word' && node.value === 'from';
return node.type === 'word' && node.value === 'from';
}

namespace detective {
Expand Down
Loading

0 comments on commit 56ea4c9

Please sign in to comment.