2013-02-01 08:47:41 +09:00
|
|
|
'use strict';
|
|
|
|
|
2012-08-02 06:27:42 +09:00
|
|
|
var fs = require('fs'),
|
|
|
|
path = require('path'),
|
|
|
|
vm = require('vm');
|
|
|
|
|
|
|
|
/**
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
* A simple preprocessor that is based on the Firefox preprocessor
|
|
|
|
* (https://dxr.mozilla.org/mozilla-central/source/build/docs/preprocessor.rst).
|
|
|
|
* The main difference is that this supports a subset of the commands and it
|
|
|
|
* supports preprocessor commands in HTML-style comments.
|
|
|
|
*
|
|
|
|
* Currently supported commands:
|
2012-08-02 06:27:42 +09:00
|
|
|
* - if
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
* - elif
|
2012-08-02 06:27:42 +09:00
|
|
|
* - else
|
|
|
|
* - endif
|
|
|
|
* - include
|
|
|
|
* - expand
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
* - error
|
|
|
|
*
|
|
|
|
* Every #if must be closed with an #endif. Nested conditions are supported.
|
|
|
|
*
|
|
|
|
* Within an #if or #else block, one level of comment tokens is stripped. This
|
|
|
|
* allows us to write code that can run even without preprocessing. For example:
|
|
|
|
*
|
|
|
|
* //#if SOME_RARE_CONDITION
|
|
|
|
* // // Decrement by one
|
|
|
|
* // --i;
|
|
|
|
* //#else
|
|
|
|
* // // Increment by one.
|
|
|
|
* ++i;
|
|
|
|
* //#endif
|
2012-08-02 06:27:42 +09:00
|
|
|
*/
|
|
|
|
function preprocess(inFilename, outFilename, defines) {
|
|
|
|
// TODO make this really read line by line.
|
|
|
|
var lines = fs.readFileSync(inFilename).toString().split('\n');
|
|
|
|
var totalLines = lines.length;
|
|
|
|
var out = '';
|
|
|
|
var i = 0;
|
|
|
|
function readLine() {
|
|
|
|
if (i < totalLines) {
|
|
|
|
return lines[i++];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2014-03-10 23:53:41 +09:00
|
|
|
var writeLine = (typeof outFilename === 'function' ? outFilename :
|
|
|
|
function(line) {
|
|
|
|
out += line + '\n';
|
|
|
|
});
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
function evaluateCondition(code) {
|
|
|
|
if (!code || !code.trim()) {
|
|
|
|
throw new Error('No JavaScript expression given at ' + loc());
|
|
|
|
}
|
|
|
|
try {
|
Fix the remaining cases of inconsistent spacing and trailing commas in objects, and enable the `comma-dangle` and `object-curly-spacing` ESLint rules
http://eslint.org/docs/rules/comma-dangle
http://eslint.org/docs/rules/object-curly-spacing
*Please note:* This patch was created automatically, using the ESLint `--fix` command line option. In a couple of places this caused lines to become too long, and I've fixed those manually; please refer to the interdiff below for the only hand-edits in this patch.
```diff
diff --git a/gulpfile.js b/gulpfile.js
index d18b9c58..7d47fd8d 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -1247,7 +1247,8 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var reason = process.env['PDFJS_UPDATE_REASON'];
safeSpawnSync('git', ['init'], { cwd: GH_PAGES_DIR, });
- safeSpawnSync('git', ['remote', 'add', 'origin', REPO], { cwd: GH_PAGES_DIR, });
+ safeSpawnSync('git', ['remote', 'add', 'origin', REPO],
+ { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', ['add', '-A'], { cwd: GH_PAGES_DIR, });
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
```
2017-06-03 00:13:35 +09:00
|
|
|
return vm.runInNewContext(code, defines, { displayErrors: false, });
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Could not evaluate "' + code + '" at ' + loc() + '\n' +
|
|
|
|
e.name + ': ' + e.message);
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
function include(file) {
|
|
|
|
var realPath = fs.realpathSync(inFilename);
|
|
|
|
var dir = path.dirname(realPath);
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
try {
|
2016-04-09 02:34:27 +09:00
|
|
|
var fullpath;
|
|
|
|
if (file.indexOf('$ROOT/') === 0) {
|
|
|
|
fullpath = path.join(__dirname, '../..',
|
|
|
|
file.substring('$ROOT/'.length));
|
|
|
|
} else {
|
|
|
|
fullpath = path.join(dir, file);
|
|
|
|
}
|
|
|
|
preprocess(fullpath, writeLine, defines);
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
} catch (e) {
|
|
|
|
if (e.code === 'ENOENT') {
|
|
|
|
throw new Error('Failed to include "' + file + '" at ' + loc());
|
|
|
|
}
|
|
|
|
throw e; // Some other error
|
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
}
|
|
|
|
function expand(line) {
|
|
|
|
line = line.replace(/__[\w]+__/g, function(variable) {
|
|
|
|
variable = variable.substring(2, variable.length - 2);
|
|
|
|
if (variable in defines) {
|
|
|
|
return defines[variable];
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
});
|
|
|
|
writeLine(line);
|
|
|
|
}
|
|
|
|
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
// not inside if or else (process lines)
|
|
|
|
var STATE_NONE = 0;
|
|
|
|
// inside if, condition false (ignore until #else or #endif)
|
|
|
|
var STATE_IF_FALSE = 1;
|
|
|
|
// inside else, #if was false, so #else is true (process lines until #endif)
|
|
|
|
var STATE_ELSE_TRUE = 2;
|
|
|
|
// inside if, condition true (process lines until #else or #endif)
|
|
|
|
var STATE_IF_TRUE = 3;
|
2016-05-10 08:18:43 +09:00
|
|
|
// inside else or elif, #if/#elif was true, so following #else or #elif is
|
|
|
|
// false (ignore lines until #endif)
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
var STATE_ELSE_FALSE = 4;
|
|
|
|
|
|
|
|
var line;
|
|
|
|
var state = STATE_NONE;
|
|
|
|
var stack = [];
|
Switch to using ESLint, instead of JSHint, for linting
*Please note that most of the necessary code adjustments were made in PR 7890.*
ESLint has a number of advantageous properties, compared to JSHint. Among those are:
- The ability to find subtle bugs, thanks to more rules (e.g. PR 7881).
- Much more customizable in general, and many rules allow fine-tuned behaviour rather than the just the on/off rules in JSHint.
- Many more rules that can help developers avoid bugs, and a lot of rules that can be used to enforce a consistent coding style. The latter should be particularily useful for new contributors (and reduce the amount of stylistic review comments necessary).
- The ability to easily specify exactly what rules to use/not to use, as opposed to JSHint which has a default set. *Note:* in future JSHint version some of the rules we depend on will be removed, according to warnings in http://jshint.com/docs/options/, so we wouldn't be able to update without losing lint coverage.
- More easily disable one, or more, rules temporarily. In JSHint this requires using a numeric code, which isn't very user friendly, whereas in ESLint the rule name is simply used instead.
By default there's no rules enabled in ESLint, but there are some default rule sets available. However, to prevent linting failures if we update ESLint in the future, it seemed easier to just explicitly specify what rules we want.
Obviously this makes the ESLint config file somewhat bigger than the old JSHint config file, but given how rarely that one has been updated over the years I don't think that matters too much.
I've tried, to the best of my ability, to ensure that we enable the same rules for ESLint that we had for JSHint. Furthermore, I've also enabled a number of rules that seemed to make sense, both to catch possible errors *and* various style guide violations.
Despite the ESLint README claiming that it's slower that JSHint, https://github.com/eslint/eslint#how-does-eslint-performance-compare-to-jshint, locally this patch actually reduces the runtime for `gulp` lint (by approximately 20-25%).
A couple of stylistic rules that would have been nice to enable, but where our code currently differs to much to make it feasible:
- `comma-dangle`, controls trailing commas in Objects and Arrays (among others).
- `object-curly-spacing`, controls spacing inside of Objects.
- `spaced-comment`, used to enforce spaces after `//` and `/*. (This is made difficult by the fact that there's still some usage of the old preprocessor left.)
Rules that I indend to look into possibly enabling in follow-ups, if it seems to make sense: `no-else-return`, `no-lonely-if`, `brace-style` with the `allowSingleLine` parameter removed.
Useful links:
- http://eslint.org/docs/user-guide/configuring
- http://eslint.org/docs/rules/
2016-12-15 23:52:29 +09:00
|
|
|
var control = // eslint-disable-next-line max-len
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
/^(?:\/\/|<!--)\s*#(if|elif|else|endif|expand|include|error)\b(?:\s+(.*?)(?:-->)?$)?/;
|
2012-08-02 06:27:42 +09:00
|
|
|
var lineNumber = 0;
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
var loc = function() {
|
|
|
|
return fs.realpathSync(inFilename) + ':' + lineNumber;
|
|
|
|
};
|
|
|
|
while ((line = readLine()) !== null) {
|
2012-08-02 06:27:42 +09:00
|
|
|
++lineNumber;
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
var m = control.exec(line);
|
2012-08-02 06:27:42 +09:00
|
|
|
if (m) {
|
|
|
|
switch (m[1]) {
|
|
|
|
case 'if':
|
|
|
|
stack.push(state);
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
|
|
|
|
break;
|
|
|
|
case 'elif':
|
2016-05-10 08:18:43 +09:00
|
|
|
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
state = STATE_ELSE_FALSE;
|
|
|
|
} else if (state === STATE_IF_FALSE) {
|
|
|
|
state = evaluateCondition(m[2]) ? STATE_IF_TRUE : STATE_IF_FALSE;
|
2016-05-10 08:18:43 +09:00
|
|
|
} else if (state === STATE_ELSE_TRUE) {
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
throw new Error('Found #elif after #else at ' + loc());
|
|
|
|
} else {
|
|
|
|
throw new Error('Found #elif without matching #if at ' + loc());
|
2012-08-02 06:27:42 +09:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'else':
|
2016-05-10 08:18:43 +09:00
|
|
|
if (state === STATE_IF_TRUE || state === STATE_ELSE_FALSE) {
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
state = STATE_ELSE_FALSE;
|
|
|
|
} else if (state === STATE_IF_FALSE) {
|
|
|
|
state = STATE_ELSE_TRUE;
|
|
|
|
} else {
|
|
|
|
throw new Error('Found #else without matching #if at ' + loc());
|
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
break;
|
|
|
|
case 'endif':
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
if (state === STATE_NONE) {
|
|
|
|
throw new Error('Found #endif without #if at ' + loc());
|
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
state = stack.pop();
|
|
|
|
break;
|
|
|
|
case 'expand':
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
2012-08-02 06:27:42 +09:00
|
|
|
expand(m[2]);
|
2014-03-10 23:53:41 +09:00
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
break;
|
|
|
|
case 'include':
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
2012-08-02 06:27:42 +09:00
|
|
|
include(m[2]);
|
2014-03-10 23:53:41 +09:00
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
break;
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
case 'error':
|
|
|
|
if (state !== STATE_IF_FALSE && state !== STATE_ELSE_FALSE) {
|
|
|
|
throw new Error('Found #error ' + m[2] + ' at ' + loc());
|
|
|
|
}
|
|
|
|
break;
|
2012-08-02 06:27:42 +09:00
|
|
|
}
|
|
|
|
} else {
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
if (state === STATE_NONE) {
|
|
|
|
writeLine(line);
|
|
|
|
} else if ((state === STATE_IF_TRUE || state === STATE_ELSE_TRUE) &&
|
|
|
|
stack.indexOf(STATE_IF_FALSE) === -1 &&
|
|
|
|
stack.indexOf(STATE_ELSE_FALSE) === -1) {
|
|
|
|
writeLine(line.replace(/^\/\/|^<!--|-->$/g, ' '));
|
2012-08-02 06:27:42 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
Fix preprocessor: nesting, error & tests
Features / bug fixes in the preprocessor:
- Add word boundary after regex for preprocessor token matching.
Previously, when you mistakenly used "#ifdef" instead of "#if", the
line would be parsed as a preprocessor directive (because "#ifdef"
starts with "#if"), but without condition (because "def" does not
start with a space). Consequently, the condition would always be false
and anything between "#ifdef" and "#endif" would not be included.
- Add validation and error reporting everywhere, to aid debugging.
- Support nested comments (by accounting for the whole stack of
conditions, instead of only the current one).
- Add #elif preprocessor command. Could be used as follows:
//#if !FEATURE_ENABLED
//#error FEATURE_ENABLED must be set
//#endif
- Add #error preprocessor command.
- Add end-of-line word boundary after "-->" in the comment trimmer.
Otherwise the pattern would also match "-->" in the middle of a line,
and incorrectly convert something like "while(i-->0)" to "while(i0)".
Code health:
- Add unit tests for the preprocessor (run external/builder/test.js).
- Fix broken link to MDN (resolved to DXR).
- Refactor to use STATE_* names instead of magic numbers (the original
meaning of the numbers is preserved, with one exception).
- State 3 has been split in two states, to distinguish between being in
an #if and #else. This is needed to ensure that #else cannot be
started without an #if.
2015-07-09 18:03:34 +09:00
|
|
|
if (state !== STATE_NONE || stack.length !== 0) {
|
|
|
|
throw new Error('Missing #endif in preprocessor for ' +
|
|
|
|
fs.realpathSync(inFilename));
|
2014-03-10 23:53:41 +09:00
|
|
|
}
|
|
|
|
if (typeof outFilename !== 'function') {
|
2012-08-02 06:27:42 +09:00
|
|
|
fs.writeFileSync(outFilename, out);
|
2014-03-10 23:53:41 +09:00
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
}
|
|
|
|
exports.preprocess = preprocess;
|
|
|
|
|
2014-02-11 06:06:03 +09:00
|
|
|
var deprecatedInMozcentral = new RegExp('(^|\\W)(' + [
|
|
|
|
'-moz-box-sizing',
|
|
|
|
'-moz-grab',
|
|
|
|
'-moz-grabbing'
|
|
|
|
].join('|') + ')');
|
|
|
|
|
2013-07-10 07:14:21 +09:00
|
|
|
function preprocessCSS(mode, source, destination) {
|
2014-02-11 06:06:03 +09:00
|
|
|
function hasPrefixedFirefox(line) {
|
2013-07-10 07:14:21 +09:00
|
|
|
return (/(^|\W)-(ms|o|webkit)-\w/.test(line));
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:06:03 +09:00
|
|
|
function hasPrefixedMozcentral(line) {
|
|
|
|
return (/(^|\W)-(ms|o|webkit)-\w/.test(line) ||
|
|
|
|
deprecatedInMozcentral.test(line));
|
|
|
|
}
|
|
|
|
|
2014-09-11 01:10:04 +09:00
|
|
|
function expandImports(content, baseUrl) {
|
|
|
|
return content.replace(/^\s*@import\s+url\(([^\)]+)\);\s*$/gm,
|
|
|
|
function(all, url) {
|
|
|
|
var file = path.join(path.dirname(baseUrl), url);
|
|
|
|
var imported = fs.readFileSync(file, 'utf8').toString();
|
|
|
|
return expandImports(imported, file);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:06:03 +09:00
|
|
|
function removePrefixed(content, hasPrefixedFilter) {
|
2013-07-10 07:14:21 +09:00
|
|
|
var lines = content.split(/\r?\n/g);
|
|
|
|
var i = 0;
|
|
|
|
while (i < lines.length) {
|
|
|
|
var line = lines[i];
|
2014-02-11 06:06:03 +09:00
|
|
|
if (!hasPrefixedFilter(line)) {
|
2013-07-10 07:14:21 +09:00
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (/\{\s*$/.test(line)) {
|
|
|
|
var bracketLevel = 1;
|
|
|
|
var j = i + 1;
|
|
|
|
while (j < lines.length && bracketLevel > 0) {
|
|
|
|
var checkBracket = /([{}])\s*$/.exec(lines[j]);
|
|
|
|
if (checkBracket) {
|
|
|
|
if (checkBracket[1] === '{') {
|
|
|
|
bracketLevel++;
|
|
|
|
} else if (lines[j].indexOf('{') < 0) {
|
|
|
|
bracketLevel--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
lines.splice(i, j - i);
|
|
|
|
} else if (/[};]\s*$/.test(line)) {
|
|
|
|
lines.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
// multiline? skipping until next directive or bracket
|
|
|
|
do {
|
|
|
|
lines.splice(i, 1);
|
|
|
|
} while (i < lines.length &&
|
|
|
|
!/\}\s*$/.test(lines[i]) &&
|
|
|
|
lines[i].indexOf(':') < 0);
|
|
|
|
if (i < lines.length && /\S\s*}\s*$/.test(lines[i])) {
|
|
|
|
lines[i] = lines[i].substr(lines[i].indexOf('}'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// collapse whitespaces
|
|
|
|
while (lines[i] === '' && lines[i - 1] === '') {
|
|
|
|
lines.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lines.join('\n');
|
|
|
|
}
|
|
|
|
|
2014-09-11 01:10:04 +09:00
|
|
|
if (!mode) {
|
2013-07-10 07:14:21 +09:00
|
|
|
throw new Error('Invalid CSS preprocessor mode');
|
|
|
|
}
|
|
|
|
|
2014-09-11 01:10:04 +09:00
|
|
|
var content = fs.readFileSync(source, 'utf8').toString();
|
|
|
|
content = expandImports(content, source);
|
|
|
|
if (mode === 'mozcentral' || mode === 'firefox') {
|
|
|
|
content = removePrefixed(content, mode === 'mozcentral' ?
|
|
|
|
hasPrefixedMozcentral : hasPrefixedFirefox);
|
|
|
|
}
|
|
|
|
fs.writeFileSync(destination, content);
|
2013-07-10 07:14:21 +09:00
|
|
|
}
|
|
|
|
exports.preprocessCSS = preprocessCSS;
|
|
|
|
|
2012-08-02 06:27:42 +09:00
|
|
|
/**
|
|
|
|
* Merge two defines arrays. Values in the second param will override values in
|
|
|
|
* the first.
|
|
|
|
*/
|
|
|
|
function merge(defaults, defines) {
|
|
|
|
var ret = {};
|
2014-03-10 23:53:41 +09:00
|
|
|
for (var key in defaults) {
|
2012-08-02 06:27:42 +09:00
|
|
|
ret[key] = defaults[key];
|
2014-03-10 23:53:41 +09:00
|
|
|
}
|
|
|
|
for (key in defines) {
|
2012-08-02 06:27:42 +09:00
|
|
|
ret[key] = defines[key];
|
2014-03-10 23:53:41 +09:00
|
|
|
}
|
2012-08-02 06:27:42 +09:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
exports.merge = merge;
|