Skip to content

Commit ff4b706

Browse files
committed
Use path.join instead of string joins for path construction. Move dynamic requires to boot time. Move formatted-data to the src directory.
1 parent 81cb2db commit ff4b706

18 files changed

+64
-53
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ node_modules
44
.vscode
55

66
# ignore generated grammars
7-
formatted-data
7+
src/formatted-data
88
src/grammars/generated
99

1010

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"prepublish": "npm start",
1212
"test": "./node_modules/.bin/mocha --reporter spec --recursive",
1313
"start": "./updateCSSData.sh",
14-
"clean": "rm -rf src/grammars/generated",
14+
"clean": "rm -rf src/formatted-data src/grammars/generated",
1515
"benchmark": "node test/benchmark.js",
1616
"doctoc": "node ./node_modules/doctoc/doctoc.js README.md",
1717
"lint": "node node_modules/eslint/bin/eslint.js src/",

src/constants/paths.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const path = require('path');
22

33
module.exports = {
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/'),
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'),
1212
};

src/expandShorthandProperty.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const nearley = require('nearley');
2-
const properties = require('../formatted-data/properties.json');
2+
const properties = require('./formatted-data/properties.json');
33
const isShorthandProperty = require('./isShorthandProperty');
44
const getShorthandComputedProperties = require('./getShorthandComputedProperties');
5-
const shorthandProperties = require('../formatted-data/shorthand-properties.json');
5+
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');
@@ -18,6 +18,7 @@ const {
1818
UnorderedOptionalListPropertyFormatter,
1919
} = require('./formatters/shorthandPropertyTypeFormatters');
2020
const { initialValue, initialValues } = require('./initialValueMap');
21+
const grammars = require('./grammars/generated');
2122

2223
const shorthandPropertyTypeToActionDictionaryFactoryMap = {
2324
[CLASSIFICATIONS.TRBL]: TrblPropertyFormatter,
@@ -88,7 +89,7 @@ module.exports = function expandShorthandProperty(propertyName,
8889
}
8990

9091
// get the compiled grammar file for this property
91-
const grammar = require('./grammars/generated')[propertyName];
92+
const grammar = grammars[propertyName];
9293
// remove any block style comments and extra whitespace
9394
const formattedPropertyValue = propertyValue.replace(R_BLOCK_COMMENT, ' ').replace(/\s+/g, ' ').trim();
9495
let parser;

src/formatters/grammarFormatters/NearleyGrammarFormatter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const CaseConverterUtils = require('../../utils/CaseConverterUtils');
22
const fs = require('fs-extra');
3+
const path = require('path');
34
const PATHS = require('../../constants/paths');
45
const GRAMMAR_CONSTANTS = require('../../constants/grammars');
56
const shorthandIdentToLongHandPropertyMap = require('../../constants/shorthandIdentToLonghandPropertyMap.json');
@@ -36,7 +37,7 @@ module.exports = class NearleyGrammarFormatter {
3637
._getGrammarsToResolve(jsonGrammar)
3738
.map(fileToResolve => [
3839
fileToResolve,
39-
fs.readJsonSync(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}${fileToResolve}.json`),
40+
fs.readJsonSync(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, `${fileToResolve}.json`)),
4041
])
4142
.filter(([, json]) => NearleyGrammarFormatter._isGrammarValid(json))
4243
.map(([grammarName, jsonGrammar]) => (
@@ -122,7 +123,7 @@ module.exports = class NearleyGrammarFormatter {
122123

123124
return [...new Set(resolutions.concat(
124125
...resolutions
125-
.map(file => fs.readJsonSync(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}${file}.json`))
126+
.map(file => fs.readJsonSync(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, `${file}.json`)))
126127
.map(grammar => NearleyGrammarFormatter._getGrammarsToResolve(grammar, resolved.concat(resolutions)))))];
127128
}
128129

src/getShorthandComputedProperties.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const properties = require('../formatted-data/properties.json');
1+
const properties = require('./formatted-data/properties.json');
22

33
/**
44
* Given a shorthand property, returns an array of the computed properties for that shorthand property. If given

src/getShorthandsForProperty.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
const shortHandProperties = require('../formatted-data/shorthand-properties.json');
2-
const properties = require('../formatted-data/properties.json');
1+
const shortHandProperties = require('./formatted-data/shorthand-properties.json');
2+
const properties = require('./formatted-data/properties.json');
33

44
/**
55
* @type {Object}

src/initialValueMap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const properties = require('../formatted-data/properties.json');
1+
const properties = require('./formatted-data/properties.json');
22
const getShorthandComputedProperties = require('./getShorthandComputedProperties');
33
const isShorthandProperty = require('./isShorthandProperty');
44

src/isShorthandProperty.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const shortHandProperties = require('../formatted-data/shorthand-properties.json');
1+
const shortHandProperties = require('./formatted-data/shorthand-properties.json');
22

33
/**
44
* Checks if a given property is a shorthand property

src/isValidDeclaration.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const nearley = require('nearley');
2-
const properties = require('../formatted-data/properties.json');
2+
const properties = require('./formatted-data/properties.json');
33
const { CSS } = require('./constants');
4+
const grammars = require('./grammars/generated');
45

56
/**
67
* Checks if the given property, value pair is valid.
@@ -16,7 +17,7 @@ module.exports = function isValidDeclaration(property, value) {
1617
return true;
1718
}
1819

19-
const propertyGrammar = require('./grammars/generated')[property];
20+
const propertyGrammar = grammars[property];
2021

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

src/scripts/extractProperties.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
* Writes the formatted data to FORMATTED_DATA_PATH.
44
*/
55
const fs = require('fs-extra');
6+
const path = require('path');
67
const { css: { properties } } = require('mdn-data');
78
const PATHS = require('../constants/paths');
89

910
const ALL_PROPERTIES_DATA_FILE_NAME = 'properties.json';
10-
const OUTPUT_FILE = `${PATHS.FORMATTED_DATA_PATH}${ALL_PROPERTIES_DATA_FILE_NAME}`;
11+
const OUTPUT_FILE = path.join(PATHS.FORMATTED_DATA_PATH, ALL_PROPERTIES_DATA_FILE_NAME);
1112
fs.writeJson(OUTPUT_FILE, properties, { spaces: 2 })
1213
.then(() => (
1314
console.log(`Successfully extracted properties to ${OUTPUT_FILE}`)

src/scripts/formatData.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Writes the formatted data to FORMATTED_DATA_PATH.
44
*/
55
const fs = require('fs-extra');
6+
const path = require('path');
67
const { css: { properties } } = require('mdn-data');
78
const ShorthandPropertyClassifierUtils = require('../utils/ShorthandPropertyClassifierUtils');
89
const PATHS = require('../constants/paths');
@@ -21,7 +22,8 @@ const formattedData = Object.entries(properties)
2122
// reduce it down to an object again so we can write it to a file
2223
.reduce((propertyMap, [property, data]) => Object.assign({ [property]: data }, propertyMap), {});
2324

24-
fs.writeJson(`${PATHS.FORMATTED_DATA_PATH}${SHORTHAND_FORMATTED_DATA_FILE_NAME}`, formattedData, { spaces: 2 })
25-
.then(() => (
26-
console.log(`Successfully formatted data to ${PATHS.FORMATTED_DATA_PATH}${SHORTHAND_FORMATTED_DATA_FILE_NAME}`)
27-
));
25+
fs.writeJson(path.join(PATHS.FORMATTED_DATA_PATH, SHORTHAND_FORMATTED_DATA_FILE_NAME), formattedData, { spaces: 2 })
26+
.then(() => {
27+
const fdPath = path.join(PATHS.FORMATTED_DATA_PATH, SHORTHAND_FORMATTED_DATA_FILE_NAME);
28+
console.log(`Successfully formatted data to ${fdPath}`);
29+
});

src/scripts/formatFormalSyntaxes.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Format each formal syntax into a json grammar
33
*/
44
const fs = require('fs-extra');
5+
const path = require('path');
56
const { css: { properties, syntaxes } } = require('mdn-data');
67
const { PATHS, SYNTAX_OVERRIDES } = require('../constants/index');
78
const JsonGrammarFormatter = require('../formatters/grammarFormatters/JsonGrammarFormatter');
@@ -30,18 +31,19 @@ const syntaxesSyntaxMap = Object.entries(syntaxes)
3031
const propertySyntaxMap = Object.entries(properties).reduce((syntaxMap, [propertyName, { syntax }]) => (
3132
Object.assign({ [propertyName]: syntax }, syntaxMap)
3233
), syntaxesSyntaxMap);
33-
const overridenPropertySyntaxMap = Object.assign(propertySyntaxMap, SYNTAX_OVERRIDES);
34+
const overriddenPropertySyntaxMap = Object.assign(propertySyntaxMap, SYNTAX_OVERRIDES);
3435

3536
// make the json grammar directory if needed
3637
if (!fs.existsSync(PATHS.GENERATED_JSON_GRAMMAR_PATH)) {
3738
fs.mkdirSync(PATHS.GENERATED_JSON_GRAMMAR_PATH);
3839
}
3940

40-
Object.entries(overridenPropertySyntaxMap)
41+
Object.entries(overriddenPropertySyntaxMap)
4142
// filter out any entries that we need to do manually
4243
.filter(([grammarName]) => !manualSyntaxes.includes(grammarName))
4344
.forEach(([grammarName, formalSyntax]) => {
44-
console.log(`creating ${PATHS.GENERATED_JSON_GRAMMAR_PATH}${grammarName}.json`);
45+
const filename = path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, `${grammarName}.json`);
46+
console.log(`creating ${filename}`);
4547
const jsonGrammar = JsonGrammarFormatter.format(formalSyntax);
46-
fs.writeJson(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}${grammarName}.json`, jsonGrammar, { spaces: 2 });
48+
fs.writeJson(filename, jsonGrammar, { spaces: 2 });
4749
});

src/scripts/formatGrammars.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,34 @@ if (!fs.existsSync(PATHS.GENERATED_JS_GRAMMAR_PATH)) {
2424
}
2525

2626
// move manual json grammars into generated folder for grammar resolution
27-
fs.readdirSync(`${PATHS.JSON_GRAMMAR_PATH}`)
28-
.forEach(fileName => (
29-
fs.copySync(`${PATHS.JSON_GRAMMAR_PATH}${fileName}`, `${PATHS.GENERATED_JSON_GRAMMAR_PATH}${fileName}`))
30-
);
27+
fs.readdirSync(PATHS.JSON_GRAMMAR_PATH)
28+
.forEach((fileName) => {
29+
const fullSrc = path.join(PATHS.JSON_GRAMMAR_PATH, fileName);
30+
const fullDest = path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, fileName);
31+
fs.copySync(fullSrc, fullDest);
32+
});
3133

3234
// read each json grammar and format it into an nearley grammar
3335
fs.readdirSync(PATHS.GENERATED_JSON_GRAMMAR_PATH)
34-
.map(fileName => [fileName, fs.readJsonSync(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}${fileName}`)])
36+
.map(fileName => [fileName, fs.readJsonSync(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, fileName))])
3537
.forEach(([fileName, jsonGrammar]) => {
3638
console.log(`creating ${fileName}`);
3739

3840
const grammarName = fileName.replace('.json', '');
3941
const nearleyGrammar = NearleyGrammarFormatter.format(jsonGrammar, grammarName);
40-
const fileToWrite = `${PATHS.GENERATED_NEARLEY_GRAMMAR_PATH}${fileName.replace('.json', `.${GRAMMAR_CONSTANTS.GRAMMAR_FILE_EXTENSION}`)}`;
42+
const fileToWrite = path.join(PATHS.GENERATED_NEARLEY_GRAMMAR_PATH, fileName.replace('.json', `.${GRAMMAR_CONSTANTS.GRAMMAR_FILE_EXTENSION}`));
4143

4244
fs.createFileSync(fileToWrite);
4345
fs.writeFileSync(fileToWrite, nearleyGrammar);
4446
});
4547

4648
// copy over overridden grammars
4749
fs.readdirSync(PATHS.NEARLEY_PROPERTY_GRAMMAR_PATH)
48-
.forEach(fileName => (
49-
fs.copySync(
50-
`${PATHS.NEARLEY_PROPERTY_GRAMMAR_PATH}${fileName}`,
51-
`${PATHS.GENERATED_NEARLEY_GRAMMAR_PATH}${fileName}`
52-
)
53-
));
50+
.forEach((fileName) => {
51+
const fullSrc = path.join(PATHS.NEARLEY_PROPERTY_GRAMMAR_PATH, fileName);
52+
const fullDest = path.join(PATHS.GENERATED_NEARLEY_GRAMMAR_PATH, fileName);
53+
fs.copySync(fullSrc, fullDest);
54+
});
5455

5556
console.log('...Successfully created nearley grammars...');
5657

@@ -65,19 +66,19 @@ const compilationCommands = fs.readdirSync(PATHS.GENERATED_NEARLEY_GRAMMAR_PATH)
6566
.map((fileName) => {
6667
const propName = fileName.replace(`.${GRAMMAR_CONSTANTS.GRAMMAR_FILE_EXTENSION}`, '');
6768
const jsFileName = `${propName}.${JAVASCRIPT_FILE_EXTENSION}`;
68-
const nearleyFilePath = JSON.stringify(`${PATHS.GENERATED_NEARLEY_GRAMMAR_PATH}${fileName}`);
69+
const nearleyFilePath = JSON.stringify(path.join(PATHS.GENERATED_NEARLEY_GRAMMAR_PATH, fileName));
6970
const jsFilePath = JSON.stringify(
7071
path.join(PATHS.GENERATED_JS_GRAMMAR_PATH, jsFileName)
7172
);
7273

7374
jsModules.push(` '${propName}': require('./js/${jsFileName}')`);
7475

75-
return `node ${PATHS.NEARLEY_BIN_ROOT}${NEARLEY_COMPILER_FILE_NAME} ${nearleyFilePath} > ${jsFilePath}`;
76+
return `node ${path.join(PATHS.NEARLEY_BIN_ROOT, NEARLEY_COMPILER_FILE_NAME)} ${nearleyFilePath} > ${jsFilePath}`;
7677
});
7778

7879
const jsExportsFile = `module.exports = {\n${jsModules.join(',\n')}\n}`;
7980

80-
fs.writeFileSync(path.join(PATHS.GENERATED_JS_GRAMMAR_PATH, '../index.js'), jsExportsFile);
81+
fs.writeFileSync(path.resolve(PATHS.GENERATED_JS_GRAMMAR_PATH, '..', 'index.js'), jsExportsFile);
8182

8283
compilationQueue.push(compilationCommands, (err) => {
8384
if (err) {

src/scripts/updateBasicDataUnits.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
const { css: { units } } = require('mdn-data');
77
const fs = require('fs-extra');
8+
const path = require('path');
89
const PATHS = require('../constants/paths');
910
const GRAMMAR_CONSTANTS = require('../constants/grammars');
1011

@@ -28,7 +29,7 @@ const unitToTypesMap = Object.entries(units).reduce((unitMap, [unit, { groups }]
2829

2930
Promise.all(
3031
Object.entries(unitToTypesMap).map(([fileName, unitList]) => (
31-
fs.writeJson(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}${fileName}.json`, [
32+
fs.writeJson(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, `${fileName}.json`), [
3233
[GRAMMAR_CONSTANTS.BASE_GRAMMAR_RULE_NAME, `( ${unitList.map(unit => `"${unit}"`).join(' | ')} )`],
3334
], { spaces: 2 })
3435
)))

test/InitialValuesTest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { assert } = require('chai');
2-
const properties = require('../formatted-data/properties.json');
2+
const properties = require('../src/formatted-data/properties.json');
33
const {
44
initialValue,
55
initialValues,

test/formatters/NearleyGrammarFormatterTest.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const sinon = require('sinon');
22
const fs = require('fs-extra');
3+
const path = require('path');
34
const { assert } = require('chai');
45
const PATHS = require('../../src/constants/paths');
56
const GRAMMAR_CONSTANTS = require('../../src/constants/grammars');
@@ -58,17 +59,17 @@ describe('NearleyGrammarFormatter#format', function () {
5859
describe('recursive cases', function () {
5960
it('should handle deeply recursive cases', function () {
6061
sandbox.stub(fs, 'readJsonSync')
61-
.withArgs(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}ra.json`)
62+
.withArgs(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, 'ra.json'))
6263
.returns([
6364
[GRAMMAR_CONSTANTS.BASE_GRAMMAR_RULE_NAME, '<rb>'],
6465
['<rb>'],
6566
])
66-
.withArgs(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}rb.json`)
67+
.withArgs(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, 'rb.json'))
6768
.returns([
6869
[GRAMMAR_CONSTANTS.BASE_GRAMMAR_RULE_NAME, '<rc>'],
6970
['<rc>'],
7071
])
71-
.withArgs(`${PATHS.GENERATED_JSON_GRAMMAR_PATH}rc.json`)
72+
.withArgs(path.join(PATHS.GENERATED_JSON_GRAMMAR_PATH, 'rc.json'))
7273
.returns([
7374
[GRAMMAR_CONSTANTS.BASE_GRAMMAR_RULE_NAME, '"d"'],
7475
]);

updateCSSData.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ function realpath()
1313
echo "$dir$base"
1414
}
1515
cd "$(dirname "$(realpath "$0")")";
16-
rm -rf formatted-data src/grammars/generated/json
17-
mkdir -p formatted-data src/grammars/generated/json
16+
rm -rf src/formatted-data src/grammars/generated/json
17+
mkdir -p src/formatted-data src/grammars/generated/json
1818
node ./node_modules/nearley/bin/nearleyc.js ./src/grammars/nearley/formalSyntax.ne > ./src/grammars/js/formalSyntax.js || exit 1
1919
node ./src/scripts/updateBasicDataUnits.js || exit 1
2020
node ./src/scripts/formatData.js || exit 1

0 commit comments

Comments
 (0)