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);

0 commit comments

Comments
 (0)