Browse Source

Run prettier

master
Dylan Baker 5 years ago
parent
commit
d46f25c589
6 changed files with 101 additions and 92 deletions
  1. 19
    10
      bin/oslo.js
  2. 9
    2
      src/compiler.js
  3. 9
    4
      src/parser.js
  4. 11
    11
      test/compiler.js
  5. 28
    37
      test/lexer.js
  6. 25
    28
      test/parser.js

+ 19
- 10
bin/oslo.js View File

@@ -14,10 +14,15 @@ class OsloCLI {
14 14
       output = this.file(path.join(process.cwd(), opts.f))
15 15
     } else if (opts.d) {
16 16
       if (!opts.o) {
17
-        console.log('Output directory required. Invoke oslo with `-o path/to/output/directory`')
17
+        console.log(
18
+          'Output directory required. Invoke oslo with `-o path/to/output/directory`',
19
+        )
18 20
         process.exit()
19 21
       }
20
-      output = this.directory(this.absolutePath(opts.d), this.absolutePath(opts.o))
22
+      output = this.directory(
23
+        this.absolutePath(opts.d),
24
+        this.absolutePath(opts.o),
25
+      )
21 26
     } else if (opts.e) {
22 27
       output = this.inline(opts.e)
23 28
     } else {
@@ -28,20 +33,24 @@ class OsloCLI {
28 33
     if (opts.o) {
29 34
       const absoluteOutputPath = this.absolutePath(opts.o)
30 35
       if (opts.f || opts.e) {
31
-        fs.writeFile(absoluteOutputPath, output, function (err) {
36
+        fs.writeFile(absoluteOutputPath, output, function(err) {
32 37
           if (err) throw err
33 38
         })
34 39
       } else if (opts.d) {
35 40
         const absoluteInputPath = this.absolutePath(opts.d)
36
-        Object.keys(output).forEach(function (file) {
37
-          const absoluteOutputFilePath = file.replace(absoluteInputPath, absoluteOutputPath).replace(/\.oslo$/, '.html')
41
+        Object.keys(output).forEach(function(file) {
42
+          const absoluteOutputFilePath = file
43
+            .replace(absoluteInputPath, absoluteOutputPath)
44
+            .replace(/\.oslo$/, '.html')
38 45
           const parentDirectory = path.dirname(absoluteOutputFilePath)
39 46
           if (!fs.existsSync(parentDirectory)) {
40 47
             fs.mkdirSync(parentDirectory)
41 48
           }
42
-          fs.writeFile(absoluteOutputFilePath, output[file], function (err) {
49
+          fs.writeFile(absoluteOutputFilePath, output[file], function(err) {
43 50
             if (err) throw err
44
-            console.log(`Writing to ${absoluteOutputFilePath.replace(process.cwd(), '')}`)
51
+            console.log(
52
+              `Writing to ${absoluteOutputFilePath.replace(process.cwd(), '')}`,
53
+            )
45 54
           })
46 55
         })
47 56
       }
@@ -83,7 +92,7 @@ class OsloCLI {
83 92
 
84 93
   printUsage() {
85 94
     console.log(
86
-`USAGE
95
+      `USAGE
87 96
 
88 97
   -d The path to a directory. The directory will be recursively scanned. An
89 98
      output directory is required with this option.
@@ -104,9 +113,9 @@ class OsloCLI {
104 113
      will be created if it does not already exist.
105 114
 
106 115
       oslo -f index.oslo -o index.html
107
-      oslo -d templates -o html`
116
+      oslo -d templates -o html`,
108 117
     )
109 118
   }
110 119
 }
111 120
 
112
-(new OsloCLI(args))
121
+new OsloCLI(args)

+ 9
- 2
src/compiler.js View File

@@ -8,10 +8,17 @@ module.exports = class Compiler {
8 8
   compile() {
9 9
     this.tree.forEach(node => {
10 10
       if (node.type === 'functionCall') {
11
-        const attributes = node.args.map(arg => `${arg.attributeName}="${this.compileAttribute(arg.attributeValue)}"`)
11
+        const attributes = node.args.map(
12
+          arg =>
13
+            `${arg.attributeName}="${this.compileAttribute(
14
+              arg.attributeValue,
15
+            )}"`,
16
+        )
12 17
         const compiler = new Compiler(node.subtree, this.context)
13 18
         const content = compiler.compile()
14
-        this.result += `<${node.functionName}${attributes.length ? ' ' : ''}${attributes.join(' ')}>${content}</${node.functionName}>`
19
+        this.result += `<${node.functionName}${
20
+          attributes.length ? ' ' : ''
21
+        }${attributes.join(' ')}>${content}</${node.functionName}>`
15 22
       } else if (node.type === 'string') {
16 23
         this.result += node.content
17 24
       } else if (node.type === 'identifier') {

+ 9
- 4
src/parser.js View File

@@ -23,7 +23,10 @@ module.exports = class Parser {
23 23
     elementNode.args = []
24 24
     elementNode.subtree = []
25 25
 
26
-    while (this.tokenStream.peek().type != tokenTypes.CPAREN && this.tokenStream.peek().type !== tokenTypes.EOF) {
26
+    while (
27
+      this.tokenStream.peek().type != tokenTypes.CPAREN &&
28
+      this.tokenStream.peek().type !== tokenTypes.EOF
29
+    ) {
27 30
       if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
28 31
         elementNode.args.push(this.attribute())
29 32
       } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
@@ -42,7 +45,9 @@ module.exports = class Parser {
42 45
 
43 46
   attribute() {
44 47
     let attributeNode = new Node()
45
-    attributeNode.attributeName = this.tokenStream.eat(tokenTypes.ATTRIBUTE).value
48
+    attributeNode.attributeName = this.tokenStream.eat(
49
+      tokenTypes.ATTRIBUTE,
50
+    ).value
46 51
     if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
47 52
       attributeNode.attributeValue = this.quotedString()
48 53
     } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
@@ -56,14 +61,14 @@ module.exports = class Parser {
56 61
     const identifier = this.tokenStream.eat(tokenTypes.LITERAL)
57 62
     return new Node({
58 63
       type: 'identifier',
59
-      name: identifier.value
64
+      name: identifier.value,
60 65
     })
61 66
   }
62 67
 
63 68
   quotedString() {
64 69
     return new Node({
65 70
       type: 'string',
66
-      content: this.string()
71
+      content: this.string(),
67 72
     })
68 73
   }
69 74
 

+ 11
- 11
test/compiler.js View File

@@ -1,12 +1,12 @@
1
-const test = require("tape")
1
+const test = require('tape')
2 2
 
3
-const Compiler = require("../src/compiler")
4
-const Lexer = require("../src/lexer")
5
-const Node = require("../src/node")
6
-const Parser = require("../src/parser")
7
-const tt = require("../src/tokenTypes")
3
+const Compiler = require('../src/compiler')
4
+const Lexer = require('../src/lexer')
5
+const Node = require('../src/node')
6
+const Parser = require('../src/parser')
7
+const tt = require('../src/tokenTypes')
8 8
 
9
-test("compiles a simple template", t => {
9
+test('compiles a simple template', t => {
10 10
   t.plan(1)
11 11
   const lexer = new Lexer()
12 12
   const tokenStream = lexer.scan(`
@@ -19,11 +19,11 @@ test("compiles a simple template", t => {
19 19
   const result = compiler.compile()
20 20
   t.deepEqual(
21 21
     result.replace(/\n/g, '').replace(/  +/g, ''),
22
-    '<div class="foobar"><p class="bazquux">Lorem ipsum dolor sit amet.</p></div>'
22
+    '<div class="foobar"><p class="bazquux">Lorem ipsum dolor sit amet.</p></div>',
23 23
   )
24 24
 })
25 25
 
26
-test("renders variables according to passed-in context", t => {
26
+test('renders variables according to passed-in context', t => {
27 27
   t.plan(1)
28 28
   const lexer = new Lexer()
29 29
   const tokenStream = lexer.scan(`
@@ -35,11 +35,11 @@ test("renders variables according to passed-in context", t => {
35 35
   const compiler = new Compiler(tree, {
36 36
     classOne: 'foobar',
37 37
     classTwo: 'bazquux',
38
-    bodyText: 'Lorem ipsum dolor sit amet.'
38
+    bodyText: 'Lorem ipsum dolor sit amet.',
39 39
   })
40 40
   const result = compiler.compile()
41 41
   t.deepEqual(
42 42
     result.replace(/\n/g, '').replace(/  +/g, ''),
43
-    '<div class="foobar"><p class="bazquux">Lorem ipsum dolor sit amet.</p></div>'
43
+    '<div class="foobar"><p class="bazquux">Lorem ipsum dolor sit amet.</p></div>',
44 44
   )
45 45
 })

+ 28
- 37
test/lexer.js View File

@@ -45,32 +45,26 @@ test('multiple identifiers in a row are kept separate', t => {
45 45
   t.plan(2)
46 46
   const lexer = new Lexer()
47 47
   let tokens = lexer.scan(`(test test test)`).tokens
48
-  t.deepEqual(
49
-    tokens.map(token => token.type),
50
-    [
51
-      tt.OPAREN,
52
-      tt.LITERAL,
53
-      tt.LITERAL,
54
-      tt.LITERAL,
55
-      tt.CPAREN,
56
-      tt.EOF,
57
-    ]
58
-  )
48
+  t.deepEqual(tokens.map(token => token.type), [
49
+    tt.OPAREN,
50
+    tt.LITERAL,
51
+    tt.LITERAL,
52
+    tt.LITERAL,
53
+    tt.CPAREN,
54
+    tt.EOF,
55
+  ])
59 56
   tokens = lexer.scan(`(test "test" test test)`).tokens
60
-  t.deepEqual(
61
-    tokens.map(token => token.type),
62
-    [
63
-      tt.OPAREN,
64
-      tt.LITERAL,
65
-      tt.QUOTE,
66
-      tt.LITERAL,
67
-      tt.QUOTE,
68
-      tt.LITERAL,
69
-      tt.LITERAL,
70
-      tt.CPAREN,
71
-      tt.EOF,
72
-    ]
73
-  )
57
+  t.deepEqual(tokens.map(token => token.type), [
58
+    tt.OPAREN,
59
+    tt.LITERAL,
60
+    tt.QUOTE,
61
+    tt.LITERAL,
62
+    tt.QUOTE,
63
+    tt.LITERAL,
64
+    tt.LITERAL,
65
+    tt.CPAREN,
66
+    tt.EOF,
67
+  ])
74 68
 })
75 69
 
76 70
 test('allow special characters inside quotes', t => {
@@ -79,17 +73,14 @@ test('allow special characters inside quotes', t => {
79 73
   let tokens = lexer.scan(`
80 74
     (p "(test)")
81 75
   `).tokens
82
-  t.deepEqual(
83
-    tokens.map(token => token.type),
84
-    [
85
-      tt.OPAREN,
86
-      tt.LITERAL,
87
-      tt.QUOTE,
88
-      tt.LITERAL,
89
-      tt.QUOTE,
90
-      tt.CPAREN,
91
-      tt.EOF,
92
-    ]
93
-  )
76
+  t.deepEqual(tokens.map(token => token.type), [
77
+    tt.OPAREN,
78
+    tt.LITERAL,
79
+    tt.QUOTE,
80
+    tt.LITERAL,
81
+    tt.QUOTE,
82
+    tt.CPAREN,
83
+    tt.EOF,
84
+  ])
94 85
   t.equal(tokens[3].value, '(test)')
95 86
 })

+ 25
- 28
test/parser.js View File

@@ -15,32 +15,29 @@ test('parses token stream into a tree', t => {
15 15
   let parser = new Parser(tokenStream)
16 16
   let tree = parser.parse()
17 17
 
18
-  t.deepEqual(
19
-    tree,
20
-    [
21
-      new Node({
22
-        type: 'functionCall',
23
-        functionName: 'div',
24
-        args: [
25
-          new Node({
26
-            attributeName: 'class',
27
-            attributeValue: new Node({type: 'string', content: 'foobar'}),
28
-          })
29
-        ],
30
-        subtree: [
31
-          new Node({
32
-            type: 'functionCall',
33
-            functionName: 'p',
34
-            args: [
35
-              new Node({
36
-                attributeName: 'class',
37
-                attributeValue: new Node({type: 'string', content: 'bazquux'}),
38
-              })
39
-            ],
40
-            subtree: [],
41
-          })
42
-        ]
43
-      })
44
-    ]
45
-  )
18
+  t.deepEqual(tree, [
19
+    new Node({
20
+      type: 'functionCall',
21
+      functionName: 'div',
22
+      args: [
23
+        new Node({
24
+          attributeName: 'class',
25
+          attributeValue: new Node({ type: 'string', content: 'foobar' }),
26
+        }),
27
+      ],
28
+      subtree: [
29
+        new Node({
30
+          type: 'functionCall',
31
+          functionName: 'p',
32
+          args: [
33
+            new Node({
34
+              attributeName: 'class',
35
+              attributeValue: new Node({ type: 'string', content: 'bazquux' }),
36
+            }),
37
+          ],
38
+          subtree: [],
39
+        }),
40
+      ],
41
+    }),
42
+  ])
46 43
 })

Loading…
Cancel
Save