Skip to content

Commit f94685d

Browse files
authored
Merge pull request mahirshah#8 from css-blocks/js-gen-updates
JS Grammar Updates
2 parents 59ed2d5 + 4623cdd commit f94685d

File tree

8 files changed

+199
-1064
lines changed

8 files changed

+199
-1064
lines changed

package-lock.json

Lines changed: 164 additions & 1040 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
"lint": "node node_modules/eslint/bin/eslint.js src/",
1313
"precommit-msg": "echo 'Pre-commit checks...' && exit 0"
1414
},
15-
"pre-commit": [ "precommit-msg", "lint", "test"],
15+
"pre-commit": [
16+
"precommit-msg",
17+
"lint",
18+
"test"
19+
],
1620
"author": "mahirshah",
1721
"license": "ISC",
1822
"types": "css-property-parser.d.ts",
@@ -35,6 +39,6 @@
3539
"fs-extra": "^3.0.1",
3640
"mdn-data": "1.0.0",
3741
"moo": "^0.4.1",
38-
"nearley": "^2.10.3"
42+
"nearley": "^2.11.0"
3943
}
4044
}

src/constants/paths.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
const path = require('path');
2+
13
module.exports = {
2-
JSON_GRAMMAR_PATH: `${__dirname}/../grammars/json/`,
3-
NEARLEY_PROPERTY_GRAMMAR_PATH: `${__dirname}/../grammars/nearley/properties/`,
4-
GENERATED_JSON_GRAMMAR_PATH: `${__dirname}/../grammars/generated/json/`,
5-
GENERATED_NEARLEY_GRAMMAR_PATH: `${__dirname}/../grammars/generated/nearley/`,
6-
GENERATED_JS_GRAMMAR_PATH: `${__dirname}/../grammars/generated/js/`,
7-
FORMATTED_DATA_PATH: `${__dirname}/../../formatted-data/`,
8-
FORMAL_SYNTAX_GRAMMAR_PATH: `${__dirname}/../grammars/`,
9-
NEARLEY_BIN_ROOT: `${__dirname}/../../node_modules/nearley/bin/`,
4+
JSON_GRAMMAR_PATH: path.join(__dirname, '/../grammars/json/'),
5+
NEARLEY_PROPERTY_GRAMMAR_PATH: path.join(__dirname, '/../grammars/nearley/properties/'),
6+
GENERATED_JSON_GRAMMAR_PATH: path.join(__dirname, '/../grammars/generated/json/'),
7+
GENERATED_NEARLEY_GRAMMAR_PATH: path.join(__dirname, '/../grammars/generated/nearley/'),
8+
GENERATED_JS_GRAMMAR_PATH: path.join(__dirname, '/../grammars/generated/js/'),
9+
FORMATTED_DATA_PATH: path.join(__dirname, '/../../formatted-data/'),
10+
FORMAL_SYNTAX_GRAMMAR_PATH: path.join(__dirname, '/../grammars/'),
11+
NEARLEY_BIN_ROOT: path.join(__dirname, '/../../node_modules/nearley/bin/'),
1012
};

src/expandShorthandProperty.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const shorthandProperties = require('../formatted-data/shorthand-properties.json
66
const CSS_CONSTANTS = require('./constants/css');
77
const { CLASSIFICATIONS } = require('./constants/shorthandProperties');
88
const LocationIndexTracker = require('./utils/LocationIndexTracker');
9-
const PATHS = require('./constants/paths');
109
const { ParseError, UnsupportedPropertyError, UnknownPropertyError } = require('./errors');
1110
const SHORTHAND_IDENT_TO_LONGHAND_PROPERTY_MAP = require('./constants/shorthandIdentToLonghandPropertyMap.json');
1211
const {
@@ -89,8 +88,7 @@ module.exports = function expandShorthandProperty(propertyName,
8988
}
9089

9190
// get the compiled grammar file for this property
92-
// eslint-disable-next-line import/no-dynamic-require
93-
const grammar = require(`${PATHS.GENERATED_JS_GRAMMAR_PATH}${propertyName}`);
91+
const grammar = require('./grammars/generated')[propertyName];
9492
// remove any block style comments and extra whitespace
9593
const formattedPropertyValue = propertyValue.replace(R_BLOCK_COMMENT, ' ').replace(/\s+/g, ' ').trim();
9694
let parser;

src/grammars/json/hex-color.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[
2-
["Base", "%hexColor"]
3-
]
2+
["Base", "%color"]
3+
]

src/isValidDeclaration.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const nearley = require('nearley');
22
const { css: { properties } } = require('mdn-data');
3-
const { PATHS, CSS } = require('./constants');
3+
const { CSS } = require('./constants');
44

55
/**
66
* Checks if the given property, value pair is valid.
@@ -16,8 +16,7 @@ module.exports = function isValidDeclaration(property, value) {
1616
return true;
1717
}
1818

19-
// eslint-disable-next-line import/no-dynamic-require
20-
const propertyGrammar = require(`${PATHS.GENERATED_JS_GRAMMAR_PATH}${property}.js`);
19+
const propertyGrammar = require('./grammars/generated')[property];
2120

2221
try {
2322
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(propertyGrammar)).feed(value);

src/scripts/formatGrammars.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Format each JSON grammar into a Nearley grammar
33
*/
44
const fs = require('fs-extra');
5+
const path = require('path');
56
const NearleyGrammarFormatter = require('../formatters/grammarFormatters/NearleyGrammarFormatter');
67
const PATHS = require('../constants/paths');
78
const GRAMMAR_CONSTANTS = require('../constants/grammars');
@@ -58,17 +59,26 @@ const compilationQueue = async.queue((task, callback) => {
5859
console.log(task);
5960
exec(task, callback);
6061
}, MAX_PARALLEL_PROCESSES);
62+
63+
const jsModules = [];
6164
const compilationCommands = fs.readdirSync(PATHS.GENERATED_NEARLEY_GRAMMAR_PATH)
6265
.map((fileName) => {
66+
const propName = fileName.replace(`.${GRAMMAR_CONSTANTS.GRAMMAR_FILE_EXTENSION}`, '');
67+
const jsFileName = `${propName}.${JAVASCRIPT_FILE_EXTENSION}`;
6368
const nearleyFilePath = JSON.stringify(`${PATHS.GENERATED_NEARLEY_GRAMMAR_PATH}${fileName}`);
6469
const jsFilePath = JSON.stringify(
65-
`${PATHS.GENERATED_JS_GRAMMAR_PATH}${fileName.replace(`.${GRAMMAR_CONSTANTS.GRAMMAR_FILE_EXTENSION}`,
66-
`.${JAVASCRIPT_FILE_EXTENSION}`)}`
70+
path.join(PATHS.GENERATED_JS_GRAMMAR_PATH, jsFileName)
6771
);
6872

73+
jsModules.push(` '${propName}': require('./js/${jsFileName}')`);
74+
6975
return `node ${PATHS.NEARLEY_BIN_ROOT}${NEARLEY_COMPILER_FILE_NAME} ${nearleyFilePath} > ${jsFilePath}`;
7076
});
7177

78+
const jsExportsFile = `module.exports = {\n${jsModules.join(',\n')}\n}`;
79+
80+
fs.writeFileSync(path.join(PATHS.GENERATED_JS_GRAMMAR_PATH, '../index.js'), jsExportsFile);
81+
7282
compilationQueue.push(compilationCommands, (err) => {
7383
if (err) {
7484
console.error(err);

test/BaseGrammarsTest.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
const { assert } = require('chai');
22
const nearley = require('nearley');
3-
const { PATHS } = require('../src/constants');
4-
53

64
/**
75
* Tests for {@link isValidDeclaration}
86
*/
97
describe('baseGrammars', function () {
108
// TODO: add support for scientific notation
119
describe('number', function () {
12-
const propertyGrammar = require(`${PATHS.GENERATED_JS_GRAMMAR_PATH}number.js`);
10+
const propertyGrammar = require('../src/grammars/generated').number;
1311

1412
[
1513
'1',
@@ -34,7 +32,7 @@ describe('baseGrammars', function () {
3432
});
3533

3634
describe('length', function () {
37-
const propertyGrammar = require(`${PATHS.GENERATED_JS_GRAMMAR_PATH}length.js`);
35+
const propertyGrammar = require('../src/grammars/generated').length;
3836

3937
[
4038
'1px',

0 commit comments

Comments
 (0)