瀏覽代碼

Run prettier

master
Dylan Baker 6 年之前
父節點
當前提交
d46f25c589
共有 6 個檔案被更改,包括 101 行新增92 行删除
  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 查看文件

14
       output = this.file(path.join(process.cwd(), opts.f))
14
       output = this.file(path.join(process.cwd(), opts.f))
15
     } else if (opts.d) {
15
     } else if (opts.d) {
16
       if (!opts.o) {
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
         process.exit()
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
     } else if (opts.e) {
26
     } else if (opts.e) {
22
       output = this.inline(opts.e)
27
       output = this.inline(opts.e)
23
     } else {
28
     } else {
28
     if (opts.o) {
33
     if (opts.o) {
29
       const absoluteOutputPath = this.absolutePath(opts.o)
34
       const absoluteOutputPath = this.absolutePath(opts.o)
30
       if (opts.f || opts.e) {
35
       if (opts.f || opts.e) {
31
-        fs.writeFile(absoluteOutputPath, output, function (err) {
36
+        fs.writeFile(absoluteOutputPath, output, function(err) {
32
           if (err) throw err
37
           if (err) throw err
33
         })
38
         })
34
       } else if (opts.d) {
39
       } else if (opts.d) {
35
         const absoluteInputPath = this.absolutePath(opts.d)
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
           const parentDirectory = path.dirname(absoluteOutputFilePath)
45
           const parentDirectory = path.dirname(absoluteOutputFilePath)
39
           if (!fs.existsSync(parentDirectory)) {
46
           if (!fs.existsSync(parentDirectory)) {
40
             fs.mkdirSync(parentDirectory)
47
             fs.mkdirSync(parentDirectory)
41
           }
48
           }
42
-          fs.writeFile(absoluteOutputFilePath, output[file], function (err) {
49
+          fs.writeFile(absoluteOutputFilePath, output[file], function(err) {
43
             if (err) throw err
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
 
92
 
84
   printUsage() {
93
   printUsage() {
85
     console.log(
94
     console.log(
86
-`USAGE
95
+      `USAGE
87
 
96
 
88
   -d The path to a directory. The directory will be recursively scanned. An
97
   -d The path to a directory. The directory will be recursively scanned. An
89
      output directory is required with this option.
98
      output directory is required with this option.
104
      will be created if it does not already exist.
113
      will be created if it does not already exist.
105
 
114
 
106
       oslo -f index.oslo -o index.html
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 查看文件

8
   compile() {
8
   compile() {
9
     this.tree.forEach(node => {
9
     this.tree.forEach(node => {
10
       if (node.type === 'functionCall') {
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
         const compiler = new Compiler(node.subtree, this.context)
17
         const compiler = new Compiler(node.subtree, this.context)
13
         const content = compiler.compile()
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
       } else if (node.type === 'string') {
22
       } else if (node.type === 'string') {
16
         this.result += node.content
23
         this.result += node.content
17
       } else if (node.type === 'identifier') {
24
       } else if (node.type === 'identifier') {

+ 9
- 4
src/parser.js 查看文件

23
     elementNode.args = []
23
     elementNode.args = []
24
     elementNode.subtree = []
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
       if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
30
       if (this.tokenStream.peek().type === tokenTypes.ATTRIBUTE) {
28
         elementNode.args.push(this.attribute())
31
         elementNode.args.push(this.attribute())
29
       } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
32
       } else if (this.tokenStream.peek().type === tokenTypes.OPAREN) {
42
 
45
 
43
   attribute() {
46
   attribute() {
44
     let attributeNode = new Node()
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
     if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
51
     if (this.tokenStream.peek().type === tokenTypes.QUOTE) {
47
       attributeNode.attributeValue = this.quotedString()
52
       attributeNode.attributeValue = this.quotedString()
48
     } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
53
     } else if (this.tokenStream.peek().type === tokenTypes.LITERAL) {
56
     const identifier = this.tokenStream.eat(tokenTypes.LITERAL)
61
     const identifier = this.tokenStream.eat(tokenTypes.LITERAL)
57
     return new Node({
62
     return new Node({
58
       type: 'identifier',
63
       type: 'identifier',
59
-      name: identifier.value
64
+      name: identifier.value,
60
     })
65
     })
61
   }
66
   }
62
 
67
 
63
   quotedString() {
68
   quotedString() {
64
     return new Node({
69
     return new Node({
65
       type: 'string',
70
       type: 'string',
66
-      content: this.string()
71
+      content: this.string(),
67
     })
72
     })
68
   }
73
   }
69
 
74
 

+ 11
- 11
test/compiler.js 查看文件

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
   t.plan(1)
10
   t.plan(1)
11
   const lexer = new Lexer()
11
   const lexer = new Lexer()
12
   const tokenStream = lexer.scan(`
12
   const tokenStream = lexer.scan(`
19
   const result = compiler.compile()
19
   const result = compiler.compile()
20
   t.deepEqual(
20
   t.deepEqual(
21
     result.replace(/\n/g, '').replace(/  +/g, ''),
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
   t.plan(1)
27
   t.plan(1)
28
   const lexer = new Lexer()
28
   const lexer = new Lexer()
29
   const tokenStream = lexer.scan(`
29
   const tokenStream = lexer.scan(`
35
   const compiler = new Compiler(tree, {
35
   const compiler = new Compiler(tree, {
36
     classOne: 'foobar',
36
     classOne: 'foobar',
37
     classTwo: 'bazquux',
37
     classTwo: 'bazquux',
38
-    bodyText: 'Lorem ipsum dolor sit amet.'
38
+    bodyText: 'Lorem ipsum dolor sit amet.',
39
   })
39
   })
40
   const result = compiler.compile()
40
   const result = compiler.compile()
41
   t.deepEqual(
41
   t.deepEqual(
42
     result.replace(/\n/g, '').replace(/  +/g, ''),
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 查看文件

45
   t.plan(2)
45
   t.plan(2)
46
   const lexer = new Lexer()
46
   const lexer = new Lexer()
47
   let tokens = lexer.scan(`(test test test)`).tokens
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
   tokens = lexer.scan(`(test "test" test test)`).tokens
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
 test('allow special characters inside quotes', t => {
70
 test('allow special characters inside quotes', t => {
79
   let tokens = lexer.scan(`
73
   let tokens = lexer.scan(`
80
     (p "(test)")
74
     (p "(test)")
81
   `).tokens
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
   t.equal(tokens[3].value, '(test)')
85
   t.equal(tokens[3].value, '(test)')
95
 })
86
 })

+ 25
- 28
test/parser.js 查看文件

15
   let parser = new Parser(tokenStream)
15
   let parser = new Parser(tokenStream)
16
   let tree = parser.parse()
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…
取消
儲存