Skip to content

Commit 6dce866

Browse files
committed
feature: @putout/printer: DirectiveLiteral: no raw
1 parent ff8d51a commit 6dce866

3 files changed

Lines changed: 56 additions & 6 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
3+
module.exports.DirectiveLiteral = (path, {write}) => {
4+
write.indent();
5+
write(path.node.raw || `'${path.node.value}'`);
6+
write(';');
7+
write.newline();
8+
};
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const {createTest} = require('#test');
4+
const {parse, transform} = require('putout');
5+
6+
const {print} = require('#printer');
7+
const {types} = require('@putout/babel');
8+
const {
9+
directiveLiteral,
10+
directive,
11+
} = types;
12+
const {test, fixture} = createTest(__dirname);
13+
14+
test('printer: tokenizer: literals: DirectiveLiteral', (t) => {
15+
t.print(fixture.directiveLiteral);
16+
t.end();
17+
});
18+
19+
test('printer: tokenize: literals: DirectiveLiteral: no raw', (t) => {
20+
const source = '';
21+
22+
const ast = parse(source, {
23+
printer: 'putout',
24+
});
25+
26+
transform(ast, source, {
27+
plugins: [
28+
['convert', {
29+
report: () => '',
30+
traverse: () => ({
31+
Program(path) {
32+
path.node.directives = [directive(directiveLiteral('hello'))];
33+
},
34+
}),
35+
}],
36+
],
37+
});
38+
39+
const result = print(ast);
40+
const expected = `'hello';\n`;
41+
42+
t.equal(result, expected);
43+
t.end();
44+
});
45+

lib/tokenize/literals/index.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ const {Identifier} = require('./identifier');
55

66
const {Decorator} = require('../expressions/decorator/decorator');
77
const {StringLiteral} = require('./string-literal');
8+
const {DirectiveLiteral} = require('./directive-literal');
89

910
module.exports = {
1011
Identifier,
1112
Decorator,
13+
DirectiveLiteral,
1214
TemplateLiteral,
1315
BigIntLiteral(path, {write}) {
1416
write(path.node.raw);
@@ -26,12 +28,6 @@ module.exports = {
2628
maybe.print.breakline(path.node.leadingComments?.length);
2729
print('__value');
2830
},
29-
DirectiveLiteral(path, {write}) {
30-
write.indent();
31-
write(path.node.raw);
32-
write(';');
33-
write.newline();
34-
},
3531
BooleanLiteral(path, {write}) {
3632
write(path.node.value);
3733
},
@@ -50,3 +46,4 @@ module.exports = {
5046
write('super');
5147
},
5248
};
49+

0 commit comments

Comments
 (0)