Skip to content
This repository was archived by the owner on Sep 6, 2018. It is now read-only.

Commit a05132a

Browse files
author
Michael Hadley
committed
Read and parse sequentially, so that soy will always fail first, fixes #1
1 parent 488a072 commit a05132a

File tree

6 files changed

+39
-8
lines changed

6 files changed

+39
-8
lines changed

src/__tests__/__snapshots__/validate-file.js.snap

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Validate file should fail; invalid namespace 1`] = `
4+
Array [
5+
"Failed to parse soy template",
6+
]
7+
`;
8+
39
exports[`Validate file should fail; missing import 1`] = `
410
Array [
511
"It looks like the following component calls are missing an import:

src/__tests__/validate-file.js

+5
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ describe('Validate file', () => {
2929
expect(result.status).toBe(false);
3030
expect(result.messages).toMatchSnapshot();
3131
}));
32+
33+
test('should fail; invalid namespace', () => validate('InvalidNamespace.soy').then(result => {
34+
expect(result.status).toBe(false);
35+
expect(result.messages).toMatchSnapshot();
36+
}));
3237
});

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
const chalk = require('chalk');
55
const pkg = require('../package.json');
66
const program = require('commander');
7+
const Promise = require('bluebird');
78
const validateFile = require('./validate-file');
89

910
function main() {
@@ -17,7 +18,7 @@ function main() {
1718
}
1819

1920
Promise
20-
.all(program.args.map(validate))
21+
.map(program.args, validate)
2122
.then(validations => {
2223
const failed = validations.filter(validation => !validation.result.status);
2324

src/util.js

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const chalk = require('chalk');
2+
const Promise = require('bluebird');
23

34
function combineResults(first, second) {
45
return toResult(
@@ -36,6 +37,13 @@ function parseTemplateName(rawName) {
3637
};
3738
}
3839

40+
function sequence(...tasks) {
41+
return Promise.reduce(tasks, (res, next) =>
42+
next().then(value => [...res, value]),
43+
[]
44+
);
45+
}
46+
3947
function joinErrors(lines) {
4048
return lines
4149
.map(line => chalk.red(line))
@@ -47,5 +55,6 @@ module.exports = {
4755
difference,
4856
joinErrors,
4957
parseTemplateName,
58+
sequence,
5059
toResult
5160
};

src/validate-file.js

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = Promise.promisifyAll(require('fs'));
44
const babylon = require('babylon');
55
const parseSoy = require('./soy-parser');
66
const path = require('path');
7-
const {combineResults, toResult} = require('./util');
7+
const {combineResults, sequence, toResult} = require('./util');
88

99
/* Error Types */
1010

@@ -68,14 +68,10 @@ function runValidations(soyAst, jsAst) {
6868
}
6969

7070
module.exports = function validateFile(filePath) {
71-
return Promise
72-
.all([
73-
getSoyAst(filePath),
74-
getJSAst(getJSPath(filePath))
75-
])
71+
return sequence(() => getSoyAst(filePath), () => getJSAst(getJSPath(filePath)))
7672
.then(([soyAst, jsAst]) => runValidations(soyAst, jsAst))
7773
.catch(error => {
78-
switch(error.type) {
74+
switch (error.type) {
7975
case ERR_JS_READ:
8076
return toResult(true);
8177
case ERR_JS_PARSE:

test/fixtures/InvalidNamespace.soy

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{namespace Test}
2+
3+
/**
4+
* Test
5+
*/
6+
{template .render}
7+
{@param name: string}
8+
{@param title: string}
9+
10+
<div>
11+
<h1>{$title}</h1>
12+
<h2>{$name}</h2>
13+
</div>
14+
{/template}

0 commit comments

Comments
 (0)