postcss-normalize-string 5.0.4 → 6.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,11 +1,13 @@
1
1
  {
2
2
  "name": "postcss-normalize-string",
3
- "version": "5.0.4",
3
+ "version": "6.0.0",
4
4
  "description": "Normalize wrapping quotes for CSS string literals.",
5
5
  "main": "src/index.js",
6
+ "types": "types/index.d.ts",
6
7
  "files": [
7
8
  "src",
8
- "LICENSE-MIT"
9
+ "LICENSE-MIT",
10
+ "types"
9
11
  ],
10
12
  "keywords": [
11
13
  "css",
@@ -27,13 +29,12 @@
27
29
  "url": "https://github.com/cssnano/cssnano/issues"
28
30
  },
29
31
  "engines": {
30
- "node": "^10 || ^12 || >=14.0"
32
+ "node": "^14 || ^16 || >=18.0"
31
33
  },
32
34
  "devDependencies": {
33
35
  "postcss": "^8.2.15"
34
36
  },
35
37
  "peerDependencies": {
36
38
  "postcss": "^8.2.15"
37
- },
38
- "readme": "# [postcss][postcss]-normalize-string\n\n> Normalize strings with PostCSS.\n\n## Install\n\nWith [npm](https://npmjs.org/package/postcss-normalize-string) do:\n\n```\nnpm install postcss-normalize-string --save\n```\n\n## Example\n\n### Input\n\n```css\np:after{ content: '\\\\'string\\\\' is intact' }\n```\n\n### Output\n\n```css\np:after{ content:\"'string' is intact\" }\n```\n\n## Usage\n\nSee the [PostCSS documentation](https://github.com/postcss/postcss#usage) for\nexamples for your environment.\n\n## API\n\n### normalize([options])\n\n#### options\n\n##### preferredQuote\n\nType: `string`\nDefault: `double`\n\nSets what type of quote to prefer. Possible values are `single` and `double`.\n\n```js\nvar css = 'p:after{content:\"\"}';\nconsole.log(postcss(normalize({preferredQuote: 'single'})).process(css).css);\n//=> p:after{content:''}\n```\n\n## Contributors\n\nSee [CONTRIBUTORS.md](https://github.com/cssnano/cssnano/blob/master/CONTRIBUTORS.md).\n\n## License\n\nMIT © [Ben Briggs](http://beneb.info)\n\n[postcss]: https://github.com/postcss/postcss\n"
39
+ }
39
40
  }
package/src/index.js CHANGED
@@ -46,6 +46,17 @@ const T_SINGLE_QUOTE = { type: C_SINGLE_QUOTE, value: L_SINGLE_QUOTE };
46
46
  const T_DOUBLE_QUOTE = { type: C_DOUBLE_QUOTE, value: L_DOUBLE_QUOTE };
47
47
  const T_NEWLINE = { type: C_NEWLINE, value: L_NEWLINE };
48
48
 
49
+ /** @typedef {T_ESCAPED_SINGLE_QUOTE | T_ESCAPED_DOUBLE_QUOTE | T_SINGLE_QUOTE | T_NEWLINE} StringAstNode */
50
+ /**
51
+ * @typedef {{nodes: StringAstNode[],
52
+ * types: {escapedSingleQuote: number, escapedDoubleQuote: number, singleQuote: number, doubleQuote: number},
53
+ * quotes: boolean}} StringAst
54
+ */
55
+
56
+ /**
57
+ * @param {StringAst} ast
58
+ * @return {string}
59
+ */
49
60
  function stringify(ast) {
50
61
  return ast.nodes.reduce((str, { value }) => {
51
62
  // Collapse multiple line strings automatically
@@ -57,11 +68,16 @@ function stringify(ast) {
57
68
  }, '');
58
69
  }
59
70
 
71
+ /**
72
+ * @param {string} str
73
+ * @return {StringAst}
74
+ */
60
75
  function parse(str) {
61
76
  let code, next, value;
62
77
  let pos = 0;
63
78
  let len = str.length;
64
79
 
80
+ /** @type StringAst */
65
81
  const ast = {
66
82
  nodes: [],
67
83
  types: {
@@ -159,6 +175,11 @@ function parse(str) {
159
175
  return ast;
160
176
  }
161
177
 
178
+ /**
179
+ * @param {valueParser.StringNode} node
180
+ * @param {StringAst} ast
181
+ * @return {void}
182
+ */
162
183
  function changeWrappingQuotes(node, ast) {
163
184
  const { types } = ast;
164
185
 
@@ -185,7 +206,9 @@ function changeWrappingQuotes(node, ast) {
185
206
  ast.nodes = changeChildQuotes(ast.nodes, node.quote);
186
207
  }
187
208
  /**
209
+ * @param {StringAstNode[]} childNodes
188
210
  * @param {string} parentQuote
211
+ * @return {StringAstNode[]}
189
212
  */
190
213
  function changeChildQuotes(childNodes, parentQuote) {
191
214
  const updatedChildren = [];
@@ -207,6 +230,11 @@ function changeChildQuotes(childNodes, parentQuote) {
207
230
  return updatedChildren;
208
231
  }
209
232
 
233
+ /**
234
+ * @param {string} value
235
+ * @param {'single' | 'double'} preferredQuote
236
+ * @return {string}
237
+ */
210
238
  function normalize(value, preferredQuote) {
211
239
  if (!value || !value.length) {
212
240
  return value;
@@ -233,16 +261,28 @@ function normalize(value, preferredQuote) {
233
261
  .toString();
234
262
  }
235
263
 
264
+ /**
265
+ * @param {string} original
266
+ * @param {Map<string, string>} cache
267
+ * @param {'single' | 'double'} preferredQuote
268
+ * @return {string}
269
+ */
236
270
  function minify(original, cache, preferredQuote) {
237
271
  const key = original + '|' + preferredQuote;
238
272
  if (cache.has(key)) {
239
- return cache.get(key);
273
+ return /** @type {string} */ (cache.get(key));
240
274
  }
241
275
  const newValue = normalize(original, preferredQuote);
242
276
  cache.set(key, newValue);
243
277
  return newValue;
244
278
  }
245
279
 
280
+ /** @typedef {{preferredQuote?: 'double' | 'single'}} Options */
281
+ /**
282
+ * @type {import('postcss').PluginCreator<Options>}
283
+ * @param {Options} opts
284
+ * @return {import('postcss').Plugin}
285
+ */
246
286
  function pluginCreator(opts) {
247
287
  const { preferredQuote } = Object.assign(
248
288
  {},
@@ -0,0 +1,38 @@
1
+ export = pluginCreator;
2
+ /** @typedef {{preferredQuote?: 'double' | 'single'}} Options */
3
+ /**
4
+ * @type {import('postcss').PluginCreator<Options>}
5
+ * @param {Options} opts
6
+ * @return {import('postcss').Plugin}
7
+ */
8
+ declare function pluginCreator(opts: Options): import('postcss').Plugin;
9
+ declare namespace pluginCreator {
10
+ export { postcss, StringAstNode, StringAst, Options };
11
+ }
12
+ type Options = {
13
+ preferredQuote?: 'double' | 'single';
14
+ };
15
+ declare var postcss: true;
16
+ type StringAstNode = {
17
+ type: string;
18
+ value: string;
19
+ } | {
20
+ type: string;
21
+ value: string;
22
+ } | {
23
+ type: string;
24
+ value: string;
25
+ } | {
26
+ type: string;
27
+ value: string;
28
+ };
29
+ type StringAst = {
30
+ nodes: StringAstNode[];
31
+ types: {
32
+ escapedSingleQuote: number;
33
+ escapedDoubleQuote: number;
34
+ singleQuote: number;
35
+ doubleQuote: number;
36
+ };
37
+ quotes: boolean;
38
+ };