11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_cirequire('../common');
41cb0ef41Sopenharmony_ciconst assert = require('assert');
51cb0ef41Sopenharmony_ciconst { spawnSync } = require('child_process');
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ciconst node = process.execPath;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Test both sets of arguments that check syntax
101cb0ef41Sopenharmony_ciconst syntaxArgs = [
111cb0ef41Sopenharmony_ci  '-c',
121cb0ef41Sopenharmony_ci  '--check',
131cb0ef41Sopenharmony_ci];
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci// Match on the name of the `Error` but not the message as it is different
161cb0ef41Sopenharmony_ci// depending on the JavaScript engine.
171cb0ef41Sopenharmony_ciconst syntaxErrorRE = /^SyntaxError: Unexpected identifier\b/m;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ci// Should throw if code piped from stdin with --check has bad syntax
201cb0ef41Sopenharmony_ci// loop each possible option, `-c` or `--check`
211cb0ef41Sopenharmony_cisyntaxArgs.forEach(function(arg) {
221cb0ef41Sopenharmony_ci  const stdin = 'var foo bar;';
231cb0ef41Sopenharmony_ci  const c = spawnSync(node, [arg], { encoding: 'utf8', input: stdin });
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  // stderr should include '[stdin]' as the filename
261cb0ef41Sopenharmony_ci  assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ci  // No stdout should be produced
291cb0ef41Sopenharmony_ci  assert.strictEqual(c.stdout, '');
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  // stderr should have a syntax error message
321cb0ef41Sopenharmony_ci  assert.match(c.stderr, syntaxErrorRE);
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  assert.strictEqual(c.status, 1);
351cb0ef41Sopenharmony_ci});
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci// Check --input-type=module
381cb0ef41Sopenharmony_cisyntaxArgs.forEach(function(arg) {
391cb0ef41Sopenharmony_ci  const stdin = 'export var p = 5; var foo bar;';
401cb0ef41Sopenharmony_ci  const c = spawnSync(
411cb0ef41Sopenharmony_ci    node,
421cb0ef41Sopenharmony_ci    ['--input-type=module', '--no-warnings', arg],
431cb0ef41Sopenharmony_ci    { encoding: 'utf8', input: stdin }
441cb0ef41Sopenharmony_ci  );
451cb0ef41Sopenharmony_ci
461cb0ef41Sopenharmony_ci  // stderr should include '[stdin]' as the filename
471cb0ef41Sopenharmony_ci  assert(c.stderr.startsWith('[stdin]'), `${c.stderr} starts with ${stdin}`);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci  // No stdout should be produced
501cb0ef41Sopenharmony_ci  assert.strictEqual(c.stdout, '');
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  // stderr should have a syntax error message
531cb0ef41Sopenharmony_ci  assert.match(c.stderr, syntaxErrorRE);
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_ci  assert.strictEqual(c.status, 1);
561cb0ef41Sopenharmony_ci});
57