diff --git a/dist/parser.js b/dist/parser.js index 8814556..f11520d 100644 --- a/dist/parser.js +++ b/dist/parser.js @@ -9,6 +9,9 @@ const parse = (source, opts) => { const { from } = opts; logger_1.logger.info(`Parsing ${from}`); const document = (0, postcss_1.parse)("", { from }); + // Store the original TypeScript source so the stringifier can return it + // unchanged when there are no inline styles to process. + document.raws.angularSource = sourceString; try { const allStyles = (0, get_component_metadata_1.getComponentStyles)(sourceString, from); logger_1.logger.info(`Located ${allStyles.length} inline styles.`); diff --git a/dist/stringifier.js b/dist/stringifier.js index eae7383..5aff570 100644 --- a/dist/stringifier.js +++ b/dist/stringifier.js @@ -6,7 +6,16 @@ const logger_1 = require("./logger"); const stringify = (node, builder) => { logger_1.logger.info(`Stringify called.`); try { - node.root().nodes.forEach((node) => { + const root = node.root(); + const nodes = root.nodes; + if (nodes.length === 0) { + // No inline styles — return the original TypeScript source unchanged + // to prevent stylelint's fix mode from writing an empty string back + // to disk. + builder(root.raws.angularSource ?? ""); + return; + } + nodes.forEach((node) => { builder(node.raws.angularCodeBefore, node); (0, postcss_1.stringify)(node, builder); builder(node.raws.angularCodeAfter, node); diff --git a/src/parser.ts b/src/parser.ts index 55887f1..fed0801 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -1,4 +1,4 @@ -import { ProcessOptions, Root, parse as postcssParse, Document } from "postcss"; +import { ProcessOptions, Root, parse as postcssParse } from "postcss"; import { getComponentStyles } from "./get-component-metadata"; import { logger } from "./logger"; @@ -10,6 +10,10 @@ export const parse = (source: string, opts: ProcessOptions): Root => { const document = postcssParse("", { from }); + // Store the original TypeScript source so the stringifier can return it + // unchanged when there are no inline styles to process. + document.raws.angularSource = sourceString; + try { const allStyles = getComponentStyles(sourceString, from); diff --git a/src/stringifier.ts b/src/stringifier.ts index ecf5518..4e5c1b5 100644 --- a/src/stringifier.ts +++ b/src/stringifier.ts @@ -5,7 +5,17 @@ export const stringify = (node: AnyNode, builder) => { logger.info(`Stringify called.`); try { - node.root().nodes.forEach((node) => { + const root = node.root(); + const nodes = root.nodes; + + if (nodes.length === 0) { + // No inline styles — return the original TypeScript source unchanged to + // prevent stylelint's fix mode from writing an empty string back to disk. + builder(root.raws.angularSource ?? ""); + return; + } + + nodes.forEach((node) => { builder(node.raws.angularCodeBefore, node); postcssStringify(node, builder); builder(node.raws.angularCodeAfter, node);