Skip to content

Commit 6340124

Browse files
committed
refactor(style): add indent option
1 parent ce64163 commit 6340124

File tree

7 files changed

+58
-42
lines changed

7 files changed

+58
-42
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
# Unix-style newlines with a newline ending every file
4+
[*]
5+
end_of_line = lf
6+
insert_final_newline = true
7+
8+
# Matches multiple files with brace expansion notation
9+
# Set default charset
10+
[*.{js}]
11+
charset = utf-8
12+
indent_style = space
13+
indent_size = 4
14+
15+
# Matches the exact files either package.json or .travis.yml
16+
[{package.json,.travis.yml}]
17+
indent_style = space
18+
indent_size = 2

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ module.exports = {
6969
"id-blacklist": "error",
7070
"id-length": "off",
7171
"id-match": "error",
72-
"indent": "off",
72+
"indent": ["error", 4],
7373
"init-declarations": "off",
7474
"jsx-quotes": "error",
7575
"key-spacing": "off",

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,4 @@ crashlytics.properties
8282
crashlytics-build.properties
8383

8484

85+
/.eslintcache

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ sudo: false
22
language: node_js
33
node_js: "stable"
44
script:
5-
- npm run check
65
- npm test
76
- npm run test:example-default
87
- npm run test:example-ace

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
},
2525
"scripts": {
2626
"build": "NODE_ENV=production babel src --out-dir lib --source-maps",
27-
"check": "eslint src/*.js test/*.js",
28-
"lint:fix": "eslint --fix src/*.js test/*.js",
27+
"lint": "eslint --cache src/*.js test/*.js",
28+
"lint:fix": "eslint --cache --fix src/*.js test/*.js",
2929
"watch": "babel src --out-dir lib --watch --source-maps",
3030
"prepublish": "npm run --if-present build",
31-
"test": "mocha",
31+
"test": "npm run lint && mocha",
3232
"test:example-default": "cd examples/default && npm i && npm run build",
3333
"test:example-ace": "cd examples/ace && npm i && npm run build",
3434
"test:example-custom": "cd examples/custom && npm i && npm run build"

src/parser.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ export function splitLabelToCommands(label = "") {
4545
* @return {string}
4646
*/
4747
export function strip(s) {
48-
// inspired from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/strip.rb
49-
if((s === undefined) || (s === "")) {
50-
return s;
51-
}
52-
const indents = s.split(/\n/).map(s => s.match(/^[ \t]*(?=\S)/)).filter(m => m).map(m => m[0]);
53-
const smallestIndent = indents.sort((a,b) => a.length - b.length)[0];
54-
return s.replace(new RegExp(`^${smallestIndent}`, "gm"), "");
48+
// inspired from https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/strip.rb
49+
if ((s === undefined) || (s === "")) {
50+
return s;
51+
}
52+
const indents = s.split(/\n/).map(s => s.match(/^[ \t]*(?=\S)/)).filter(m => m).map(m => m[0]);
53+
const smallestIndent = indents.sort((a, b) => a.length - b.length)[0];
54+
return s.replace(new RegExp(`^${smallestIndent}`, "gm"), "");
5555
}
5656

5757
/**
@@ -73,8 +73,8 @@ export function containIncludeCommand(commands = []) {
7373
* @param {string} label
7474
* @return {object}
7575
*/
76-
export function parseVariablesFromLabel(kvMap,label) {
77-
const kv = Object.assign({},kvMap);
76+
export function parseVariablesFromLabel(kvMap, label) {
77+
const kv = Object.assign({}, kvMap);
7878
const beginEx = "\^.*";
7979
const endEx = ".*\$";
8080
const sepEx = ",?";
@@ -90,18 +90,17 @@ export function parseVariablesFromLabel(kvMap,label) {
9090
valEx = "(([-\\w\\s]*,?)*)";
9191
}
9292
// Add value check here
93-
switch(typeof defaultKeyValueMap[key]) {
94-
case "string":
95-
valEx = quotesEx + valEx + quotesEx;
96-
break;
97-
case "boolean":
93+
switch (typeof defaultKeyValueMap[key]) {
94+
case "string":
95+
valEx = quotesEx + valEx + quotesEx;
96+
break;
97+
case "boolean":
9898
// no quotes
99-
valEx = quotesEx + "?(true|false)" + quotesEx + "?";
100-
break;
101-
default:
102-
logger.error("include-codeblock: parseVariablesFromLabel: key type `" +
103-
typeof defaultKeyValueMap[key] + "` unknown (see options.js)");
104-
break;
99+
valEx = quotesEx + "?(true|false)" + quotesEx + "?";
100+
break;
101+
default:
102+
logger.error("include-codeblock: parseVariablesFromLabel: key type `" + typeof defaultKeyValueMap[key] + "` unknown (see options.js)");
103+
break;
105104
}
106105
// Val type cast to string.
107106
const regStr = beginEx + sepEx + spacesEx + keyEx +
@@ -123,18 +122,17 @@ export function parseVariablesFromLabel(kvMap,label) {
123122
* @param {string} content
124123
* @return {string}
125124
*/
126-
export function generateEmbedCode(
127-
kvMap,
128-
{fileName, originalPath, content}){
125+
export function generateEmbedCode(kvMap,
126+
{fileName, originalPath, content}) {
129127
const tContent = getTemplateContent(kvMap);
130-
const kv = Object.assign({},kvMap);
128+
const kv = Object.assign({}, kvMap);
131129
const count = hasTitle(kv) ? codeCounter() : -1;
132130
checkMapTypes(kvMap, "generatedEmbedCode");
133-
const contextMap = Object.assign({},kvMap,{
134-
"content":content,
135-
"count":count,
136-
"fileName":fileName,
137-
"originalPath":originalPath
131+
const contextMap = Object.assign({}, kvMap, {
132+
"content": content,
133+
"count": count,
134+
"fileName": fileName,
135+
"originalPath": originalPath
138136
});
139137
// compile template
140138
const handlebars = Handlebars.compile(tContent);
@@ -148,8 +146,8 @@ export function generateEmbedCode(
148146
* @param {string} originalPath
149147
* @return {string}
150148
*/
151-
export function getContent(filePath,originalPath){
152-
return readFileFromPath(filePath);
149+
export function getContent(filePath, originalPath) {
150+
return readFileFromPath(filePath);
153151
}
154152

155153
/**
@@ -161,7 +159,7 @@ export function getContent(filePath,originalPath){
161159
* @return {string}
162160
*/
163161
export function embedCode(kvMap,
164-
{filePath, originalPath, label}){
162+
{filePath, originalPath, label}) {
165163
const code = getContent(filePath, originalPath);
166164
const fileName = path.basename(filePath);
167165
const kvmparsed = parseVariablesFromLabel(kvMap, label);
@@ -206,10 +204,10 @@ export function parse(content, baseDir, options = {}) {
206204
const replacedContent = embedCode(
207205
kvMap,
208206
{
209-
filePath: absolutePath,
210-
originalPath: originalPath,
211-
label
212-
});
207+
filePath: absolutePath,
208+
originalPath: originalPath,
209+
label
210+
});
213211
results.push({
214212
target: all,
215213
replaced: replacedContent

src/template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {defaultBookOptionsMap, defaultTemplateMap} from "./options.js";
1212
export function readFileFromPath(path){
1313
var content;
1414
try {
15-
content = fs.readFileSync(path, "utf8");
15+
content = fs.readFileSync(path, "utf8");
1616
} catch (err) {
1717
if (err.code === "ENOENT") {
1818
logger.warn("Error: file not found: " + path);

0 commit comments

Comments
 (0)