Skip to content
This repository was archived by the owner on Oct 5, 2024. It is now read-only.

Commit 20a2d82

Browse files
authored
refuse to parse operators unless they are indented (#111)
1 parent 495b5ae commit 20a2d82

File tree

46 files changed

+3396
-344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+3396
-344
lines changed

Makefile

+2
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,12 @@ test: build
3838
.PHONY: format
3939
format:
4040
npx elm-format $(FORMAT_DIRS) --yes
41+
npx xo --fix
4142

4243
.PHONY: lint
4344
lint:
4445
npx elm-format $(FORMAT_DIRS) --validate
46+
npx xo
4547

4648
.PHONY: readme_lib
4749
readme_lib:

cli/index.js

+70-72
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,79 @@
1-
const fs = require('fs').promises; // needs Node.JS v10+
1+
const fs = require('fs').promises; // Needs Node.JS v10+
22

3-
const { Elm } = require('../build/elm.js'); // build using Makefile... no Webpack around here!
4-
const { registerPort } = require('./utils.js');
3+
const {Elm} = require('../build/elm.js'); // Build using Makefile... no Webpack around here!
4+
const {registerPort} = require('./utils.js');
55

66
// Async/await is nice! (needs Node.JS v7.6+)
77
(async function () {
8+
// Process command line arguments
9+
const yargs = require('yargs');
10+
const argv = yargs
11+
.option('main', {
12+
alias: 'm',
13+
description: 'The main Elm file',
14+
type: 'string',
15+
demandOption: true
16+
})
17+
.option('output', {
18+
alias: 'o',
19+
description: 'The format to emit: JavaScript or JSON',
20+
type: 'string',
21+
default: 'JavaScript'
22+
})
23+
.help()
24+
.alias('help', 'h')
25+
.argv;
826

9-
// process command line arguments
10-
const yargs = require('yargs')
11-
const argv = yargs
12-
.option('main', {
13-
alias: 'm',
14-
description: 'The main Elm file',
15-
type: 'string',
16-
demandOption: true
17-
})
18-
.option('output', {
19-
alias: 'o',
20-
description: 'The format to emit: JavaScript or JSON',
21-
type: 'string',
22-
default: 'JavaScript'
23-
})
24-
.help()
25-
.alias('help', 'h')
26-
.argv;
27+
console.log('---------------------------');
28+
console.log('-- STARTING THE COMPILER --');
29+
console.log('---------------------------');
2730

28-
console.log('---------------------------');
29-
console.log('-- STARTING THE COMPILER --');
30-
console.log('---------------------------');
31+
const mainFileContents = await fs.readFile(argv.main, {encoding: 'utf8'});
32+
const elmJson = await fs.readFile('./elm.json', {encoding: 'utf8'});
3133

32-
const mainFileContents = await fs.readFile(argv.main, { encoding: 'utf8' });
33-
const elmJson = await fs.readFile('./elm.json', { encoding: 'utf8' });
34-
35-
const app = Elm.Main.init({
36-
flags: {
37-
mainFilePath: argv.main,
38-
mainFileContents,
39-
elmJson,
40-
outputFormat: argv.output
41-
}
42-
});
43-
44-
registerPort(app, 'stdout', string => process.stdout.write(string));
45-
registerPort(app, 'stderr', string => {
46-
process.stderr.write('\n---------------------------');
47-
process.stderr.write('\n-- COMPILER ERROR ---------');
48-
process.stderr.write('\n---------------------------');
49-
process.stderr.write(`\n${string}`);
50-
});
51-
registerPort(app, 'read', async function ({moduleName, filePath}) {
52-
try {
53-
const fileContents = await fs.readFile(filePath, { encoding: 'utf8' });
54-
app.ports.readSubscription.send({
55-
filePath,
56-
fileContents,
57-
});
58-
} catch (e) {
59-
if (e.code != null) {
60-
app.ports.readErrorSubscription.send({
61-
moduleName,
62-
filePath,
63-
errorCode: e.code,
64-
});
65-
}
66-
}
67-
});
68-
registerPort(app, 'writeToFile', async function ({ filePath, fileContents }) {
69-
setTimeout(() => {
70-
process.stdout.write('---------------------------\n');
71-
process.stdout.write('-- WRITING TO FS ----------\n');
72-
process.stdout.write('---------------------------\n');
73-
process.stdout.write(`${fileContents}\n`);
74-
}, 0);
75-
await fs.writeFile(filePath, fileContents);
76-
});
77-
registerPort(app, 'setExitCode', code => {
78-
process.exitCode = code;
79-
});
34+
const app = Elm.Main.init({
35+
flags: {
36+
mainFilePath: argv.main,
37+
mainFileContents,
38+
elmJson,
39+
outputFormat: argv.output
40+
}
41+
});
8042

43+
registerPort(app, 'stdout', string => process.stdout.write(string));
44+
registerPort(app, 'stderr', string => {
45+
process.stderr.write('\n---------------------------');
46+
process.stderr.write('\n-- COMPILER ERROR ---------');
47+
process.stderr.write('\n---------------------------');
48+
process.stderr.write(`\n${string}`);
49+
});
50+
registerPort(app, 'read', async ({moduleName, filePath}) => {
51+
try {
52+
const fileContents = await fs.readFile(filePath, {encoding: 'utf8'});
53+
app.ports.readSubscription.send({
54+
filePath,
55+
fileContents
56+
});
57+
} catch (error) {
58+
if (error.code !== null && error.code !== undefined) {
59+
app.ports.readErrorSubscription.send({
60+
moduleName,
61+
filePath,
62+
errorCode: error.code
63+
});
64+
}
65+
}
66+
});
67+
registerPort(app, 'writeToFile', async ({filePath, fileContents}) => {
68+
setTimeout(() => {
69+
process.stdout.write('---------------------------\n');
70+
process.stdout.write('-- WRITING TO FS ----------\n');
71+
process.stdout.write('---------------------------\n');
72+
process.stdout.write(`${fileContents}\n`);
73+
}, 0);
74+
await fs.writeFile(filePath, fileContents);
75+
});
76+
registerPort(app, 'setExitCode', code => {
77+
process.exitCode = code;
78+
});
8179
})();

cli/utils.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const registerPort = (app, portName, callback) => {
2-
if (app.ports && app.ports[portName]) {
3-
app.ports[portName].subscribe(callback);
4-
} else {
5-
console.error(`Tried to register an Elm port callback but failed: ${portName}`);
6-
process.exit(1);
7-
}
2+
if (app.ports && app.ports[portName]) {
3+
app.ports[portName].subscribe(callback);
4+
} else {
5+
console.error(`Tried to register an Elm port callback but failed: ${portName}`);
6+
process.exit(1);
7+
}
88
};
99

1010
module.exports = {
11-
registerPort
11+
registerPort
1212
};

integration-tests/cli/non-zero-exit-error/test.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
'cli: exit with non-zero exit code on compile error',
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async (program, t) => {
10-
const snapshot = await t.throwsAsync(program);
11-
t.is(snapshot.code, 1);
12-
return { snapshot };
13-
}
5+
'cli: exit with non-zero exit code on compile error',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await t.throwsAsync(program);
11+
t.is(snapshot.code, 1);
12+
await t.context.cliSnapshot(snapshot);
13+
}
1414
);

integration-tests/cli/two-source/test.test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
'cli: supports multiple source directories',
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async program => ({snapshot: await program})
5+
'cli: supports multiple source directories',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await program;
11+
await t.context.cliSnapshot(snapshot);
12+
}
1013
);

integration-tests/desugar/import/test.test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
'desugar: able to import variable from module',
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async program => ({snapshot: await program})
5+
'desugar: able to import variable from module',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await program;
11+
await t.context.cliSnapshot(snapshot);
12+
}
1013
);

integration-tests/desugar/type-annotation-check-varname-err-not-same/test.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
"desugar: type annotation var name doesn't agree with the declaration var name",
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async (program, t) => {
10-
const snapshot = await t.throwsAsync(program);
11-
t.is(snapshot.code, 1);
12-
return {snapshot};
13-
}
5+
'desugar: type annotation var name doesn\'t agree with the declaration var name',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await t.throwsAsync(program);
11+
t.is(snapshot.code, 1);
12+
await t.context.cliSnapshot(snapshot);
13+
}
1414
);

integration-tests/desugar/type-annotation-check-varname-ok/test.test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
'desugar: type annotation var name agrees with the declaration var name',
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async program => ({snapshot: await program})
5+
'desugar: type annotation var name agrees with the declaration var name',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await program;
11+
await t.context.cliSnapshot(snapshot);
12+
}
1013
);

integration-tests/desugar/type-annotation-qualified-type-err-module-not-found/test.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
"desugar: module for the qualified type in the type annotation is not found",
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async (program, t) => {
10-
const snapshot = await t.throwsAsync(program);
11-
t.is(snapshot.code, 1);
12-
return {snapshot};
13-
}
5+
'desugar: module for the qualified type in the type annotation is not found',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await t.throwsAsync(program);
11+
t.is(snapshot.code, 1);
12+
await t.context.cliSnapshot(snapshot);
13+
}
1414
);

integration-tests/desugar/type-annotation-qualified-type-err-type-not-found/test.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
"desugar: qualified type in the type annotation is not found in the imported module",
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async (program, t) => {
10-
const snapshot = await t.throwsAsync(program);
11-
t.is(snapshot.code, 1);
12-
return {snapshot};
13-
}
5+
'desugar: qualified type in the type annotation is not found in the imported module',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await t.throwsAsync(program);
11+
t.is(snapshot.code, 1);
12+
await t.context.cliSnapshot(snapshot);
13+
}
1414
);

integration-tests/desugar/type-annotation-qualified-type-ok/test.test.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
"desugar: qualified type in the type annotation is found and agrees with the declaration",
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async program => ({snapshot: await program})
5+
'desugar: qualified type in the type annotation is found and agrees with the declaration',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await program;
11+
await t.context.cliSnapshot(snapshot);
12+
}
1013
);

integration-tests/desugar/type-annotation-unqualified-type-err-imported-type-not-found/test.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
"desugar: imported unqualified type in the type annotation is not found",
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async (program, t) => {
10-
const snapshot = await t.throwsAsync(program);
11-
t.is(snapshot.code, 1);
12-
return {snapshot};
13-
}
5+
'desugar: imported unqualified type in the type annotation is not found',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await t.throwsAsync(program);
11+
t.is(snapshot.code, 1);
12+
await t.context.cliSnapshot(snapshot);
13+
}
1414
);

integration-tests/desugar/type-annotation-unqualified-type-err-local-type-not-found/test.test.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ const test = require('ava');
22
const {exec} = require('../../setup');
33

44
test(
5-
"desugar: local unqualified type in the type annotation is not found",
6-
exec,
7-
__dirname,
8-
["-m", "src/Main.elm"],
9-
async (program, t) => {
10-
const snapshot = await t.throwsAsync(program);
11-
t.is(snapshot.code, 1);
12-
return {snapshot};
13-
}
5+
'desugar: local unqualified type in the type annotation is not found',
6+
exec,
7+
__dirname,
8+
['-m', 'src/Main.elm'],
9+
async (program, t) => {
10+
const snapshot = await t.throwsAsync(program);
11+
t.is(snapshot.code, 1);
12+
await t.context.cliSnapshot(snapshot);
13+
}
1414
);

0 commit comments

Comments
 (0)