12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- /**
- * @fileoverview Detects unused styles
- * @author Tom Hastjarjanto
- */
- 'use strict';
- const Components = require('../util/Components');
- const styleSheet = require('../util/stylesheet');
- const { StyleSheets } = styleSheet;
- const { astHelpers } = styleSheet;
- const create = Components.detect((context, components) => {
- const styleSheets = new StyleSheets();
- const styleReferences = new Set();
- function reportUnusedStyles(unusedStyles) {
- Object.keys(unusedStyles).forEach((key) => {
- if ({}.hasOwnProperty.call(unusedStyles, key)) {
- const styles = unusedStyles[key];
- styles.forEach((node) => {
- const message = [
- 'Unused style detected: ',
- key,
- '.',
- node.key.name,
- ].join('');
- context.report(node, message);
- });
- }
- });
- }
- return {
- MemberExpression: function (node) {
- const styleRef = astHelpers.getPotentialStyleReferenceFromMemberExpression(node);
- if (styleRef) {
- styleReferences.add(styleRef);
- }
- },
- CallExpression: function (node) {
- if (astHelpers.isStyleSheetDeclaration(node, context.settings)) {
- const styleSheetName = astHelpers.getStyleSheetName(node);
- const styles = astHelpers.getStyleDeclarations(node);
- styleSheets.add(styleSheetName, styles);
- }
- },
- 'Program:exit': function () {
- const list = components.all();
- if (Object.keys(list).length > 0) {
- styleReferences.forEach((reference) => {
- styleSheets.markAsUsed(reference);
- });
- reportUnusedStyles(styleSheets.getUnusedReferences());
- }
- },
- };
- });
- module.exports = {
- meta: {
- schema: [],
- },
- create,
- };
|