• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3require('../common');
4const assert = require('assert');
5const { spawnSync } = require('child_process');
6
7const node = process.execPath;
8
9// Test both sets of arguments that check syntax
10const syntaxArgs = [
11  '-c',
12  '--check',
13];
14
15// Match on the name of the `Error` but not the message as it is different
16// depending on the JavaScript engine.
17const syntaxErrorRE = /^SyntaxError: Unexpected identifier\b/m;
18
19// Should throw if code piped from stdin with --check has bad syntax
20// loop each possible option, `-c` or `--check`
21syntaxArgs.forEach(function(arg) {
22  const stdin = 'var foo bar;';
23  const c = spawnSync(node, [arg], { encoding: 'utf8', input: stdin });
24
25  // stderr should include '[stdin]' as the filename
26  assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
27
28  // No stdout should be produced
29  assert.strictEqual(c.stdout, '');
30
31  // stderr should have a syntax error message
32  assert(syntaxErrorRE.test(c.stderr), `${syntaxErrorRE} === ${c.stderr}`);
33
34  assert.strictEqual(c.status, 1);
35});
36
37// Check --input-type=module
38syntaxArgs.forEach(function(arg) {
39  const stdin = 'export var p = 5; var foo bar;';
40  const c = spawnSync(
41    node,
42    ['--input-type=module', '--no-warnings', arg],
43    { encoding: 'utf8', input: stdin }
44  );
45
46  // stderr should include '[stdin]' as the filename
47  assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
48
49  // No stdout should be produced
50  assert.strictEqual(c.stdout, '');
51
52  // stderr should have a syntax error message
53  assert(syntaxErrorRE.test(c.stderr), `${syntaxErrorRE} === ${c.stderr}`);
54
55  assert.strictEqual(c.status, 1);
56});
57