Skip to content

Commit 987b48e

Browse files
authored
Use early continue to simplify preprocess function. NFC. (#18613)
1 parent 85fab9f commit 987b48e

File tree

1 file changed

+62
-61
lines changed

1 file changed

+62
-61
lines changed

src/parseTools.js

Lines changed: 62 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -75,78 +75,79 @@ function preprocess(filename) {
7575
inStyle = false;
7676
}
7777

78-
if (!inStyle) {
79-
const trimmed = line.trim();
80-
if (trimmed.startsWith('#')) {
81-
const first = trimmed.split(' ', 1)[0];
82-
if (first == '#if' || first == '#ifdef' || first == '#elif') {
83-
if (first == '#ifdef') {
84-
warn('use of #ifdef in js library. Use #if instead.');
85-
}
86-
if (first == '#elif') {
87-
const curr = showStack.pop();
88-
if (curr == SHOW || curr == IGNORE_ALL) {
89-
// If we showed to previous block we enter the IGNORE_ALL state
90-
// and stay there until endif is seen
91-
showStack.push(IGNORE_ALL);
92-
continue;
93-
}
94-
}
95-
const after = trimmed.substring(trimmed.indexOf(' '));
96-
const truthy = !!vm.runInThisContext(after, { filename, lineOffset: i, columnOffset: line.indexOf(after) });
97-
showStack.push(truthy ? SHOW : IGNORE);
98-
} else if (first === '#include') {
99-
if (showCurrentLine()) {
100-
let filename = line.substr(line.indexOf(' ') + 1);
101-
if (filename.startsWith('"')) {
102-
filename = filename.substr(1, filename.length - 2);
103-
}
104-
const result = preprocess(filename);
105-
if (result) {
106-
ret += `// include: ${filename}\n`;
107-
ret += result;
108-
ret += `// end include: ${filename}\n`;
109-
}
110-
}
111-
} else if (first === '#else') {
112-
assert(showStack.length > 0);
78+
if (inStyle) {
79+
if (showCurrentLine()) {
80+
ret += line + '\n';
81+
}
82+
continue;
83+
}
84+
85+
const trimmed = line.trim();
86+
if (trimmed.startsWith('#')) {
87+
const first = trimmed.split(' ', 1)[0];
88+
if (first == '#if' || first == '#ifdef' || first == '#elif') {
89+
if (first == '#ifdef') {
90+
warn('use of #ifdef in js library. Use #if instead.');
91+
}
92+
if (first == '#elif') {
11393
const curr = showStack.pop();
114-
if (curr == IGNORE) {
115-
showStack.push(SHOW);
116-
} else {
117-
showStack.push(IGNORE);
94+
if (curr == SHOW || curr == IGNORE_ALL) {
95+
// If we showed to previous block we enter the IGNORE_ALL state
96+
// and stay there until endif is seen
97+
showStack.push(IGNORE_ALL);
98+
continue;
11899
}
119-
} else if (first === '#endif') {
120-
assert(showStack.length > 0);
121-
showStack.pop();
122-
} else if (first === '#warning') {
123-
if (showCurrentLine()) {
124-
printErr(`${filename}:${i + 1}: #warning ${trimmed.substring(trimmed.indexOf(' ')).trim()}`);
100+
}
101+
const after = trimmed.substring(trimmed.indexOf(' '));
102+
const truthy = !!vm.runInThisContext(after, { filename, lineOffset: i, columnOffset: line.indexOf(after) });
103+
showStack.push(truthy ? SHOW : IGNORE);
104+
} else if (first === '#include') {
105+
if (showCurrentLine()) {
106+
let filename = line.substr(line.indexOf(' ') + 1);
107+
if (filename.startsWith('"')) {
108+
filename = filename.substr(1, filename.length - 2);
125109
}
126-
} else if (first === '#error') {
127-
if (showCurrentLine()) {
128-
error(`${filename}:${i + 1}: #error ${trimmed.substring(trimmed.indexOf(' ')).trim()}`);
110+
const result = preprocess(filename);
111+
if (result) {
112+
ret += `// include: ${filename}\n`;
113+
ret += result;
114+
ret += `// end include: ${filename}\n`;
129115
}
116+
}
117+
} else if (first === '#else') {
118+
assert(showStack.length > 0);
119+
const curr = showStack.pop();
120+
if (curr == IGNORE) {
121+
showStack.push(SHOW);
130122
} else {
131-
throw new Error(`${filename}:${i + 1}: Unknown preprocessor directive ${first}`);
123+
showStack.push(IGNORE);
132124
}
133-
} else {
125+
} else if (first === '#endif') {
126+
assert(showStack.length > 0);
127+
showStack.pop();
128+
} else if (first === '#warning') {
134129
if (showCurrentLine()) {
135-
// Never emit more than one empty line at a time.
136-
if (emptyLine && !line) {
137-
continue;
138-
}
139-
ret += line + '\n';
140-
if (!line) {
141-
emptyLine = true;
142-
} else {
143-
emptyLine = false;
144-
}
130+
printErr(`${filename}:${i + 1}: #warning ${trimmed.substring(trimmed.indexOf(' ')).trim()}`);
131+
}
132+
} else if (first === '#error') {
133+
if (showCurrentLine()) {
134+
error(`${filename}:${i + 1}: #error ${trimmed.substring(trimmed.indexOf(' ')).trim()}`);
145135
}
136+
} else {
137+
throw new Error(`${filename}:${i + 1}: Unknown preprocessor directive ${first}`);
146138
}
147-
} else { // !inStyle
139+
} else {
148140
if (showCurrentLine()) {
141+
// Never emit more than one empty line at a time.
142+
if (emptyLine && !line) {
143+
continue;
144+
}
149145
ret += line + '\n';
146+
if (!line) {
147+
emptyLine = true;
148+
} else {
149+
emptyLine = false;
150+
}
150151
}
151152
}
152153
}

0 commit comments

Comments
 (0)