@diplodoc/transform 4.17.2 → 4.18.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.
@@ -3,4 +3,5 @@
3
3
  @import 'print/cut';
4
4
  @import 'print/note';
5
5
  @import 'print/table';
6
- @import 'print/tabs';
6
+ @import 'print/tabs';
7
+ @import 'print/term';
@@ -124,7 +124,10 @@ function liquid<
124
124
  output = applySubstitutions(output, vars, path);
125
125
  }
126
126
 
127
- output = conditionsInCode ? output : repairCode(output, codes);
127
+ if (!conditionsInCode && typeof output === 'string') {
128
+ output = repairCode(output, codes);
129
+ }
130
+
128
131
  codes.length = 0;
129
132
 
130
133
  if (withSourceMap) {
@@ -6,6 +6,7 @@ const quoted = new RegExp(`${singleQuoted.source}|${doubleQuoted.source}`);
6
6
  export const quoteBalanced = new RegExp(`(?:${quoted.source}|[^'"])*`);
7
7
 
8
8
  export const vars = /((not_var)?({{2}([. \w-|(),]+)}{2}))/gm;
9
+ export const singleVariable = /^{{2}([. \w-|(),]+)}{2}$/;
9
10
 
10
11
  // basic types
11
12
  const number = /-?\d+\.?\d*|\.?\d+/;
@@ -66,6 +67,7 @@ export const getParsedMethod = (exp: String) => {
66
67
 
67
68
  export const isLiteral = (str: string) => literalLine.test(str);
68
69
  export const isVariable = (str: string) => variableLine.test(str);
70
+ export const isSingleVariable = (str: string) => singleVariable.test(str);
69
71
 
70
72
  export function parseLiteral(str: string) {
71
73
  let res = str.match(numberLine);
@@ -4,11 +4,35 @@ import ArgvService from './services/argv';
4
4
  import getObject from '../getObject';
5
5
  import {evalExp} from './evaluation';
6
6
  import {log} from '../log';
7
- import {isVariable, vars as varsRe} from './lexical';
7
+ import {
8
+ isSingleVariable,
9
+ isVariable,
10
+ singleVariable as singleVariableRe,
11
+ vars as varsRe,
12
+ } from './lexical';
8
13
 
9
14
  const substitutions = (str: string, builtVars: Record<string, unknown>, path?: string) => {
10
15
  const {keepNotVar} = ArgvService.getConfig();
11
16
 
17
+ if (isSingleVariable(str)) {
18
+ const match = str.match(singleVariableRe);
19
+
20
+ if (!match) {
21
+ return str;
22
+ }
23
+
24
+ const trimVarPath = match[1].trim();
25
+ const value = substituteVariable(trimVarPath, builtVars);
26
+
27
+ if (value === undefined) {
28
+ logNotFoundVariable(trimVarPath, path);
29
+
30
+ return str;
31
+ }
32
+
33
+ return value;
34
+ }
35
+
12
36
  return str.replace(varsRe, (match, _groupNotVar, flag, groupVar, groupVarValue) => {
13
37
  if (flag) {
14
38
  return keepNotVar ? _groupNotVar : groupVar;
@@ -20,21 +44,31 @@ const substitutions = (str: string, builtVars: Record<string, unknown>, path?: s
20
44
  return groupVar;
21
45
  }
22
46
 
23
- let value;
24
- if (isVariable(trimVarPath)) {
25
- value = getObject(trimVarPath, builtVars);
26
- } else {
27
- value = evalExp(trimVarPath, builtVars);
28
- }
47
+ const value = substituteVariable(trimVarPath, builtVars);
29
48
 
30
49
  if (value === undefined) {
31
- value = match;
50
+ logNotFoundVariable(trimVarPath, path);
32
51
 
33
- log.warn(`Variable ${bold(trimVarPath)} not found${path ? ` in ${bold(path)}` : ''}`);
52
+ return match;
34
53
  }
35
54
 
36
55
  return value;
37
56
  });
38
57
  };
39
58
 
59
+ function logNotFoundVariable(varPath: string, path?: string) {
60
+ log.warn(`Variable ${bold(varPath)} not found${path ? ` in ${bold(path)}` : ''}`);
61
+ }
62
+
63
+ function substituteVariable(varPath: string, builtVars: Record<string, unknown>) {
64
+ let value;
65
+ if (isVariable(varPath)) {
66
+ value = getObject(varPath, builtVars);
67
+ } else {
68
+ value = evalExp(varPath, builtVars);
69
+ }
70
+
71
+ return value;
72
+ }
73
+
40
74
  export = substitutions;